Skip to content

[clang-tidy] Fix performance-unnecessary-value-param #109145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %check_clang_tidy -std=c++14-or-later %s performance-unnecessary-value-param %t

// The test case used to crash clang-tidy.
// https://github.com/llvm/llvm-project/issues/108963

struct A
{
template<typename T> A(T&&) {}
};

struct B
{
~B();
};

struct C
{
A a;
C(B, int i) : a(i) {}
// 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
};

C c(B(), 0);
17 changes: 13 additions & 4 deletions clang/include/clang/Analysis/Analyses/ExprMutationAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,19 @@ class FunctionParmMutationAnalyzer {
static FunctionParmMutationAnalyzer *
getFunctionParmMutationAnalyzer(const FunctionDecl &Func, ASTContext &Context,
ExprMutationAnalyzer::Memoized &Memorized) {
auto [it, Inserted] = Memorized.FuncParmAnalyzer.try_emplace(&Func);
if (Inserted)
it->second = std::unique_ptr<FunctionParmMutationAnalyzer>(
new FunctionParmMutationAnalyzer(Func, Context, Memorized));
auto it = Memorized.FuncParmAnalyzer.find(&Func);
if (it == Memorized.FuncParmAnalyzer.end()) {
// Creating a new instance of FunctionParmMutationAnalyzer below may add
// additional elements to FuncParmAnalyzer. If we did try_emplace before
// creating a new instance, the returned iterator of try_emplace could be
// invalidated.
it =
Memorized.FuncParmAnalyzer
.try_emplace(&Func, std::unique_ptr<FunctionParmMutationAnalyzer>(
new FunctionParmMutationAnalyzer(
Func, Context, Memorized)))
.first;
}
return it->getSecond().get();
}

Expand Down
Loading