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)
| 3297 | |
| 3298 | |
| 3299 | def CheckOperatorSpacing(filename, clean_lines, linenum, error): |
| 3300 | """Checks for horizontal spacing around operators. |
| 3301 | |
| 3302 | Args: |
| 3303 | filename: The name of the current file. |
| 3304 | clean_lines: A CleansedLines instance containing the file. |
| 3305 | linenum: The number of the line to check. |
| 3306 | error: The function to call with any errors found. |
| 3307 | """ |
| 3308 | line = clean_lines.elided[linenum] |
| 3309 | |
| 3310 | # Don't try to do spacing checks for operator methods. Do this by |
| 3311 | # replacing the troublesome characters with something else, |
| 3312 | # preserving column position for all other characters. |
| 3313 | # |
| 3314 | # The replacement is done repeatedly to avoid false positives from |
| 3315 | # operators that call operators. |
| 3316 | while True: |
| 3317 | match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line) |
| 3318 | if match: |
| 3319 | line = match.group(1) + ('_' * len(match.group(2))) + match.group(3) |
| 3320 | else: |
| 3321 | break |
| 3322 | |
| 3323 | # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )". |
| 3324 | # Otherwise not. Note we only check for non-spaces on *both* sides; |
| 3325 | # sometimes people put non-spaces on one side when aligning ='s among |
| 3326 | # many lines (not that this is behavior that I approve of...) |
| 3327 | if ((Search(r'[\w.]=', line) or |
| 3328 | Search(r'=[\w.]', line)) |
| 3329 | and not Search(r'\b(if|while|for) ', line) |
| 3330 | # Operators taken from [lex.operators] in C++11 standard. |
| 3331 | and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line) |
| 3332 | and not Search(r'operator=', line)): |
| 3333 | error(filename, linenum, 'whitespace/operators', 4, |
| 3334 | 'Missing spaces around =') |
| 3335 | |
| 3336 | # It's ok not to have spaces around binary operators like + - * /, but if |
| 3337 | # there's too little whitespace, we get concerned. It's hard to tell, |
| 3338 | # though, so we punt on this one for now. TODO. |
| 3339 | |
| 3340 | # You should always have whitespace around binary operators. |
| 3341 | # |
| 3342 | # Check <= and >= first to avoid false positives with < and >, then |
| 3343 | # check non-include lines for spacing around < and >. |
| 3344 | # |
| 3345 | # If the operator is followed by a comma, assume it's be used in a |
| 3346 | # macro context and don't do any checks. This avoids false |
| 3347 | # positives. |
| 3348 | # |
| 3349 | # Note that && is not included here. This is because there are too |
| 3350 | # many false positives due to RValue references. |
| 3351 | match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line) |
| 3352 | if match: |
| 3353 | error(filename, linenum, 'whitespace/operators', 3, |
| 3354 | 'Missing spaces around %s' % match.group(1)) |
| 3355 | elif not Match(r'#.*include', line): |
| 3356 | # Look for < that is not surrounded by spaces. This is only |
no test coverage detected
searching dependent graphs…