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 error
(line, filename, linenum, next_line_start, error)
| 4139 | |
| 4140 | |
| 4141 | def CheckComment(line, filename, linenum, next_line_start, error): |
| 4142 | """Checks for common mistakes in comments. |
| 4143 | |
| 4144 | Args: |
| 4145 | line: The line in question. |
| 4146 | filename: The name of the current file. |
| 4147 | linenum: The number of the line to check. |
| 4148 | next_line_start: The first non-whitespace column of the next line. |
| 4149 | error: The function to call with any errors found. |
| 4150 | """ |
| 4151 | commentpos = line.find("//") |
| 4152 | if commentpos != -1: |
| 4153 | # Check if the // may be in quotes. If so, ignore it |
| 4154 | if re.sub(r"\\.", "", line[0:commentpos]).count('"') % 2 == 0: |
| 4155 | # Allow one space for new scopes, two spaces otherwise: |
| 4156 | if not (re.match(r"^.*{ *//", line) and next_line_start == commentpos) and ( |
| 4157 | (commentpos >= 1 and line[commentpos - 1] not in string.whitespace) |
| 4158 | or (commentpos >= 2 and line[commentpos - 2] not in string.whitespace) |
| 4159 | ): |
| 4160 | error( |
| 4161 | filename, |
| 4162 | linenum, |
| 4163 | "whitespace/comments", |
| 4164 | 2, |
| 4165 | "At least two spaces is best between code and comments", |
| 4166 | ) |
| 4167 | |
| 4168 | # Checks for common mistakes in TODO comments. |
| 4169 | comment = line[commentpos:] |
| 4170 | match = _RE_PATTERN_TODO.match(comment) |
| 4171 | if match: |
| 4172 | # One whitespace is correct; zero whitespace is handled elsewhere. |
| 4173 | leading_whitespace = match.group(1) |
| 4174 | if len(leading_whitespace) > 1: |
| 4175 | error(filename, linenum, "whitespace/todo", 2, "Too many spaces before TODO") |
| 4176 | |
| 4177 | username = match.group(2) |
| 4178 | if not username: |
| 4179 | error( |
| 4180 | filename, |
| 4181 | linenum, |
| 4182 | "readability/todo", |
| 4183 | 2, |
| 4184 | "Missing username in TODO; it should look like " |
| 4185 | '"// TODO(my_username): Stuff."', |
| 4186 | ) |
| 4187 | |
| 4188 | middle_whitespace = match.group(3) |
| 4189 | # Comparisons made explicit for correctness |
| 4190 | # -- pylint: disable=g-explicit-bool-comparison |
| 4191 | if middle_whitespace not in {" ", ""}: |
| 4192 | error( |
| 4193 | filename, |
| 4194 | linenum, |
| 4195 | "whitespace/todo", |
| 4196 | 2, |
| 4197 | "TODO(my_username) should be followed by a space", |
| 4198 | ) |
no test coverage detected
searching dependent graphs…