[clang-tools-extra] r274552 - [clang-tidy] UnnecessaryValueParamCheck - only warn for virtual methods

Felix Berger via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 5 07:40:44 PDT 2016


Author: flx
Date: Tue Jul  5 09:40:44 2016
New Revision: 274552

URL: http://llvm.org/viewvc/llvm-project?rev=274552&view=rev
Log:
[clang-tidy] UnnecessaryValueParamCheck - only warn for virtual methods

Summary:

As changing virtual methods could break method overrides disable applying the fix and just warn.

Reviewers: alexfh, sbenza

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D21936

Modified:
    clang-tools-extra/trunk/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp

Modified: clang-tools-extra/trunk/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/performance/UnnecessaryValueParamCheck.cpp?rev=274552&r1=274551&r2=274552&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/performance/UnnecessaryValueParamCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/performance/UnnecessaryValueParamCheck.cpp Tue Jul  5 09:40:44 2016
@@ -118,8 +118,10 @@ void UnnecessaryValueParamCheck::check(c
                               "invocation but only used as a const reference; "
                               "consider making it a const reference")
       << paramNameOrIndex(Param->getName(), Index);
-  // Do not propose fixes in macros since we cannot place them correctly.
-  if (Param->getLocStart().isMacroID())
+  // Do not propose fixes in macros since we cannot place them correctly, or if
+  // function is virtual as it might break overrides.
+  const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function);
+  if (Param->getLocStart().isMacroID() || (Method && Method->isVirtual()))
     return;
   for (const auto *FunctionDecl = Function; FunctionDecl != nullptr;
        FunctionDecl = FunctionDecl->getPreviousDecl()) {

Modified: clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp?rev=274552&r1=274551&r2=274552&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp Tue Jul  5 09:40:44 2016
@@ -182,6 +182,19 @@ struct NegativeOverriddenMethod : public
   }
 };
 
+struct VirtualMethodWarningOnly {
+  virtual void methodWithExpensiveValueParam(ExpensiveToCopyType T) {}
+  // CHECK-MESSAGES: [[@LINE-1]]:66: warning: the parameter 'T' is copied
+  // CHECK-FIXES: virtual void methodWithExpensiveValueParam(ExpensiveToCopyType T) {}
+  virtual ~VirtualMethodWarningOnly() {}
+};
+
+struct PositiveNonVirualMethod {
+  void method(const ExpensiveToCopyType T) {}
+  // CHECK-MESSAGES: [[@LINE-1]]:41: warning: the const qualified parameter 'T' is copied
+  // CHECK-FIXES: void method(const ExpensiveToCopyType& T) {}
+};
+
 struct NegativeDeletedMethod {
   ~NegativeDeletedMethod() {}
   NegativeDeletedMethod& operator=(NegativeDeletedMethod N) = delete;




More information about the cfe-commits mailing list