Skip to content

Commit 2a114d1

Browse files
authored
[clang-tidy] Ignore if consteval in else-after-return (#91588)
1 parent 37ffbbb commit 2a114d1

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static bool containsDeclInScope(const Stmt *Node) {
113113
}
114114

115115
static void removeElseAndBrackets(DiagnosticBuilder &Diag, ASTContext &Context,
116-
const Stmt *Else, SourceLocation ElseLoc) {
116+
const Stmt *Else, SourceLocation ElseLoc) {
117117
auto Remap = [&](SourceLocation Loc) {
118118
return Context.getSourceManager().getExpansionLoc(Loc);
119119
};
@@ -172,7 +172,7 @@ void ElseAfterReturnCheck::registerMatchers(MatchFinder *Finder) {
172172
breakStmt().bind(InterruptingStr), cxxThrowExpr().bind(InterruptingStr)));
173173
Finder->addMatcher(
174174
compoundStmt(
175-
forEach(ifStmt(unless(isConstexpr()),
175+
forEach(ifStmt(unless(isConstexpr()), unless(isConsteval()),
176176
hasThen(stmt(
177177
anyOf(InterruptsControlFlow,
178178
compoundStmt(has(InterruptsControlFlow))))),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ Changes in existing checks
339339
<clang-tidy/checks/readability/duplicate-include>` check by excluding include
340340
directives that form the filename using macro.
341341

342+
- Improved :doc:`readability-else-after-return
343+
<clang-tidy/checks/readability/else-after-return>` check to ignore
344+
`if consteval` statements, for which the `else` branch must not be removed.
345+
342346
- Improved :doc:`readability-identifier-naming
343347
<clang-tidy/checks/readability/identifier-naming>` check in `GetConfigPerFile`
344348
mode by resolving symbolic links to header files. Fixed handling of Hungarian
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %check_clang_tidy -std=c++23 %s readability-else-after-return %t
2+
3+
// Consteval if is an exception to the rule, we cannot remove the else.
4+
void f() {
5+
if (sizeof(int) > 4) {
6+
return;
7+
} else {
8+
return;
9+
}
10+
// CHECK-MESSAGES: [[@LINE-3]]:5: warning: do not use 'else' after 'return'
11+
12+
if consteval {
13+
return;
14+
} else {
15+
return;
16+
}
17+
}

0 commit comments

Comments
 (0)