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)
| 3807 | |
| 3808 | |
| 3809 | def CheckOperatorSpacing(filename, clean_lines, linenum, error): |
| 3810 | """Checks for horizontal spacing around operators. |
| 3811 | |
| 3812 | Args: |
| 3813 | filename: The name of the current file. |
| 3814 | clean_lines: A CleansedLines instance containing the file. |
| 3815 | linenum: The number of the line to check. |
| 3816 | error: The function to call with any errors found. |
| 3817 | """ |
| 3818 | line = clean_lines.elided[linenum] |
| 3819 | |
| 3820 | # Don't try to do spacing checks for operator methods. Do this by |
| 3821 | # replacing the troublesome characters with something else, |
| 3822 | # preserving column position for all other characters. |
| 3823 | # |
| 3824 | # The replacement is done repeatedly to avoid false positives from |
| 3825 | # operators that call operators. |
| 3826 | while True: |
| 3827 | match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line) |
| 3828 | if match: |
| 3829 | line = match.group(1) + ('_' * len(match.group(2))) + match.group(3) |
| 3830 | else: |
| 3831 | break |
| 3832 | |
| 3833 | # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )". |
| 3834 | # Otherwise not. Note we only check for non-spaces on *both* sides; |
| 3835 | # sometimes people put non-spaces on one side when aligning ='s among |
| 3836 | # many lines (not that this is behavior that I approve of...) |
| 3837 | if ((Search(r'[\w.]=', line) or |
| 3838 | Search(r'=[\w.]', line)) |
| 3839 | and not Search(r'\b(if|while|for) ', line) |
| 3840 | # Operators taken from [lex.operators] in C++11 standard. |
| 3841 | and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line) |
| 3842 | and not Search(r'operator=', line)): |
| 3843 | error(filename, linenum, 'whitespace/operators', 4, |
| 3844 | 'Missing spaces around =') |
| 3845 | |
| 3846 | # It's ok not to have spaces around binary operators like + - * /, but if |
| 3847 | # there's too little whitespace, we get concerned. It's hard to tell, |
| 3848 | # though, so we punt on this one for now. TODO. |
| 3849 | |
| 3850 | # You should always have whitespace around binary operators. |
| 3851 | # |
| 3852 | # Check <= and >= first to avoid false positives with < and >, then |
| 3853 | # check non-include lines for spacing around < and >. |
| 3854 | # |
| 3855 | # If the operator is followed by a comma, assume it's be used in a |
| 3856 | # macro context and don't do any checks. This avoids false |
| 3857 | # positives. |
| 3858 | # |
| 3859 | # Note that && is not included here. This is because there are too |
| 3860 | # many false positives due to RValue references. |
| 3861 | match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line) |
| 3862 | if match: |
| 3863 | error(filename, linenum, 'whitespace/operators', 3, |
| 3864 | 'Missing spaces around %s' % match.group(1)) |
| 3865 | elif not Match(r'#.*include', line): |
| 3866 | # Look for < that is not surrounded by spaces. This is only |
no test coverage detected