Skip to content

Commit b1d7985

Browse files
schenkeryuxuanchen1997
authored andcommitted
[clang-tidy] fix misc-const-correctness to work with function-try-blocks (#99925)
Make the clang-tidy check misc-const-correctness work with function-try-blocks. Fixes #99860.
1 parent 091de2f commit b1d7985

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
9393
// shall be run.
9494
const auto FunctionScope =
9595
functionDecl(
96-
hasBody(
97-
compoundStmt(forEachDescendant(
98-
declStmt(containsAnyDeclaration(
99-
LocalValDecl.bind("local-value")),
100-
unless(has(decompositionDecl())))
101-
.bind("decl-stmt")))
102-
.bind("scope")))
96+
hasBody(stmt(forEachDescendant(
97+
declStmt(containsAnyDeclaration(
98+
LocalValDecl.bind("local-value")),
99+
unless(has(decompositionDecl())))
100+
.bind("decl-stmt")))
101+
.bind("scope")))
103102
.bind("function-decl");
104103

105104
Finder->addMatcher(FunctionScope, this);
@@ -109,7 +108,7 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
109108
enum class VariableCategory { Value, Reference, Pointer };
110109

111110
void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
112-
const auto *LocalScope = Result.Nodes.getNodeAs<CompoundStmt>("scope");
111+
const auto *LocalScope = Result.Nodes.getNodeAs<Stmt>("scope");
113112
const auto *Variable = Result.Nodes.getNodeAs<VarDecl>("local-value");
114113
const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function-decl");
115114

@@ -198,7 +197,7 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
198197
}
199198
}
200199

201-
void ConstCorrectnessCheck::registerScope(const CompoundStmt *LocalScope,
200+
void ConstCorrectnessCheck::registerScope(const Stmt *LocalScope,
202201
ASTContext *Context) {
203202
auto &Analyzer = ScopesCache[LocalScope];
204203
if (!Analyzer)

clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class ConstCorrectnessCheck : public ClangTidyCheck {
3232
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
3333

3434
private:
35-
void registerScope(const CompoundStmt *LocalScope, ASTContext *Context);
35+
void registerScope(const Stmt *LocalScope, ASTContext *Context);
3636

3737
using MutationAnalyzer = std::unique_ptr<ExprMutationAnalyzer>;
38-
llvm::DenseMap<const CompoundStmt *, MutationAnalyzer> ScopesCache;
38+
llvm::DenseMap<const Stmt *, MutationAnalyzer> ScopesCache;
3939
llvm::DenseSet<SourceLocation> TemplateDiagnosticsCache;
4040

4141
const bool AnalyzeValues;

clang-tools-extra/docs/ReleaseNotes.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ Changes in existing checks
381381
- Improved :doc:`misc-const-correctness
382382
<clang-tidy/checks/misc/const-correctness>` check by avoiding infinite recursion
383383
for recursive functions with forwarding reference parameters and reference
384-
variables which refer to themselves.
384+
variables which refer to themselves. Also adapted the check to work with
385+
function-try-blocks.
385386

386387
- Improved :doc:`misc-definitions-in-headers
387388
<clang-tidy/checks/misc/definitions-in-headers>` check by replacing the local

clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ void some_function(double np_arg0, wchar_t np_arg1) {
5656
np_local6--;
5757
}
5858

59+
int function_try_block() try {
60+
int p_local0 = 0;
61+
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int' can be declared 'const'
62+
// CHECK-FIXES: int const p_local0
63+
return p_local0;
64+
} catch (...) {
65+
return 0;
66+
}
67+
5968
void nested_scopes() {
6069
int p_local0 = 2;
6170
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 'int' can be declared 'const'

0 commit comments

Comments
 (0)