Checks for horizontal spacing around operators. Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. error: The function to call with any errors found.
(filename, clean_lines, linenum, error)
| 3125 | |
| 3126 | |
| 3127 | def CheckOperatorSpacing(filename, clean_lines, linenum, error): |
| 3128 | """Checks for horizontal spacing around operators. |
| 3129 | |
| 3130 | Args: |
| 3131 | filename: The name of the current file. |
| 3132 | clean_lines: A CleansedLines instance containing the file. |
| 3133 | linenum: The number of the line to check. |
| 3134 | error: The function to call with any errors found. |
| 3135 | """ |
| 3136 | line = clean_lines.elided[linenum] |
| 3137 | |
| 3138 | # Don't try to do spacing checks for operator methods. Do this by |
| 3139 | # replacing the troublesome characters with something else, |
| 3140 | # preserving column position for all other characters. |
| 3141 | # |
| 3142 | # The replacement is done repeatedly to avoid false positives from |
| 3143 | # operators that call operators. |
| 3144 | while True: |
| 3145 | match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line) |
| 3146 | if match: |
| 3147 | line = match.group(1) + ('_' * len(match.group(2))) + match.group(3) |
| 3148 | else: |
| 3149 | break |
| 3150 | |
| 3151 | # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )". |
| 3152 | # Otherwise not. Note we only check for non-spaces on *both* sides; |
| 3153 | # sometimes people put non-spaces on one side when aligning ='s among |
| 3154 | # many lines (not that this is behavior that I approve of...) |
| 3155 | if ((Search(r'[\w.]=', line) or |
| 3156 | Search(r'=[\w.]', line)) |
| 3157 | and not Search(r'\b(if|while|for) ', line) |
| 3158 | # Operators taken from [lex.operators] in C++11 standard. |
| 3159 | and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line) |
| 3160 | and not Search(r'operator=', line)): |
| 3161 | error(filename, linenum, 'whitespace/operators', 4, |
| 3162 | 'Missing spaces around =') |
| 3163 | |
| 3164 | # It's ok not to have spaces around binary operators like + - * /, but if |
| 3165 | # there's too little whitespace, we get concerned. It's hard to tell, |
| 3166 | # though, so we punt on this one for now. TODO. |
| 3167 | |
| 3168 | # You should always have whitespace around binary operators. |
| 3169 | # |
| 3170 | # Check <= and >= first to avoid false positives with < and >, then |
| 3171 | # check non-include lines for spacing around < and >. |
| 3172 | # |
| 3173 | # If the operator is followed by a comma, assume it's be used in a |
| 3174 | # macro context and don't do any checks. This avoids false |
| 3175 | # positives. |
| 3176 | # |
| 3177 | # Note that && is not included here. Those are checked separately |
| 3178 | # in CheckRValueReference |
| 3179 | match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line) |
| 3180 | if match: |
| 3181 | error(filename, linenum, 'whitespace/operators', 3, |
| 3182 | 'Missing spaces around %s' % match.group(1)) |
| 3183 | elif not Match(r'#.*include', line): |
| 3184 | # Look for < that is not surrounded by spaces. This is only |
no test coverage detected