15
15
#include " clang/AST/Decl.h"
16
16
#include " clang/Basic/Diagnostic.h"
17
17
#include < optional>
18
+ #include < utility>
18
19
19
20
namespace clang ::tidy::performance {
20
21
namespace {
@@ -302,6 +303,19 @@ void UnnecessaryCopyInitialization::check(
302
303
}
303
304
}
304
305
306
+ void UnnecessaryCopyInitialization::makeDiagnostic (
307
+ DiagnosticBuilder Diagnostic, const VarDecl &Var, const Stmt &BlockStmt,
308
+ const DeclStmt &Stmt, ASTContext &Context, bool IssueFix) {
309
+ const bool IsVarUnused = isVariableUnused (Var, BlockStmt, Context);
310
+ Diagnostic << &Var << IsVarUnused;
311
+ if (!IssueFix)
312
+ return ;
313
+ if (IsVarUnused)
314
+ recordRemoval (Stmt, Context, Diagnostic);
315
+ else
316
+ recordFixes (Var, Context, Diagnostic);
317
+ }
318
+
305
319
void UnnecessaryCopyInitialization::handleCopyFromMethodReturn (
306
320
const VarDecl &Var, const Stmt &BlockStmt, const DeclStmt &Stmt,
307
321
bool IssueFix, const VarDecl *ObjectArg, ASTContext &Context) {
@@ -312,52 +326,33 @@ void UnnecessaryCopyInitialization::handleCopyFromMethodReturn(
312
326
!isInitializingVariableImmutable (*ObjectArg, BlockStmt, Context,
313
327
ExcludedContainerTypes))
314
328
return ;
315
- if (isVariableUnused (Var, BlockStmt, Context)) {
316
- auto Diagnostic =
317
- diag (Var.getLocation (),
318
- " the %select{|const qualified }0variable %1 is copy-constructed "
319
- " from a const reference but is never used; consider "
320
- " removing the statement" )
321
- << IsConstQualified << &Var;
322
- if (IssueFix)
323
- recordRemoval (Stmt, Context, Diagnostic);
324
- } else {
325
- auto Diagnostic =
326
- diag (Var.getLocation (),
327
- " the %select{|const qualified }0variable %1 is copy-constructed "
328
- " from a const reference%select{ but is only used as const "
329
- " reference|}0; consider making it a const reference" )
330
- << IsConstQualified << &Var;
331
- if (IssueFix)
332
- recordFixes (Var, Context, Diagnostic);
333
- }
329
+
330
+ auto Diagnostic =
331
+ diag (Var.getLocation (),
332
+ " the %select{|const qualified }0variable %1 is copy-constructed "
333
+ " from a const reference%select{"
334
+ " %select{ but is only used as const reference|}0"
335
+ " | but is never used}2; consider "
336
+ " %select{making it a const reference|removing the statement}2" )
337
+ << IsConstQualified;
338
+ makeDiagnostic (std::move (Diagnostic), Var, BlockStmt, Stmt, Context,
339
+ IssueFix);
334
340
}
335
341
336
342
void UnnecessaryCopyInitialization::handleCopyFromLocalVar (
337
- const VarDecl &NewVar , const VarDecl &OldVar, const Stmt &BlockStmt,
343
+ const VarDecl &Var , const VarDecl &OldVar, const Stmt &BlockStmt,
338
344
const DeclStmt &Stmt, bool IssueFix, ASTContext &Context) {
339
- if (!isOnlyUsedAsConst (NewVar , BlockStmt, Context) ||
345
+ if (!isOnlyUsedAsConst (Var , BlockStmt, Context) ||
340
346
!isInitializingVariableImmutable (OldVar, BlockStmt, Context,
341
347
ExcludedContainerTypes))
342
348
return ;
343
-
344
- if (isVariableUnused (NewVar, BlockStmt, Context)) {
345
- auto Diagnostic = diag (NewVar.getLocation (),
346
- " local copy %0 of the variable %1 is never modified "
347
- " and never used; "
348
- " consider removing the statement" )
349
- << &NewVar << &OldVar;
350
- if (IssueFix)
351
- recordRemoval (Stmt, Context, Diagnostic);
352
- } else {
353
- auto Diagnostic =
354
- diag (NewVar.getLocation (),
355
- " local copy %0 of the variable %1 is never modified; "
356
- " consider avoiding the copy" )
357
- << &NewVar << &OldVar;
358
- if (IssueFix)
359
- recordFixes (NewVar, Context, Diagnostic);
360
- }
349
+ auto Diagnostic = diag (Var.getLocation (),
350
+ " local copy %1 of the variable %0 is never modified"
351
+ " %select{| and never used}2; consider "
352
+ " %select{avoiding the copy|removing the statement}2" )
353
+ << &OldVar;
354
+ makeDiagnostic (std::move (Diagnostic), Var, BlockStmt, Stmt, Context,
355
+ IssueFix);
361
356
}
362
357
363
358
void UnnecessaryCopyInitialization::storeOptions (
0 commit comments