[clang-tools-extra] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions (PR #119974)
https://github.com/pobrn updated https://github.com/llvm/llvm-project/pull/119974 From fcacca26be4350d005c08a781bf4cee29abb404e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Sat, 14 Dec 2024 16:57:57 +0100 Subject: [PATCH] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions Static member functions can be considered the same way as free functions are, so do that. --- .../UnnecessaryCopyInitialization.cpp | 6 +++-- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../unnecessary-copy-initialization.cpp | 24 +++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp index 034894c11bf2c0..f63be0d33bf0a8 100644 --- a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp +++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp @@ -104,6 +104,8 @@ AST_MATCHER_FUNCTION_P(StatementMatcher, hasArgument(0, hasType(ReceiverType); } +AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); } + AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) { // Only allow initialization of a const reference from a free function if it // has no arguments. Otherwise it could return an alias to one of its @@ -111,7 +113,7 @@ AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) { return callExpr(callee(functionDecl(returns(hasCanonicalType( matchers::isReferenceToConst( .bind(FunctionDeclId)), - argumentCountIs(0), unless(callee(cxxMethodDecl( + argumentCountIs(0), unless(callee(cxxMethodDecl(unless(isStatic()) .bind(InitFunctionCallId); } @@ -232,7 +234,7 @@ UnnecessaryCopyInitialization::UnnecessaryCopyInitialization( Options.get("ExcludedContainerTypes", ""))) {} void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) { - auto LocalVarCopiedFrom = [this](const internal::Matcher &CopyCtorArg) { + auto LocalVarCopiedFrom = [this](const ast_matchers::internal::Matcher &CopyCtorArg) { return compoundStmt( forEachDescendant( declStmt( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6803842106791b..ca60cfacc55e3d 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -332,6 +332,10 @@ Changes in existing checks ` check to validate ``namespace`` aliases. +- Improved :doc:`performance-unnecessary-copy-initialization` + check + to consider static member functions the same way as free functions. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp index d02bb98cf583cb..b5325776f54c61 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp @@ -28,6 +28,8 @@ struct ExpensiveToCopyType { template const A &templatedAccessor() const; operator int() const; // Implicit conversion to int. + + static const ExpensiveToCopyType &instance(); }; template @@ -100,6 +102,28 @@ void PositiveFunctionCall() { VarCopyConstructed.constMethod(); } +void PositiveStaticMethodCall() { + const auto AutoAssigned = ExpensiveToCopyType::instance(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' is copy-constructed from a const reference; consider making it a const reference [performance-unnecessary-copy-initialization] + // CHECK-FIXES: const auto& AutoAssigned = ExpensiveToCopyType::instance(); + AutoAssigned.constMethod(); + + const auto AutoCopyConstructed(ExpensiveToCopyType::instance()); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed' + // CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveToCopyType::instance()); + AutoCopyConstructed.constMethod(); + + const ExpensiveToCopyType VarAssigned = ExpensiveToCopyType::instance(); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned' + // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = ExpensiveToCopyType::instance(); + VarAssigned.constMethod(); + + const ExpensiveToCopyType VarCopyConstructed(ExpensiveToCopyType::instance()); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed' + // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(ExpensiveT
[clang-tools-extra] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions (PR #119974)
https://github.com/pobrn updated https://github.com/llvm/llvm-project/pull/119974 From e9cd529ef486df9b5279aef974f136c63cb367ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Sat, 14 Dec 2024 16:57:57 +0100 Subject: [PATCH] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions Static member functions can be considered the same way as free functions are, so do that. --- .../UnnecessaryCopyInitialization.cpp | 6 +++-- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../unnecessary-copy-initialization.cpp | 24 +++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp index 034894c11bf2c0..f63be0d33bf0a8 100644 --- a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp +++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp @@ -104,6 +104,8 @@ AST_MATCHER_FUNCTION_P(StatementMatcher, hasArgument(0, hasType(ReceiverType); } +AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); } + AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) { // Only allow initialization of a const reference from a free function if it // has no arguments. Otherwise it could return an alias to one of its @@ -111,7 +113,7 @@ AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) { return callExpr(callee(functionDecl(returns(hasCanonicalType( matchers::isReferenceToConst( .bind(FunctionDeclId)), - argumentCountIs(0), unless(callee(cxxMethodDecl( + argumentCountIs(0), unless(callee(cxxMethodDecl(unless(isStatic()) .bind(InitFunctionCallId); } @@ -232,7 +234,7 @@ UnnecessaryCopyInitialization::UnnecessaryCopyInitialization( Options.get("ExcludedContainerTypes", ""))) {} void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) { - auto LocalVarCopiedFrom = [this](const internal::Matcher &CopyCtorArg) { + auto LocalVarCopiedFrom = [this](const ast_matchers::internal::Matcher &CopyCtorArg) { return compoundStmt( forEachDescendant( declStmt( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6803842106791b..d26a6c1a8061d6 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -307,6 +307,10 @@ Changes in existing checks ` check to fix a crash when an argument type is declared but not defined. +- Improved :doc:`performance-unnecessary-copy-initialization` + check + to consider static member functions the same way as free functions. + - Improved :doc:`readability-container-contains ` check to let it work on any class that has a ``contains`` method. Fix some false negatives in the diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp index d02bb98cf583cb..b5325776f54c61 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp @@ -28,6 +28,8 @@ struct ExpensiveToCopyType { template const A &templatedAccessor() const; operator int() const; // Implicit conversion to int. + + static const ExpensiveToCopyType &instance(); }; template @@ -100,6 +102,28 @@ void PositiveFunctionCall() { VarCopyConstructed.constMethod(); } +void PositiveStaticMethodCall() { + const auto AutoAssigned = ExpensiveToCopyType::instance(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' is copy-constructed from a const reference; consider making it a const reference [performance-unnecessary-copy-initialization] + // CHECK-FIXES: const auto& AutoAssigned = ExpensiveToCopyType::instance(); + AutoAssigned.constMethod(); + + const auto AutoCopyConstructed(ExpensiveToCopyType::instance()); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed' + // CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveToCopyType::instance()); + AutoCopyConstructed.constMethod(); + + const ExpensiveToCopyType VarAssigned = ExpensiveToCopyType::instance(); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned' + // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = ExpensiveToCopyType::instance(); + VarAssigned.constMethod(); + + const ExpensiveToCopyType VarCopyConstructed(ExpensiveToCopyType::instance()); + // CHECK-MESSAGES:
[clang-tools-extra] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions (PR #119974)
pobrn wrote: > Please mention changes in Release Notes. Sorry, I hope it is fixed now. https://github.com/llvm/llvm-project/pull/119974 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions (PR #119974)
https://github.com/pobrn updated https://github.com/llvm/llvm-project/pull/119974 From ee2b0c2dc334f443fc90c6638a44ab508eb66bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Sat, 14 Dec 2024 16:57:57 +0100 Subject: [PATCH] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions Static member functions can be considered the same way as free functions are, so do that. --- .../UnnecessaryCopyInitialization.cpp | 21 +--- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../unnecessary-copy-initialization.cpp | 24 +++ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp index 034894c11bf2c0..dc2e8a38a3e97a 100644 --- a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp +++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp @@ -104,15 +104,18 @@ AST_MATCHER_FUNCTION_P(StatementMatcher, hasArgument(0, hasType(ReceiverType); } +AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); } + AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) { - // Only allow initialization of a const reference from a free function if it - // has no arguments. Otherwise it could return an alias to one of its - // arguments and the arguments need to be checked for const use as well. - return callExpr(callee(functionDecl(returns(hasCanonicalType( - matchers::isReferenceToConst( - .bind(FunctionDeclId)), - argumentCountIs(0), unless(callee(cxxMethodDecl( - .bind(InitFunctionCallId); + // Only allow initialization of a const reference from a free function or + // static member function if it has no arguments. Otherwise it could return + // an alias to one of its arguments and the arguments need to be checked + // for const use as well. + return callExpr(argumentCountIs(0), + callee(functionDecl(returns(hasCanonicalType(matchers::isReferenceToConst())), + unless(cxxMethodDecl(unless(isStatic() + .bind(FunctionDeclId))) + .bind(InitFunctionCallId); } AST_MATCHER_FUNCTION_P(StatementMatcher, initializerReturnsReferenceToConst, @@ -232,7 +235,7 @@ UnnecessaryCopyInitialization::UnnecessaryCopyInitialization( Options.get("ExcludedContainerTypes", ""))) {} void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) { - auto LocalVarCopiedFrom = [this](const internal::Matcher &CopyCtorArg) { + auto LocalVarCopiedFrom = [this](const ast_matchers::internal::Matcher &CopyCtorArg) { return compoundStmt( forEachDescendant( declStmt( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6803842106791b..d26a6c1a8061d6 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -307,6 +307,10 @@ Changes in existing checks ` check to fix a crash when an argument type is declared but not defined. +- Improved :doc:`performance-unnecessary-copy-initialization` + check + to consider static member functions the same way as free functions. + - Improved :doc:`readability-container-contains ` check to let it work on any class that has a ``contains`` method. Fix some false negatives in the diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp index d02bb98cf583cb..b5325776f54c61 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp @@ -28,6 +28,8 @@ struct ExpensiveToCopyType { template const A &templatedAccessor() const; operator int() const; // Implicit conversion to int. + + static const ExpensiveToCopyType &instance(); }; template @@ -100,6 +102,28 @@ void PositiveFunctionCall() { VarCopyConstructed.constMethod(); } +void PositiveStaticMethodCall() { + const auto AutoAssigned = ExpensiveToCopyType::instance(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' is copy-constructed from a const reference; consider making it a const reference [performance-unnecessary-copy-initialization] + // CHECK-FIXES: const auto& AutoAssigned = ExpensiveToCopyType::instance(); + AutoAssigned.constMethod(); + + const auto AutoCopyConstructed(ExpensiveToCopyType::instance()); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable
[clang-tools-extra] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions (PR #119974)
https://github.com/pobrn created https://github.com/llvm/llvm-project/pull/119974 Static member functions can be considered the same way as free functions are, so do that. --- 1. Not sure how many more tests I should add, since this uses the same code paths as free functions. 2. This is the 5th instance of the `isStatic` matcher in `clang-tidy`. From 6a160c92bd1210e1ec340d2d14650cc0c9d91459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Sat, 14 Dec 2024 16:57:57 +0100 Subject: [PATCH] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions Static member functions can be considered the same way as free functions are, so do that. --- .../UnnecessaryCopyInitialization.cpp | 6 - .../unnecessary-copy-initialization.cpp | 24 +++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp index 034894c11bf2c0..778d1d00250315 100644 --- a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp +++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp @@ -104,6 +104,10 @@ AST_MATCHER_FUNCTION_P(StatementMatcher, hasArgument(0, hasType(ReceiverType); } +namespace { +AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); } +} // namespace + AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) { // Only allow initialization of a const reference from a free function if it // has no arguments. Otherwise it could return an alias to one of its @@ -111,7 +115,7 @@ AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) { return callExpr(callee(functionDecl(returns(hasCanonicalType( matchers::isReferenceToConst( .bind(FunctionDeclId)), - argumentCountIs(0), unless(callee(cxxMethodDecl( + argumentCountIs(0), unless(callee(cxxMethodDecl(unless(isStatic()) .bind(InitFunctionCallId); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp index d02bb98cf583cb..b5325776f54c61 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp @@ -28,6 +28,8 @@ struct ExpensiveToCopyType { template const A &templatedAccessor() const; operator int() const; // Implicit conversion to int. + + static const ExpensiveToCopyType &instance(); }; template @@ -100,6 +102,28 @@ void PositiveFunctionCall() { VarCopyConstructed.constMethod(); } +void PositiveStaticMethodCall() { + const auto AutoAssigned = ExpensiveToCopyType::instance(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' is copy-constructed from a const reference; consider making it a const reference [performance-unnecessary-copy-initialization] + // CHECK-FIXES: const auto& AutoAssigned = ExpensiveToCopyType::instance(); + AutoAssigned.constMethod(); + + const auto AutoCopyConstructed(ExpensiveToCopyType::instance()); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed' + // CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveToCopyType::instance()); + AutoCopyConstructed.constMethod(); + + const ExpensiveToCopyType VarAssigned = ExpensiveToCopyType::instance(); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned' + // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = ExpensiveToCopyType::instance(); + VarAssigned.constMethod(); + + const ExpensiveToCopyType VarCopyConstructed(ExpensiveToCopyType::instance()); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed' + // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(ExpensiveToCopyType::instance()); + VarCopyConstructed.constMethod(); +} + void PositiveMethodCallConstReferenceParam(const ExpensiveToCopyType &Obj) { const auto AutoAssigned = Obj.reference(); // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions (PR #119974)
https://github.com/pobrn updated https://github.com/llvm/llvm-project/pull/119974 From 2d72c484291862fd8c9d8cea1a3ebadbe37343be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Sat, 14 Dec 2024 16:57:57 +0100 Subject: [PATCH] [clang-tidy] performance-unnecessary-copy-initialization: Consider static functions Static member functions can be considered the same way as free functions are, so do that. --- .../UnnecessaryCopyInitialization.cpp | 6 +++-- .../unnecessary-copy-initialization.cpp | 24 +++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp index 034894c11bf2c0..f63be0d33bf0a8 100644 --- a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp +++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp @@ -104,6 +104,8 @@ AST_MATCHER_FUNCTION_P(StatementMatcher, hasArgument(0, hasType(ReceiverType); } +AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); } + AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) { // Only allow initialization of a const reference from a free function if it // has no arguments. Otherwise it could return an alias to one of its @@ -111,7 +113,7 @@ AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) { return callExpr(callee(functionDecl(returns(hasCanonicalType( matchers::isReferenceToConst( .bind(FunctionDeclId)), - argumentCountIs(0), unless(callee(cxxMethodDecl( + argumentCountIs(0), unless(callee(cxxMethodDecl(unless(isStatic()) .bind(InitFunctionCallId); } @@ -232,7 +234,7 @@ UnnecessaryCopyInitialization::UnnecessaryCopyInitialization( Options.get("ExcludedContainerTypes", ""))) {} void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) { - auto LocalVarCopiedFrom = [this](const internal::Matcher &CopyCtorArg) { + auto LocalVarCopiedFrom = [this](const ast_matchers::internal::Matcher &CopyCtorArg) { return compoundStmt( forEachDescendant( declStmt( diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp index d02bb98cf583cb..b5325776f54c61 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp @@ -28,6 +28,8 @@ struct ExpensiveToCopyType { template const A &templatedAccessor() const; operator int() const; // Implicit conversion to int. + + static const ExpensiveToCopyType &instance(); }; template @@ -100,6 +102,28 @@ void PositiveFunctionCall() { VarCopyConstructed.constMethod(); } +void PositiveStaticMethodCall() { + const auto AutoAssigned = ExpensiveToCopyType::instance(); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' is copy-constructed from a const reference; consider making it a const reference [performance-unnecessary-copy-initialization] + // CHECK-FIXES: const auto& AutoAssigned = ExpensiveToCopyType::instance(); + AutoAssigned.constMethod(); + + const auto AutoCopyConstructed(ExpensiveToCopyType::instance()); + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed' + // CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveToCopyType::instance()); + AutoCopyConstructed.constMethod(); + + const ExpensiveToCopyType VarAssigned = ExpensiveToCopyType::instance(); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned' + // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = ExpensiveToCopyType::instance(); + VarAssigned.constMethod(); + + const ExpensiveToCopyType VarCopyConstructed(ExpensiveToCopyType::instance()); + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed' + // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(ExpensiveToCopyType::instance()); + VarCopyConstructed.constMethod(); +} + void PositiveMethodCallConstReferenceParam(const ExpensiveToCopyType &Obj) { const auto AutoAssigned = Obj.reference(); // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits