@@ -119,65 +119,72 @@ void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) {
119
119
}
120
120
}
121
121
122
- const size_t Index = llvm::find (Function->parameters (), Param) -
123
- Function->parameters ().begin ();
122
+ handleConstRefFix (*Function, *Param, *Result.Context );
123
+ }
124
+
125
+ void UnnecessaryValueParamCheck::registerPPCallbacks (
126
+ const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
127
+ Inserter.registerPreprocessor (PP);
128
+ }
129
+
130
+ void UnnecessaryValueParamCheck::storeOptions (
131
+ ClangTidyOptions::OptionMap &Opts) {
132
+ Options.store (Opts, " IncludeStyle" , Inserter.getStyle ());
133
+ Options.store (Opts, " AllowedTypes" ,
134
+ utils::options::serializeStringList (AllowedTypes));
135
+ }
136
+
137
+ void UnnecessaryValueParamCheck::onEndOfTranslationUnit () {
138
+ MutationAnalyzerCache.clear ();
139
+ }
140
+
141
+ void UnnecessaryValueParamCheck::handleConstRefFix (const FunctionDecl &Function,
142
+ const ParmVarDecl &Param,
143
+ ASTContext &Context) {
144
+ const size_t Index =
145
+ llvm::find (Function.parameters (), &Param) - Function.parameters ().begin ();
146
+ const bool IsConstQualified =
147
+ Param.getType ().getCanonicalType ().isConstQualified ();
124
148
125
149
auto Diag =
126
- diag (Param-> getLocation (),
150
+ diag (Param. getLocation (),
127
151
" the %select{|const qualified }0parameter %1 is copied for each "
128
152
" invocation%select{ but only used as a const reference|}0; consider "
129
153
" making it a %select{const |}0reference" )
130
- << IsConstQualified << paramNameOrIndex (Param-> getName (), Index);
154
+ << IsConstQualified << paramNameOrIndex (Param. getName (), Index);
131
155
// Do not propose fixes when:
132
156
// 1. the ParmVarDecl is in a macro, since we cannot place them correctly
133
157
// 2. the function is virtual as it might break overrides
134
158
// 3. the function is referenced outside of a call expression within the
135
159
// compilation unit as the signature change could introduce build errors.
136
160
// 4. the function is an explicit template/ specialization.
137
- const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function);
138
- if (Param-> getBeginLoc ().isMacroID () || (Method && Method->isVirtual ()) ||
139
- isReferencedOutsideOfCallExpr (* Function, *Result. Context ) ||
140
- Function-> getTemplateSpecializationKind () == TSK_ExplicitSpecialization)
161
+ const auto *Method = llvm::dyn_cast<CXXMethodDecl>(& Function);
162
+ if (Param. getBeginLoc ().isMacroID () || (Method && Method->isVirtual ()) ||
163
+ isReferencedOutsideOfCallExpr (Function, Context) ||
164
+ Function. getTemplateSpecializationKind () == TSK_ExplicitSpecialization)
141
165
return ;
142
- for (const auto *FunctionDecl = Function; FunctionDecl != nullptr ;
166
+ for (const auto *FunctionDecl = & Function; FunctionDecl != nullptr ;
143
167
FunctionDecl = FunctionDecl->getPreviousDecl ()) {
144
168
const auto &CurrentParam = *FunctionDecl->getParamDecl (Index);
145
- Diag << utils::fixit::changeVarDeclToReference (CurrentParam,
146
- *Result.Context );
169
+ Diag << utils::fixit::changeVarDeclToReference (CurrentParam, Context);
147
170
// The parameter of each declaration needs to be checked individually as to
148
171
// whether it is const or not as constness can differ between definition and
149
172
// declaration.
150
173
if (!CurrentParam.getType ().getCanonicalType ().isConstQualified ()) {
151
174
if (std::optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl (
152
- CurrentParam, *Result. Context , DeclSpec::TQ::TQ_const))
175
+ CurrentParam, Context, DeclSpec::TQ::TQ_const))
153
176
Diag << *Fix;
154
177
}
155
178
}
156
179
}
157
180
158
- void UnnecessaryValueParamCheck::registerPPCallbacks (
159
- const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
160
- Inserter.registerPreprocessor (PP);
161
- }
162
-
163
- void UnnecessaryValueParamCheck::storeOptions (
164
- ClangTidyOptions::OptionMap &Opts) {
165
- Options.store (Opts, " IncludeStyle" , Inserter.getStyle ());
166
- Options.store (Opts, " AllowedTypes" ,
167
- utils::options::serializeStringList (AllowedTypes));
168
- }
169
-
170
- void UnnecessaryValueParamCheck::onEndOfTranslationUnit () {
171
- MutationAnalyzerCache.clear ();
172
- }
173
-
174
- void UnnecessaryValueParamCheck::handleMoveFix (const ParmVarDecl &Var,
181
+ void UnnecessaryValueParamCheck::handleMoveFix (const ParmVarDecl &Param,
175
182
const DeclRefExpr &CopyArgument,
176
- const ASTContext &Context) {
183
+ ASTContext &Context) {
177
184
auto Diag = diag (CopyArgument.getBeginLoc (),
178
185
" parameter %0 is passed by value and only copied once; "
179
186
" consider moving it to avoid unnecessary copies" )
180
- << &Var ;
187
+ << &Param ;
181
188
// Do not propose fixes in macros since we cannot place them correctly.
182
189
if (CopyArgument.getBeginLoc ().isMacroID ())
183
190
return ;
0 commit comments