[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>
diff --git a/cpplint.py b/cpplint.py
index f06f677..0ddf89d 100755
--- a/cpplint.py
+++ b/cpplint.py
@@ -2832,7 +2832,7 @@
# 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 @@
# 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 @@
# 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 @@
# 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".')