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)
| 4352 | |
| 4353 | |
| 4354 | def CheckTrailingSemicolon(filename, clean_lines, linenum, error): |
| 4355 | """Looks for redundant trailing semicolon. |
| 4356 | |
| 4357 | Args: |
| 4358 | filename: The name of the current file. |
| 4359 | clean_lines: A CleansedLines instance containing the file. |
| 4360 | linenum: The number of the line to check. |
| 4361 | error: The function to call with any errors found. |
| 4362 | """ |
| 4363 | |
| 4364 | line = clean_lines.elided[linenum] |
| 4365 | |
| 4366 | # Block bodies should not be followed by a semicolon. Due to C++11 |
| 4367 | # brace initialization, there are more places where semicolons are |
| 4368 | # required than not, so we explicitly list the allowed rules rather |
| 4369 | # than listing the disallowed ones. These are the places where "};" |
| 4370 | # should be replaced by just "}": |
| 4371 | # 1. Some flavor of block following closing parenthesis: |
| 4372 | # for (;;) {}; |
| 4373 | # while (...) {}; |
| 4374 | # switch (...) {}; |
| 4375 | # Function(...) {}; |
| 4376 | # if (...) {}; |
| 4377 | # if (...) else if (...) {}; |
| 4378 | # |
| 4379 | # 2. else block: |
| 4380 | # if (...) else {}; |
| 4381 | # |
| 4382 | # 3. const member function: |
| 4383 | # Function(...) const {}; |
| 4384 | # |
| 4385 | # 4. Block following some statement: |
| 4386 | # x = 42; |
| 4387 | # {}; |
| 4388 | # |
| 4389 | # 5. Block at the beginning of a function: |
| 4390 | # Function(...) { |
| 4391 | # {}; |
| 4392 | # } |
| 4393 | # |
| 4394 | # Note that naively checking for the preceding "{" will also match |
| 4395 | # braces inside multi-dimensional arrays, but this is fine since |
| 4396 | # that expression will not contain semicolons. |
| 4397 | # |
| 4398 | # 6. Block following another block: |
| 4399 | # while (true) {} |
| 4400 | # {}; |
| 4401 | # |
| 4402 | # 7. End of namespaces: |
| 4403 | # namespace {}; |
| 4404 | # |
| 4405 | # These semicolons seems far more common than other kinds of |
| 4406 | # redundant semicolons, possibly due to people converting classes |
| 4407 | # to namespaces. For now we do not warn for this case. |
| 4408 | # |
| 4409 | # Try matching case 1 first. |
| 4410 | match = Match(r'^(.*\)\s*)\{', line) |
| 4411 | if match: |
no test coverage detected