Skip to content

Commit abb317f

Browse files
[clang-tidy] Fix performance-unnecessary-value-param (#109145)
This patch essentially reverts #108674 while adding a testcase that triggers a crash in clang-tidy. Fixes #108963.
1 parent 644899a commit abb317f

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %check_clang_tidy -std=c++14-or-later %s performance-unnecessary-value-param %t
2+
3+
// The test case used to crash clang-tidy.
4+
// https://github.com/llvm/llvm-project/issues/108963
5+
6+
struct A
7+
{
8+
template<typename T> A(T&&) {}
9+
};
10+
11+
struct B
12+
{
13+
~B();
14+
};
15+
16+
struct C
17+
{
18+
A a;
19+
C(B, int i) : a(i) {}
20+
// CHECK-MESSAGES: [[@LINE-1]]:6: warning: the parameter #1 is copied for each invocation but only used as a const reference; consider making it a const reference
21+
};
22+
23+
C c(B(), 0);

clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,19 @@ class FunctionParmMutationAnalyzer {
118118
static FunctionParmMutationAnalyzer *
119119
getFunctionParmMutationAnalyzer(const FunctionDecl &Func, ASTContext &Context,
120120
ExprMutationAnalyzer::Memoized &Memorized) {
121-
auto [it, Inserted] = Memorized.FuncParmAnalyzer.try_emplace(&Func);
122-
if (Inserted)
123-
it->second = std::unique_ptr<FunctionParmMutationAnalyzer>(
124-
new FunctionParmMutationAnalyzer(Func, Context, Memorized));
121+
auto it = Memorized.FuncParmAnalyzer.find(&Func);
122+
if (it == Memorized.FuncParmAnalyzer.end()) {
123+
// Creating a new instance of FunctionParmMutationAnalyzer below may add
124+
// additional elements to FuncParmAnalyzer. If we did try_emplace before
125+
// creating a new instance, the returned iterator of try_emplace could be
126+
// invalidated.
127+
it =
128+
Memorized.FuncParmAnalyzer
129+
.try_emplace(&Func, std::unique_ptr<FunctionParmMutationAnalyzer>(
130+
new FunctionParmMutationAnalyzer(
131+
Func, Context, Memorized)))
132+
.first;
133+
}
125134
return it->getSecond().get();
126135
}
127136

0 commit comments

Comments
 (0)