mirror of
https://chromium.googlesource.com/chromium/tools/depot_tools.git
synced 2026-01-11 18:51:29 +00:00
[cpplint] Fix bug with "if constexpr"
This snippet of code:
if constexpr (n == 1) {
return 2;
} else if constexpr (n == 2) {
int x = 2;
int y = 2;
return x + y;
}
was triggering the warning:
If/else bodies with multiple statements require braces [readability/braces]
And, in general, cpplint.py was assuming that `if (cond)` was
the only `if` construction to expect, forgetting about
`if constexpr(cond)`.
Change-Id: I4cdc8e7f134c1ebd14d00354a8baadf87c796763
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3644147
Reviewed-by: Nico Weber <thakis@chromium.org>
Commit-Queue: Darius Mercadier <dmercadier@chromium.org>
Reviewed-by: Jochen Eisinger <jochen@chromium.org>
This commit is contained in:
12
cpplint.py
vendored
12
cpplint.py
vendored
@@ -2832,7 +2832,7 @@ def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error):
|
||||
# first see if we should be looking inside such an expression for a
|
||||
# function call, to which we can apply more strict standards.
|
||||
fncall = line # if there's no control flow construct, look at whole line
|
||||
for pattern in (r'\bif\s*\((.*)\)\s*{',
|
||||
for pattern in (r'\bif\s*(?:constexpr\s*)?\((.*)\)\s*{',
|
||||
r'\bfor\s*\((.*)\)\s*{',
|
||||
r'\bwhile\s*\((.*)\)\s*[{;]',
|
||||
r'\bswitch\s*\((.*)\)\s*{'):
|
||||
@@ -3653,8 +3653,8 @@ def CheckBraces(filename, clean_lines, linenum, error):
|
||||
|
||||
# If braces come on one side of an else, they should be on both.
|
||||
# However, we have to worry about "else if" that spans multiple lines!
|
||||
if Search(r'else if\s*\(', line): # could be multi-line if
|
||||
brace_on_left = bool(Search(r'}\s*else if\s*\(', line))
|
||||
if Search(r'else if\s*(?:constexpr\s*)?\(', line): # could be multi-line if
|
||||
brace_on_left = bool(Search(r'}\s*else if\s*(?:constexpr\s*)?\(', line))
|
||||
# find the ( after the if
|
||||
pos = line.find('else if')
|
||||
pos = line.find('(', pos)
|
||||
@@ -3685,11 +3685,11 @@ def CheckBraces(filename, clean_lines, linenum, error):
|
||||
# its line, and the line after that should have an indent level equal to or
|
||||
# lower than the if. We also check for ambiguous if/else nesting without
|
||||
# braces.
|
||||
if_else_match = Search(r'\b(if\s*\(|else\b)', line)
|
||||
if_else_match = Search(r'\b(if\s*(?:constexpr\s*)?\(|else\b)', line)
|
||||
if if_else_match and not Match(r'\s*#', line):
|
||||
if_indent = GetIndentLevel(line)
|
||||
endline, endlinenum, endpos = line, linenum, if_else_match.end()
|
||||
if_match = Search(r'\bif\s*\(', line)
|
||||
if_match = Search(r'\bif\s*(?:constexpr\s*)?\(', line)
|
||||
if if_match:
|
||||
# This could be a multiline if condition, so find the end first.
|
||||
pos = if_match.end() - 1
|
||||
@@ -4620,7 +4620,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
|
||||
|
||||
# Check for suspicious usage of "if" like
|
||||
# } if (a == b) {
|
||||
if Search(r'\}\s*if\s*\(', line):
|
||||
if Search(r'\}\s*if\s*(?:constexpr\s*)?\(', line):
|
||||
error(filename, linenum, 'readability/braces', 4,
|
||||
'Did you mean "else if"? If not, start a new line for "if".')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user