Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] Ignore deleted functions in cppcoreguidelines-rvalue-reference-param-not-moved #69514

Conversation

PiotrZSL
Copy link
Member

Ignore functions and constructors that are maked deleted or defaulted in cppcoreguidelines-rvalue-reference-param-not-moved check.

Fixes #69412

…erence-param-not-moved

Ignore functions and constructors that are maked deleted or defaulted
in cppcoreguidelines-rvalue-reference-param-not-moved check.
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 18, 2023

@llvm/pr-subscribers-clang-tidy

Author: Piotr Zegar (PiotrZSL)

Changes

Ignore functions and constructors that are maked deleted or defaulted in cppcoreguidelines-rvalue-reference-param-not-moved check.

Fixes #69412


Full diff: https://github.com/llvm/llvm-project/pull/69514.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp (+13-12)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+2-1)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved.cpp (+11-2)
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp
index 88b00dc17470f32..7db9e29e8fd0e64 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp
@@ -62,23 +62,28 @@ void RvalueReferenceParamNotMovedCheck::registerMatchers(MatchFinder *Finder) {
               anyOf(isConstQualified(), substTemplateTypeParmType()))))),
           optionally(hasType(qualType(references(templateTypeParmType(
               hasDeclaration(templateTypeParmDecl().bind("template-type"))))))),
-          anyOf(hasAncestor(cxxConstructorDecl(
-                    ToParam, isDefinition(), unless(isMoveConstructor()),
-                    optionally(hasDescendant(MoveCallMatcher)))),
-                hasAncestor(functionDecl(
-                    unless(cxxConstructorDecl()), ToParam,
-                    unless(cxxMethodDecl(isMoveAssignmentOperator())),
-                    hasBody(optionally(hasDescendant(MoveCallMatcher))))))),
+          hasDeclContext(
+              functionDecl(
+                  isDefinition(), unless(isDeleted()), unless(isDefaulted()),
+                  unless(cxxConstructorDecl(isMoveConstructor())),
+                  unless(cxxMethodDecl(isMoveAssignmentOperator())), ToParam,
+                  anyOf(cxxConstructorDecl(
+                            optionally(hasDescendant(MoveCallMatcher))),
+                        functionDecl(unless(cxxConstructorDecl()),
+                                     optionally(hasBody(
+                                         hasDescendant(MoveCallMatcher))))))
+                  .bind("func"))),
       this);
 }
 
 void RvalueReferenceParamNotMovedCheck::check(
     const MatchFinder::MatchResult &Result) {
   const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
+  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("func");
   const auto *TemplateType =
       Result.Nodes.getNodeAs<TemplateTypeParmDecl>("template-type");
 
-  if (!Param)
+  if (!Param || !Function)
     return;
 
   if (IgnoreUnnamedParams && Param->getName().empty())
@@ -87,10 +92,6 @@ void RvalueReferenceParamNotMovedCheck::check(
   if (!Param->isUsed() && Param->hasAttr<UnusedAttr>())
     return;
 
-  const auto *Function = dyn_cast<FunctionDecl>(Param->getDeclContext());
-  if (!Function)
-    return;
-
   if (IgnoreNonDeducedTemplateTypes && TemplateType)
     return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3e1fbe091c9ff6a..6f8d213a722b353 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -246,7 +246,8 @@ Changes in existing checks
 
 - Improved :doc:`cppcoreguidelines-rvalue-reference-param-not-moved
   <clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved>` check
-  to ignore unused parameters when they are marked as unused.
+  to ignore unused parameters when they are marked as unused and parameters of
+  deleted functions and constructors.
 
 - Improved :doc:`llvm-namespace-comment
   <clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved.cpp
index a9b87567a08cc0a..e996690102dff28 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved.cpp
@@ -334,8 +334,7 @@ void instantiate_a_class_template() {
   withObjRef.never_moves(o);
 }
 
-namespace gh68209
-{
+namespace gh68209 {
   void f1([[maybe_unused]] int&& x) {}
 
   void f2(__attribute__((unused)) int&& x) {}
@@ -358,3 +357,13 @@ namespace gh68209
   void f8(__attribute__((unused)) int&& x) { x += 1; }
   // CHECK-MESSAGES: :[[@LINE-1]]:41: warning: rvalue reference parameter 'x' is never moved from inside the function body [cppcoreguidelines-rvalue-reference-param-not-moved]
 } // namespace gh68209
+
+namespace gh69412 {
+  struct S
+  {
+      S(const int&);
+      S(int&&) = delete;
+
+      void foo(int&&) = delete;
+  };
+} // namespace gh69412

@PiotrZSL PiotrZSL merged commit 20d2102 into llvm:main Oct 25, 2023
4 checks passed
@PiotrZSL PiotrZSL deleted the 69412-clang-tidy-false-positive-cppcoreguidelines-rvalue-reference-param-not-moved-when-constructor-is-deleted branch October 25, 2023 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] False positive cppcoreguidelines-rvalue-reference-param-not-moved when constructor is deleted
3 participants