Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
TBA
===

* Fixed a ``whitespace/braces`` false positive for nested brace initialization followed by an operator. (#373)
* Fixed a whitespace/newline false positive for control conditions containing lambdas. (#410)
* We now error on relative include paths (``./``, ``../``). (#432)
* This makes ``#include "./foo.h"`` produce two separate errors: that foo.cpp should include foo.h and that relative paths are not allowed.
Expand Down
11 changes: 9 additions & 2 deletions cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4745,8 +4745,15 @@ def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):
# We also suppress warnings for `uint64_t{expression}` etc., as the style
# guide recommends brace initialization for integral types to avoid
# overflow/truncation.
if not re.match(r"^[\s}]*[{.;,)<>\]:]", trailing_text) and not _IsType(
clean_lines, nesting_state, leading_text
operator_after_braced_value = (
re.match(r"^[\s}]*[+*/%&|^=!?-]", trailing_text)
and re.search(r"(?:\w|>)$", leading_text)
and not re.search(r"\brequires\s*$", leading_text)
)
if (
not re.match(r"^[\s}]*[{.;,)<>\]:]", trailing_text)
and not operator_after_braced_value
and not _IsType(clean_lines, nesting_state, leading_text)
):
error(filename, linenum, "whitespace/braces", 5, "Missing space before {")

Expand Down
38 changes: 38 additions & 0 deletions cpplint_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3097,6 +3097,14 @@ def testSpacingAfterAlternateToken(self):

def testSpacingBeforeBraces(self):
self.TestLint("if (foo){", "Missing space before { [whitespace/braces] [5]")
self.TestLint(
"concept C = requires(T t){ t.foo(); } && true;",
"Missing space before { [whitespace/braces] [5]",
)
self.TestLint(
"concept C = requires { typename T::value_type; } && true;",
"",
)
self.TestLint("for{", "Missing space before { [whitespace/braces] [5]")
self.TestLint("for {", "")
self.TestLint("EXPECT_DEBUG_DEATH({", "")
Expand Down Expand Up @@ -3212,6 +3220,36 @@ def testBraceInitializerList(self):
self.TestLint('static_assert(Max7String{}.IsValid(), "");', "")
self.TestLint("map_of_pairs[{1, 2}] = 3;", "")
self.TestLint("ItemView{has_offer() ? new Offer{offer()} : nullptr", "")
# Nested brace initialization may be followed by an operator.
# Regression test for #373.
self.TestLint(
'return std::optional{pmr_string{a} + " " + b};',
"",
)
for operator in [
"+",
"-",
"*",
"/",
"%",
"&",
"|",
"^",
"==",
"!=",
"<",
"<=",
">",
">=",
"<<",
">>",
"&&",
"||",
]:
self.TestLint(
f"return Wrapper{{Value{{a}} {operator} b}};",
"",
)
self.TestLint("template <class T, EnableIf<::std::is_const<T>{}> = 0>", "")

self.TestMultiLineLint("std::unique_ptr<Foo> foo{\n new Foo{}\n};\n", "")
Expand Down
Loading