diff --git a/cpplint.py b/cpplint.py index 07687d2..2f97c63 100755 --- a/cpplint.py +++ b/cpplint.py @@ -3943,14 +3943,15 @@ def CheckForNonStandardConstructs(filename, clean_lines, linenum, nesting_state, else: constructor_args = explicit_constructor_match.group(2).split(",") - # collapse arguments so that commas in template parameter lists and function - # argument parameter lists don't split arguments in two + # Collapse commas inside template and function parameter lists. i = 0 while i < len(constructor_args): constructor_arg = constructor_args[i] - while constructor_arg.count("<") > constructor_arg.count(">") or constructor_arg.count( - "(" - ) > constructor_arg.count(")"): + # Ignore `<`-prefixed operators when balancing template brackets. + while i + 1 < len(constructor_args) and ( + re.sub(r"<<=?|<=", "", constructor_arg).count("<") > constructor_arg.count(">") + or constructor_arg.count("(") > constructor_arg.count(")") + ): constructor_arg += "," + constructor_args[i + 1] del constructor_args[i + 1] constructor_args[i] = constructor_arg diff --git a/cpplint_unittest.py b/cpplint_unittest.py index d568251..48fd9c4 100755 --- a/cpplint_unittest.py +++ b/cpplint_unittest.py @@ -1810,6 +1810,37 @@ class Foo { "Constructors callable with one argument should be marked explicit." " [runtime/explicit] [4]", ) + # `<`-containing operators are not confused with template brackets. + self.TestMultiLineLint( + """ + class A { + A(int a, int b, int c = 1 << 1); + };""", + "", + ) + self.TestMultiLineLint( + """ + class A { + A(int a = (value <<= 1), int b = 0, int c = 0); + };""", + "Constructors callable with one argument should be marked explicit." + " [runtime/explicit] [4]", + ) + self.TestMultiLineLint( + """ + class A { + A(int a, bool b = 1 <= 1, int c = 0); + };""", + "Constructors callable with one argument should be marked explicit." + " [runtime/explicit] [4]", + ) + self.TestMultiLineLint( + """ + class A { + A(int a, int b, bool c = 1 < 2); + };""", + "", + ) # explicit no-argument constructors are just fine self.TestMultiLineLint( """