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)
| 3118 | |
| 3119 | |
| 3120 | def CheckComment(line, filename, linenum, next_line_start, error): |
| 3121 | """Checks for common mistakes in comments. |
| 3122 | |
| 3123 | Args: |
| 3124 | line: The line in question. |
| 3125 | filename: The name of the current file. |
| 3126 | linenum: The number of the line to check. |
| 3127 | next_line_start: The first non-whitespace column of the next line. |
| 3128 | error: The function to call with any errors found. |
| 3129 | """ |
| 3130 | commentpos = line.find('//') |
| 3131 | if commentpos != -1: |
| 3132 | # Check if the // may be in quotes. If so, ignore it |
| 3133 | if re.sub(r'\\.', '', line[0:commentpos]).count('"') % 2 == 0: |
| 3134 | # Allow one space for new scopes, two spaces otherwise: |
| 3135 | if (commentpos >= 1 and |
| 3136 | line[commentpos-1] not in string.whitespace): |
| 3137 | error(filename, linenum, 'whitespace/comments', 2, |
| 3138 | 'At least a single space is required between code and comments') |
| 3139 | |
| 3140 | # Checks for common mistakes in TODO comments. |
| 3141 | comment = line[commentpos:] |
| 3142 | match = _RE_PATTERN_TODO.match(comment) |
| 3143 | if match: |
| 3144 | # One whitespace is correct; zero whitespace is handled elsewhere. |
| 3145 | leading_whitespace = match.group(1) |
| 3146 | if len(leading_whitespace) > 1: |
| 3147 | error(filename, linenum, 'whitespace/todo', 2, |
| 3148 | 'Too many spaces before TODO') |
| 3149 | |
| 3150 | username = match.group(2) |
| 3151 | if not username: |
| 3152 | error(filename, linenum, 'readability/todo', 2, |
| 3153 | 'Missing username in TODO; it should look like ' |
| 3154 | '"// TODO(my_username): Stuff."') |
| 3155 | |
| 3156 | middle_whitespace = match.group(3) |
| 3157 | # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison |
| 3158 | if middle_whitespace != ' ' and middle_whitespace != '': |
| 3159 | error(filename, linenum, 'whitespace/todo', 2, |
| 3160 | 'TODO(my_username) should be followed by a space') |
| 3161 | |
| 3162 | # If the comment contains an alphanumeric character, there |
| 3163 | # should be a space somewhere between it and the // unless |
| 3164 | # it's a /// or //! Doxygen comment. |
| 3165 | if (Match(r'//[^ ]*\w', comment) and |
| 3166 | not Match(r'(///|//\!)(\s+|$)', comment)): |
| 3167 | error(filename, linenum, 'whitespace/comments', 4, |
| 3168 | 'Should have a space between // and comment') |
| 3169 | |
| 3170 | |
| 3171 | def CheckSpacing(filename, clean_lines, linenum, nesting_state, error): |