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)
| 4071 | |
| 4072 | |
| 4073 | def CheckComment(line, filename, linenum, next_line_start, error): |
| 4074 | """Checks for common mistakes in comments. |
| 4075 | |
| 4076 | Args: |
| 4077 | line: The line in question. |
| 4078 | filename: The name of the current file. |
| 4079 | linenum: The number of the line to check. |
| 4080 | next_line_start: The first non-whitespace column of the next line. |
| 4081 | error: The function to call with any errors found. |
| 4082 | """ |
| 4083 | commentpos = line.find("//") |
| 4084 | if commentpos != -1: |
| 4085 | # Check if the // may be in quotes. If so, ignore it |
| 4086 | if re.sub(r"\\.", "", line[0:commentpos]).count('"') % 2 == 0: |
| 4087 | # Allow one space for new scopes, two spaces otherwise: |
| 4088 | if not (re.match(r"^.*{ *//", line) and next_line_start == commentpos) and ( |
| 4089 | (commentpos >= 1 and line[commentpos - 1] not in string.whitespace) |
| 4090 | or (commentpos >= 2 and line[commentpos - 2] not in string.whitespace) |
| 4091 | ): |
| 4092 | error( |
| 4093 | filename, |
| 4094 | linenum, |
| 4095 | "whitespace/comments", |
| 4096 | 2, |
| 4097 | "At least two spaces is best between code and comments", |
| 4098 | ) |
| 4099 | |
| 4100 | # Checks for common mistakes in TODO comments. |
| 4101 | comment = line[commentpos:] |
| 4102 | match = _RE_PATTERN_TODO.match(comment) |
| 4103 | if match: |
| 4104 | # One whitespace is correct; zero whitespace is handled elsewhere. |
| 4105 | leading_whitespace = match.group(1) |
| 4106 | if len(leading_whitespace) > 1: |
| 4107 | error(filename, linenum, "whitespace/todo", 2, "Too many spaces before TODO") |
| 4108 | |
| 4109 | username = match.group(2) |
| 4110 | if not username: |
| 4111 | error( |
| 4112 | filename, |
| 4113 | linenum, |
| 4114 | "readability/todo", |
| 4115 | 2, |
| 4116 | "Missing username in TODO; it should look like " |
| 4117 | '"// TODO(my_username): Stuff."', |
| 4118 | ) |
| 4119 | |
| 4120 | middle_whitespace = match.group(3) |
| 4121 | # Comparisons made explicit for correctness |
| 4122 | # -- pylint: disable=g-explicit-bool-comparison |
| 4123 | if middle_whitespace not in {" ", ""}: |
| 4124 | error( |
| 4125 | filename, |
| 4126 | linenum, |
| 4127 | "whitespace/todo", |
| 4128 | 2, |
| 4129 | "TODO(my_username) should be followed by a space", |
| 4130 | ) |