[Fix] Improved Indent testing

Improved indent testing to test for more than 4 leading spaces as well as improved continuation line testing/exceptions.

Added Unit test for valid Boolean Comparators that we consider to be valid line continuations.
This commit is contained in:
Kevin Powell 2016-10-06 14:05:18 -04:00
parent 545aca25cb
commit dc0e2f067f
2 changed files with 148 additions and 10 deletions

31
cpplint/cpplint.py vendored
View File

@ -4275,22 +4275,43 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$'
classinfo = nesting_state.InnermostClass()
initial_spaces = 0
complain = 1
cleansed_line = clean_lines.elided[linenum]
while initial_spaces < len(line) and line[initial_spaces] == ' ':
initial_spaces += 1
initial_spaces = GetIndentLevel(line)
# There are certain situations we allow one space, notably for
# section labels, and also lines containing multi-line raw strings.
# We also don't check for lines that look like continuation lines
# (of lines ending in double quotes, commas, equals, or angle brackets)
# because the rules for how to indent those are non-trivial.
if (not Search(r'[",=><] *$', prev) and
(initial_spaces == 1 or initial_spaces == 3) and
# We also don't check for lines that have a number of leading spaces
# greater than 1/4 of our line length.
if (initial_spaces > (_line_length / 4)):
complain = 0
if (Search(r' +(error|private|public|protected):', prev)):
complain = 0
if (Search(r'&& *$', prev)) or (Search(r'^ *&&', line)):
complain = 0
if (Search(r'\|\| *$', prev)) or (Search(r'^ *\|\|', line)):
complain = 0
if (Search(r'[=><!]= *$', prev)) or (Search(r'^ *[=><!]=', line)):
complain = 0
if (Search(r'[><!] *$', prev)) or (Search(r'^ *[><!]', prev)):
complain = 0
if (Search(r'[",=] *$', prev)):
complain = 0
if (Search(r'[\+\-\*\\/] *$', prev)):
complain = 0
if (Search(r' +for \(', prev)):
complain = 0
if (not (complain == 0) and
#(initial_spaces == 1 or initial_spaces == 3) and
((initial_spaces % _indent_spaces) > 0) and
not Match(scope_or_label_pattern, cleansed_line) and
not (clean_lines.raw_lines[linenum] != line and
Match(r'^\s*""', line))):
error(filename, linenum, 'whitespace/indent', 3,
'Weird number of spaces at line-start. '
'Are you using a 2-space indent?')
if line and line[-1].isspace():
error(filename, linenum, 'whitespace/end_of_line', 4,

View File

@ -3459,12 +3459,19 @@ class CpplintTest(CpplintTestBase):
self.TestLint('static int noindent;', '')
self.TestLint(' int two_space_indent;', '')
self.TestLint(' int four_space_indent;', '')
self.TestLint(' int six_space_indent;', '')
self.TestLint(' int one_space_indent;',
'Weird number of spaces at line-start. '
'Are you using a 2-space indent? [whitespace/indent] [3]')
self.TestLint(' int three_space_indent;',
'Weird number of spaces at line-start. '
'Are you using a 2-space indent? [whitespace/indent] [3]')
self.TestLint(' int five_space_indent;',
'Weird number of spaces at line-start. '
'Are you using a 2-space indent? [whitespace/indent] [3]')
self.TestLint(' int seven_space_indent;',
'Weird number of spaces at line-start. '
'Are you using a 2-space indent? [whitespace/indent] [3]')
self.TestLint(' char* one_space_indent = "public:";',
'Weird number of spaces at line-start. '
'Are you using a 2-space indent? [whitespace/indent] [3]')
@ -3496,6 +3503,114 @@ class CpplintTest(CpplintTestBase):
' static const char kSingleLineRawString[] = R"(...)";',
'Weird number of spaces at line-start. '
'Are you using a 2-space indent? [whitespace/indent] [3]')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo &&
bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo
&& bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo ||
bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo
|| bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo ==
bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo
== bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo !=
bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo
!= bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo <=
bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo
<= bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo >=
bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo
>= bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo <
bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo
< bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo >
bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo
> bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo && !
bar)
goto pass;'''),
'')
self.TestMultiLineLint(
TrimExtraIndent('''
if (foo &&
!(bar))
goto pass;'''),
'')
def testSectionIndent(self):
self.TestMultiLineLint(
@ -3520,12 +3635,12 @@ class CpplintTest(CpplintTestBase):
self.TestMultiLineLint(
"""
struct D {
};""",
};""",
'Closing brace should be aligned with beginning of struct D'
' [whitespace/indent] [3]')
self.TestMultiLineLint(
"""
template<typename E> class F {
template<typename E> class F {
};""",
'Closing brace should be aligned with beginning of class F'
' [whitespace/indent] [3]')
@ -3960,8 +4075,9 @@ class CpplintTest(CpplintTestBase):
'class Foo;',
'')
self.TestMultiLineLint(
"""struct Foo*
foo = NewFoo();""",
"""
struct Foo*
foo = NewFoo();""",
'')
# Test preprocessor.
self.TestMultiLineLint(
@ -3992,7 +4108,8 @@ class CpplintTest(CpplintTestBase):
# The crosstool compiler we currently use will fail to compile the
# code in this test, so we might consider removing the lint check.
self.TestMultiLineLint(
"""#if 0
"""
#if 0
#endif Not a comment""",
'Uncommented text after #endif is non-standard. Use a comment.'
' [build/endif_comment] [5]')