Checks for common mistakes in comments. Args: line: The line in question. filename: The name of the current file. linenum: The number of the line to check. next_line_start: The first non-whitespace column of the next line. error: The function to call with any errors found.
(line, filename, linenum, next_line_start, error)
| 3611 | |
| 3612 | |
| 3613 | def CheckComment(line, filename, linenum, next_line_start, error): |
| 3614 | """Checks for common mistakes in comments. |
| 3615 | |
| 3616 | Args: |
| 3617 | line: The line in question. |
| 3618 | filename: The name of the current file. |
| 3619 | linenum: The number of the line to check. |
| 3620 | next_line_start: The first non-whitespace column of the next line. |
| 3621 | error: The function to call with any errors found. |
| 3622 | """ |
| 3623 | commentpos = line.find('//') |
| 3624 | if commentpos != -1: |
| 3625 | # Check if the // may be in quotes. If so, ignore it |
| 3626 | if re.sub(r'\\.', '', line[0:commentpos]).count('"') % 2 == 0: |
| 3627 | # Allow one space for new scopes, two spaces otherwise: |
| 3628 | if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and |
| 3629 | ((commentpos >= 1 and |
| 3630 | line[commentpos-1] not in string.whitespace) or |
| 3631 | (commentpos >= 2 and |
| 3632 | line[commentpos-2] not in string.whitespace))): |
| 3633 | error(filename, linenum, 'whitespace/comments', 2, |
| 3634 | 'At least two spaces is best between code and comments') |
| 3635 | |
| 3636 | # Checks for common mistakes in TODO comments. |
| 3637 | comment = line[commentpos:] |
| 3638 | match = _RE_PATTERN_TODO.match(comment) |
| 3639 | if match: |
| 3640 | # One whitespace is correct; zero whitespace is handled elsewhere. |
| 3641 | leading_whitespace = match.group(1) |
| 3642 | if len(leading_whitespace) > 1: |
| 3643 | error(filename, linenum, 'whitespace/todo', 2, |
| 3644 | 'Too many spaces before TODO') |
| 3645 | |
| 3646 | username = match.group(2) |
| 3647 | if not username: |
| 3648 | error(filename, linenum, 'readability/todo', 2, |
| 3649 | 'Missing username in TODO; it should look like ' |
| 3650 | '"// TODO(my_username): Stuff."') |
| 3651 | |
| 3652 | middle_whitespace = match.group(3) |
| 3653 | # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison |
| 3654 | if middle_whitespace != ' ' and middle_whitespace != '': |
| 3655 | error(filename, linenum, 'whitespace/todo', 2, |
| 3656 | 'TODO(my_username) should be followed by a space') |
| 3657 | |
| 3658 | # If the comment contains an alphanumeric character, there |
| 3659 | # should be a space somewhere between it and the // unless |
| 3660 | # it's a /// or //! Doxygen comment. |
| 3661 | if (Match(r'//[^ ]*\w', comment) and |
| 3662 | not Match(r'(///|//\!)(\s+|$)', comment)): |
| 3663 | error(filename, linenum, 'whitespace/comments', 4, |
| 3664 | 'Should have a space between // and comment') |
| 3665 | |
| 3666 | |
| 3667 | def CheckSpacing(filename, clean_lines, linenum, nesting_state, error): |
no test coverage detected