Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-tidy] Fix crashes on
if consteval
in readability checks
The `readability-braces-around-statements` check tries to look at the closing parens of the if condition to determine where to insert braces, however, "consteval if" statements don't have a condition, and always have braces regardless, so the skip can be checked. The `readability-simplify-boolean-expr` check looks at the condition of the if statement to determine what could be simplified, but as "consteval if" statements do not have a condition that could be simplified, they can also be skipped here. There may still be more checks that try to look at the conditions of `if`s that aren't included here Fixes #57568 Reviewed By: njames93, aaron.ballman Differential Revision: https://reviews.llvm.org/D133413
- Loading branch information
Showing
5 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ools-extra/test/clang-tidy/checkers/readability/braces-around-statements-consteval-if.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// RUN: clang-tidy %s -checks='-*,readability-braces-around-statements' -- -std=c++2b | count 0 | ||
|
||
constexpr void handle(bool) {} | ||
|
||
constexpr void shouldPass() { | ||
if consteval { | ||
handle(true); | ||
} else { | ||
handle(false); | ||
} | ||
} | ||
|
||
constexpr void shouldPassNegated() { | ||
if !consteval { | ||
handle(false); | ||
} else { | ||
handle(true); | ||
} | ||
} | ||
|
||
constexpr void shouldPassSimple() { | ||
if consteval { | ||
handle(true); | ||
} | ||
} | ||
|
||
void run() { | ||
shouldPass(); | ||
shouldPassNegated(); | ||
shouldPassSimple(); | ||
} |
37 changes: 37 additions & 0 deletions
37
clang-tools-extra/test/clang-tidy/checkers/readability/simplify-bool-expr-cxx23.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// RUN: clang-tidy %s -checks='-*,readability-simplify-boolean-expr' -- -std=c++2b | count 0 | ||
template <bool Cond> | ||
constexpr int testIf() { | ||
if consteval { | ||
if constexpr (Cond) { | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
} else { | ||
return 2; | ||
} | ||
} | ||
|
||
constexpr bool testCompound() { | ||
if consteval { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
constexpr bool testCase(int I) { | ||
switch (I) { | ||
case 0: { | ||
if consteval { | ||
return true; | ||
} | ||
return false; | ||
} | ||
default: { | ||
if consteval { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
} |