Skip to content

Commit 2f97761

Browse files
authored
[clang-tidy] Fix param-pack fix-its for 'performance-unnecessary-value-param' check (#164130)
Closes #154755.
1 parent a21521a commit 2f97761

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

clang-tools-extra/clang-tidy/utils/FixItHintUtils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context) {
2121
SourceLocation AmpLocation = Var.getLocation();
2222
auto Token = utils::lexer::getPreviousToken(
2323
AmpLocation, Context.getSourceManager(), Context.getLangOpts());
24+
25+
// For parameter packs the '&' must go before the '...' token
26+
if (Token.is(tok::ellipsis))
27+
return FixItHint::CreateInsertion(Token.getLocation(), "&");
28+
2429
if (!Token.is(tok::unknown))
2530
AmpLocation = Lexer::getLocForEndOfToken(Token.getLocation(), 0,
2631
Context.getSourceManager(),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ Changes in existing checks
407407

408408
- Improved :doc:`performance-unnecessary-value-param
409409
<clang-tidy/checks/performance/unnecessary-value-param>` by printing
410-
the type of the diagnosed variable.
410+
the type of the diagnosed variable and correctly generating fix-it hints for
411+
parameter-pack arguments.
411412

412413
- Improved :doc:`portability-template-virtual-member-function
413414
<clang-tidy/checks/portability/template-virtual-member-function>` check to

clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-templates.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,34 @@ void lambdaNonConstAutoValue() {
9696
};
9797
fn(ExpensiveToCopyType());
9898
}
99+
100+
template <typename... Args>
101+
void ParameterPack(Args... args) {
102+
// CHECK-MESSAGES: [[@LINE-1]]:28: warning: the parameter 'args' of type 'ExpensiveToCopyType'
103+
// CHECK-FIXES: void ParameterPack(const Args&... args) {
104+
}
105+
106+
template <typename... Args>
107+
void ParameterPackConst(Args const... args) {
108+
// CHECK-MESSAGES: [[@LINE-1]]:39: warning: the const qualified parameter 'args' of type 'const ExpensiveToCopyType'
109+
// CHECK-FIXES: void ParameterPackConst(Args const&... args) {
110+
}
111+
112+
template <typename... Args>
113+
void ParameterPackWithParams(const ExpensiveToCopyType E1, ExpensiveToCopyType E2, Args... args) {
114+
// CHECK-MESSAGES: [[@LINE-1]]:56: warning: the const qualified parameter 'E1'
115+
// CHECK-MESSAGES: [[@LINE-2]]:80: warning: the parameter 'E2'
116+
// CHECK-MESSAGES: [[@LINE-3]]:92: warning: the parameter 'args'
117+
// CHECK-FIXES: void ParameterPackWithParams(const ExpensiveToCopyType& E1, const ExpensiveToCopyType& E2, const Args&... args) {
118+
}
119+
120+
template <typename... Args>
121+
void PackWithNonExpensive(int x, Args... args) {}
122+
123+
void instantiatedParameterPack() {
124+
ExpensiveToCopyType E;
125+
ParameterPack(E);
126+
ParameterPackConst(E);
127+
ParameterPackWithParams(E, E, E);
128+
PackWithNonExpensive(5, 5);
129+
}

0 commit comments

Comments
 (0)