mirror of
https://github.com/servo/servo.git
synced 2025-07-30 18:50:36 +01:00
Deal with multiline comments and reduce false positives for common syntax overlaps. Improve error messages.
This commit is contained in:
parent
4e593280ff
commit
7f935f010b
1 changed files with 40 additions and 9 deletions
|
@ -166,17 +166,30 @@ def check_toml(file_name, contents):
|
||||||
|
|
||||||
|
|
||||||
def check_rust(file_name, contents):
|
def check_rust(file_name, contents):
|
||||||
if not file_name.endswith(".rs") or file_name.endswith("properties.mako.rs"):
|
if not file_name.endswith(".rs") or \
|
||||||
|
file_name.endswith("properties.mako.rs") or \
|
||||||
|
file_name.endswith("style/build.rs") or \
|
||||||
|
file_name.endswith("unit/style/stylesheets.rs"):
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
contents = contents.splitlines(True)
|
contents = contents.splitlines(True)
|
||||||
|
comment_depth = 0
|
||||||
merged_lines = ''
|
merged_lines = ''
|
||||||
for idx, line in enumerate(contents):
|
for idx, line in enumerate(contents):
|
||||||
# simplify the analisis
|
# simplify the analisis
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
|
|
||||||
|
if line.find('/*') != -1:
|
||||||
|
if line.find('*/') == -1:
|
||||||
|
comment_depth += 1
|
||||||
|
elif line.find('*/') != -1:
|
||||||
|
comment_depth -= 1
|
||||||
|
|
||||||
if line.endswith('\\'):
|
if line.endswith('\\'):
|
||||||
merged_lines += line[:-1]
|
merged_lines += line[:-1]
|
||||||
continue
|
continue
|
||||||
|
elif comment_depth:
|
||||||
|
merged_lines += line
|
||||||
|
continue
|
||||||
elif merged_lines:
|
elif merged_lines:
|
||||||
line = merged_lines + line
|
line = merged_lines + line
|
||||||
merged_lines = ''
|
merged_lines = ''
|
||||||
|
@ -189,17 +202,27 @@ def check_rust(file_name, contents):
|
||||||
|
|
||||||
match = re.search(r",[A-Za-z0-9]", line)
|
match = re.search(r",[A-Za-z0-9]", line)
|
||||||
if match:
|
if match:
|
||||||
yield (idx + 1, "missing space after comma")
|
yield (idx + 1, "missing space after ,")
|
||||||
|
|
||||||
match = re.search(r"[A-Za-z0-9][\+\-/\*%=]", line)
|
# Avoid flagging <Item=Foo> constructs
|
||||||
if match:
|
def is_associated_type(match, line, index):
|
||||||
yield (idx + 1, "missing space before an operator")
|
open_angle = line[0:match.end()].rfind('<')
|
||||||
|
close_angle = line[open_angle:].find('>') if open_angle != -1 else -1
|
||||||
|
is_equals = match.group(0)[index] == '='
|
||||||
|
generic_open = open_angle != -1 and open_angle < match.start()
|
||||||
|
generic_close = close_angle != -1 and close_angle + open_angle >= match.end()
|
||||||
|
return is_equals and generic_open and generic_close
|
||||||
|
|
||||||
|
# - not included because of scientific notation (1e-6)
|
||||||
|
match = re.search(r"[A-Za-z0-9][\+/\*%=]", line)
|
||||||
|
if match and not is_associated_type(match, line, 1):
|
||||||
|
yield (idx + 1, "missing space before %s" % match.group(0)[1])
|
||||||
|
|
||||||
# * not included because of dereferencing and casting
|
# * not included because of dereferencing and casting
|
||||||
# - not included because of unary negation
|
# - not included because of unary negation
|
||||||
match = re.search(r"[\+/\%=][A-Za-z0-9]", line)
|
match = re.search(r"[\+/\%=][A-Za-z0-9]", line)
|
||||||
if match:
|
if match and not is_associated_type(match, line, 0):
|
||||||
yield (idx + 1, "missing space after an operator")
|
yield (idx + 1, "missing space after %s" % match.group(0)[0])
|
||||||
|
|
||||||
match = re.search(r"\)->", line)
|
match = re.search(r"\)->", line)
|
||||||
if match:
|
if match:
|
||||||
|
@ -209,12 +232,20 @@ def check_rust(file_name, contents):
|
||||||
if match:
|
if match:
|
||||||
yield (idx + 1, "missing space after ->")
|
yield (idx + 1, "missing space after ->")
|
||||||
|
|
||||||
if line.find(" :") != -1:
|
# Avoid flagging ::crate::mod
|
||||||
|
if line.find(" :[^:]") != -1:
|
||||||
yield (idx + 1, "extra space before :")
|
yield (idx + 1, "extra space before :")
|
||||||
|
|
||||||
|
# Avoid flagging crate::mod
|
||||||
|
match = re.search(r"[^:]:[A-Za-z]", line)
|
||||||
|
if match:
|
||||||
|
# Avoid flagging macros like $t1:expr
|
||||||
|
if line[0:match.end()].rfind('$') == -1:
|
||||||
|
yield (idx + 1, "missing space after :")
|
||||||
|
|
||||||
match = re.search(r"[A-Za-z0-9\)]{", line)
|
match = re.search(r"[A-Za-z0-9\)]{", line)
|
||||||
if match:
|
if match:
|
||||||
yield (idx + 1, "extra space before {")
|
yield (idx + 1, "missing space before {")
|
||||||
|
|
||||||
if line.startswith("use ") and "{" in line and "}" not in line:
|
if line.startswith("use ") and "{" in line and "}" not in line:
|
||||||
yield (idx + 1, "use statement spans multiple lines")
|
yield (idx + 1, "use statement spans multiple lines")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue