Skip to content

Commit 20d2102

Browse files
authored
[clang-tidy] Ignore deleted functions in cppcoreguidelines-rvalue-reference-param-not-moved (#69514)
Ignore functions and constructors that are maked deleted or defaulted in cppcoreguidelines-rvalue-reference-param-not-moved check. Fixes #69412
1 parent 94aaaf4 commit 20d2102

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/RvalueReferenceParamNotMovedCheck.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,28 @@ void RvalueReferenceParamNotMovedCheck::registerMatchers(MatchFinder *Finder) {
6262
anyOf(isConstQualified(), substTemplateTypeParmType()))))),
6363
optionally(hasType(qualType(references(templateTypeParmType(
6464
hasDeclaration(templateTypeParmDecl().bind("template-type"))))))),
65-
anyOf(hasAncestor(cxxConstructorDecl(
66-
ToParam, isDefinition(), unless(isMoveConstructor()),
67-
optionally(hasDescendant(MoveCallMatcher)))),
68-
hasAncestor(functionDecl(
69-
unless(cxxConstructorDecl()), ToParam,
70-
unless(cxxMethodDecl(isMoveAssignmentOperator())),
71-
hasBody(optionally(hasDescendant(MoveCallMatcher))))))),
65+
hasDeclContext(
66+
functionDecl(
67+
isDefinition(), unless(isDeleted()), unless(isDefaulted()),
68+
unless(cxxConstructorDecl(isMoveConstructor())),
69+
unless(cxxMethodDecl(isMoveAssignmentOperator())), ToParam,
70+
anyOf(cxxConstructorDecl(
71+
optionally(hasDescendant(MoveCallMatcher))),
72+
functionDecl(unless(cxxConstructorDecl()),
73+
optionally(hasBody(
74+
hasDescendant(MoveCallMatcher))))))
75+
.bind("func"))),
7276
this);
7377
}
7478

7579
void RvalueReferenceParamNotMovedCheck::check(
7680
const MatchFinder::MatchResult &Result) {
7781
const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
82+
const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("func");
7883
const auto *TemplateType =
7984
Result.Nodes.getNodeAs<TemplateTypeParmDecl>("template-type");
8085

81-
if (!Param)
86+
if (!Param || !Function)
8287
return;
8388

8489
if (IgnoreUnnamedParams && Param->getName().empty())
@@ -87,10 +92,6 @@ void RvalueReferenceParamNotMovedCheck::check(
8792
if (!Param->isUsed() && Param->hasAttr<UnusedAttr>())
8893
return;
8994

90-
const auto *Function = dyn_cast<FunctionDecl>(Param->getDeclContext());
91-
if (!Function)
92-
return;
93-
9495
if (IgnoreNonDeducedTemplateTypes && TemplateType)
9596
return;
9697

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ Changes in existing checks
260260

261261
- Improved :doc:`cppcoreguidelines-rvalue-reference-param-not-moved
262262
<clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved>` check
263-
to ignore unused parameters when they are marked as unused.
263+
to ignore unused parameters when they are marked as unused and parameters of
264+
deleted functions and constructors.
264265

265266
- Improved :doc:`llvm-namespace-comment
266267
<clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/rvalue-reference-param-not-moved.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,7 @@ void instantiate_a_class_template() {
334334
withObjRef.never_moves(o);
335335
}
336336

337-
namespace gh68209
338-
{
337+
namespace gh68209 {
339338
void f1([[maybe_unused]] int&& x) {}
340339

341340
void f2(__attribute__((unused)) int&& x) {}
@@ -358,3 +357,13 @@ namespace gh68209
358357
void f8(__attribute__((unused)) int&& x) { x += 1; }
359358
// CHECK-MESSAGES: :[[@LINE-1]]:41: warning: rvalue reference parameter 'x' is never moved from inside the function body [cppcoreguidelines-rvalue-reference-param-not-moved]
360359
} // namespace gh68209
360+
361+
namespace gh69412 {
362+
struct S
363+
{
364+
S(const int&);
365+
S(int&&) = delete;
366+
367+
void foo(int&&) = delete;
368+
};
369+
} // namespace gh69412

0 commit comments

Comments
 (0)