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