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)
| 2911 | |
| 2912 | |
| 2913 | def CheckComment(line, filename, linenum, next_line_start, error): |
| 2914 | """Checks for common mistakes in comments. |
| 2915 | |
| 2916 | Args: |
| 2917 | line: The line in question. |
| 2918 | filename: The name of the current file. |
| 2919 | linenum: The number of the line to check. |
| 2920 | next_line_start: The first non-whitespace column of the next line. |
| 2921 | error: The function to call with any errors found. |
| 2922 | """ |
| 2923 | commentpos = line.find('//') |
| 2924 | if commentpos != -1: |
| 2925 | # Check if the // may be in quotes. If so, ignore it |
| 2926 | # Comparisons made explicit for clarity -- pylint: disable=g-explicit-bool-comparison |
| 2927 | if (line.count('"', 0, commentpos) - |
| 2928 | line.count('\\"', 0, commentpos)) % 2 == 0: # not in quotes |
| 2929 | # Allow one space for new scopes, two spaces otherwise: |
| 2930 | if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and |
| 2931 | ((commentpos >= 1 and |
| 2932 | line[commentpos-1] not in string.whitespace) or |
| 2933 | (commentpos >= 2 and |
| 2934 | line[commentpos-2] not in string.whitespace))): |
| 2935 | error(filename, linenum, 'whitespace/comments', 2, |
| 2936 | 'At least two spaces is best between code and comments') |
| 2937 | |
| 2938 | # Checks for common mistakes in TODO comments. |
| 2939 | comment = line[commentpos:] |
| 2940 | match = _RE_PATTERN_TODO.match(comment) |
| 2941 | if match: |
| 2942 | # One whitespace is correct; zero whitespace is handled elsewhere. |
| 2943 | leading_whitespace = match.group(1) |
| 2944 | if len(leading_whitespace) > 1: |
| 2945 | error(filename, linenum, 'whitespace/todo', 2, |
| 2946 | 'Too many spaces before TODO') |
| 2947 | |
| 2948 | username = match.group(2) |
| 2949 | if not username: |
| 2950 | error(filename, linenum, 'readability/todo', 2, |
| 2951 | 'Missing username in TODO; it should look like ' |
| 2952 | '"// TODO(my_username): Stuff."') |
| 2953 | |
| 2954 | middle_whitespace = match.group(3) |
| 2955 | # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison |
| 2956 | if middle_whitespace != ' ' and middle_whitespace != '': |
| 2957 | error(filename, linenum, 'whitespace/todo', 2, |
| 2958 | 'TODO(my_username) should be followed by a space') |
| 2959 | |
| 2960 | # If the comment contains an alphanumeric character, there |
| 2961 | # should be a space somewhere between it and the // unless |
| 2962 | # it's a /// or //! Doxygen comment. |
| 2963 | if (Match(r'//[^ ]*\w', comment) and |
| 2964 | not Match(r'(///|//\!)(\s+|$)', comment)): |
| 2965 | error(filename, linenum, 'whitespace/comments', 4, |
| 2966 | 'Should have a space between // and comment') |
| 2967 | |
| 2968 | |
| 2969 | def CheckAccess(filename, clean_lines, linenum, nesting_state, error): |