Looks for redundant trailing semicolon. Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. error: The function to call with any errors found.
(filename, clean_lines, linenum, error)
| 4998 | |
| 4999 | |
| 5000 | def CheckTrailingSemicolon(filename, clean_lines, linenum, error): |
| 5001 | """Looks for redundant trailing semicolon. |
| 5002 | |
| 5003 | Args: |
| 5004 | filename: The name of the current file. |
| 5005 | clean_lines: A CleansedLines instance containing the file. |
| 5006 | linenum: The number of the line to check. |
| 5007 | error: The function to call with any errors found. |
| 5008 | """ |
| 5009 | |
| 5010 | line = clean_lines.elided[linenum] |
| 5011 | |
| 5012 | # Block bodies should not be followed by a semicolon. Due to C++11 |
| 5013 | # brace initialization, there are more places where semicolons are |
| 5014 | # required than not, so we explicitly list the allowed rules rather |
| 5015 | # than listing the disallowed ones. These are the places where "};" |
| 5016 | # should be replaced by just "}": |
| 5017 | # 1. Some flavor of block following closing parenthesis: |
| 5018 | # for (;;) {}; |
| 5019 | # while (...) {}; |
| 5020 | # switch (...) {}; |
| 5021 | # Function(...) {}; |
| 5022 | # if (...) {}; |
| 5023 | # if (...) else if (...) {}; |
| 5024 | # |
| 5025 | # 2. else block: |
| 5026 | # if (...) else {}; |
| 5027 | # |
| 5028 | # 3. const member function: |
| 5029 | # Function(...) const {}; |
| 5030 | # |
| 5031 | # 4. Block following some statement: |
| 5032 | # x = 42; |
| 5033 | # {}; |
| 5034 | # |
| 5035 | # 5. Block at the beginning of a function: |
| 5036 | # Function(...) { |
| 5037 | # {}; |
| 5038 | # } |
| 5039 | # |
| 5040 | # Note that naively checking for the preceding "{" will also match |
| 5041 | # braces inside multi-dimensional arrays, but this is fine since |
| 5042 | # that expression will not contain semicolons. |
| 5043 | # |
| 5044 | # 6. Block following another block: |
| 5045 | # while (true) {} |
| 5046 | # {}; |
| 5047 | # |
| 5048 | # 7. End of namespaces: |
| 5049 | # namespace {}; |
| 5050 | # |
| 5051 | # These semicolons seems far more common than other kinds of |
| 5052 | # redundant semicolons, possibly due to people converting classes |
| 5053 | # to namespaces. For now we do not warn for this case. |
| 5054 | # |
| 5055 | # Try matching case 1 first. |
| 5056 | match = re.match(r"^(.*\)\s*)\{", line) |
| 5057 | if match: |
no test coverage detected
searching dependent graphs…