Make tidy check that opening and closing braces that begin a line do so with proper alignment.

This commit is contained in:
coalman 2017-04-17 03:45:30 -04:00
parent a754c10e79
commit 57e74542ee
3 changed files with 23 additions and 3 deletions

View file

@ -450,6 +450,8 @@ def check_rust(file_name, lines):
prev_crate = {}
prev_mod = {}
prev_feature_name = ""
indent = 0
prev_indent = 0
decl_message = "{} is not in alphabetical order"
decl_expected = "\n\t\033[93mexpected: {}\033[0m"
@ -458,6 +460,9 @@ def check_rust(file_name, lines):
for idx, original_line in enumerate(lines):
# simplify the analysis
line = original_line.strip()
prev_indent = indent
indent = len(original_line) - len(line)
is_attribute = re.search(r"#\[.*\]", line)
is_comment = re.search(r"^//|^/\*|^\*", line)
@ -577,11 +582,17 @@ def check_rust(file_name, lines):
yield (idx + 1, "found an empty line following a {")
prev_open_brace = line.endswith("{")
# ensure a line starting with { or } has a number of leading spaces that is a multiple of 4
if line.startswith(("{", "}")):
match = re.match(r"(?: {4})* {1,3}([{}])", original_line)
if match:
if indent != prev_indent - 4:
yield (idx + 1, "space before {} is not a multiple of 4".format(match.group(1)))
# check alphabetical order of extern crates
if line.startswith("extern crate "):
# strip "extern crate " from the begin and ";" from the end
crate_name = line[13:-1]
indent = len(original_line) - len(line)
if indent not in prev_crate:
prev_crate[indent] = ""
if prev_crate[indent] > crate_name:
@ -616,7 +627,6 @@ def check_rust(file_name, lines):
# into a single import block
if line.startswith("use "):
import_block = True
indent = len(original_line) - len(line)
if not line.endswith(";") and '{' in line:
yield (idx + 1, "use statement spans multiple lines")
if '{ ' in line:
@ -645,7 +655,6 @@ def check_rust(file_name, lines):
# modules must be in the same line and alphabetically sorted
if line.startswith("mod ") or line.startswith("pub mod "):
indent = len(original_line) - len(line)
# strip /(pub )?mod/ from the left and ";" from the right
mod = line[4:-1] if line.startswith("mod ") else line[8:-1]

View file

@ -63,4 +63,13 @@ impl test {
let var
= "val";
fn test_fun4()
{
}
let var = if true {
"true"
} else { // Should not trigger
"false"
} // Should not trigger
}

View file

@ -131,6 +131,8 @@ class CheckTidiness(unittest.TestCase):
self.assertEqual('extra space after (', errors.next()[2])
self.assertEqual('extra space after test_fun', errors.next()[2])
self.assertEqual('no = in the beginning of line', errors.next()[2])
self.assertEqual('space before { is not a multiple of 4', errors.next()[2])
self.assertEqual('space before } is not a multiple of 4', errors.next()[2])
self.assertNoMoreErrors(errors)
feature_errors = tidy.collect_errors_for_files(iterFile('lib.rs'), [], [tidy.check_rust], print_text=False)