Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-tidy] ElseAfterReturn check wont suggest fixes if preprocessor…
… branches are involved Consider this code: ``` if (Cond) { #ifdef X_SUPPORTED X(); #else return; #endif } else { Y(); } Z();``` In this example, if `X_SUPPORTED` is not defined, currently we'll get a warning from the else-after-return check. However If we apply that fix, and then the code is recompiled with `X_SUPPORTED` defined, we have inadvertently changed the behaviour of the if statement due to the else being removed. Code flow when `Cond` is `true` will be: ``` X(); Y(); Z();``` where as before the fix it was: ``` X(); Z();``` This patch adds checks that guard against `#endif` directives appearing between the control flow interrupter and the else and not applying the fix if they are detected. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D91485
- Loading branch information
Showing
4 changed files
with
211 additions
and
17 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
22 changes: 22 additions & 0 deletions
22
clang-tools-extra/test/clang-tidy/checkers/readability-else-after-return-pp-no-crash.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,22 @@ | ||
// RUN: clang-tidy %s -checks=-*,readability-else-after-return | ||
|
||
// We aren't concerned about the output here, just want to ensure clang-tidy doesn't crash. | ||
void foo() { | ||
#if 1 | ||
if (true) { | ||
return; | ||
#else | ||
{ | ||
#endif | ||
} else { | ||
return; | ||
} | ||
|
||
if (true) { | ||
#if 1 | ||
return; | ||
} else { | ||
#endif | ||
return; | ||
} | ||
} |
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