Skip to content

Commit bb6a98c

Browse files
authored
[clang-tidy] Ignore unused parameters in rvalue-reference-param-not-moved check (#69045)
With this patch we no longer issue a warning for unused parameters which are marked as such. This fixes #68209
1 parent 0603737 commit bb6a98c

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ void RvalueReferenceParamNotMovedCheck::check(
8484
if (IgnoreUnnamedParams && Param->getName().empty())
8585
return;
8686

87+
if (!Param->isUsed() && Param->hasAttr<UnusedAttr>())
88+
return;
89+
8790
const auto *Function = dyn_cast<FunctionDecl>(Param->getDeclContext());
8891
if (!Function)
8992
return;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ Changes in existing checks
237237
<clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check to ignore
238238
false-positives in unevaluated context (e.g., ``decltype``, ``sizeof``, ...).
239239

240+
- Improved :doc:`cppcoreguidelines-rvalue-reference-param-not-moved
241+
<clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved>` check
242+
to ignore unused parameters when they are marked as unused.
243+
240244
- Improved :doc:`llvm-namespace-comment
241245
<clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for
242246
``inline`` namespaces in the same format as :program:`clang-format`.

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ Example:
1818
std::string Copy(Input); // Oops - forgot to std::move
1919
}
2020

21+
Note that parameters that are unused and marked as such will not be diagnosed.
22+
23+
Example:
24+
25+
.. code-block:: c++
26+
27+
void conditional_use([[maybe_unused]] std::string&& Input) {
28+
// No diagnostic here since Input is unused and marked as such
29+
}
30+
2131
Options
2232
-------
2333

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,28 @@ void instantiate_a_class_template() {
333333
AClassTemplate<Obj&> withObjRef(o);
334334
withObjRef.never_moves(o);
335335
}
336+
337+
namespace gh68209
338+
{
339+
void f1([[maybe_unused]] int&& x) {}
340+
341+
void f2(__attribute__((unused)) int&& x) {}
342+
343+
void f3(int&& x) {}
344+
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: rvalue reference parameter 'x' is never moved from inside the function body [cppcoreguidelines-rvalue-reference-param-not-moved]
345+
346+
template <typename T>
347+
void f4([[maybe_unused]] T&& x) {}
348+
349+
template <typename T>
350+
void f5(__attribute((unused)) T&& x) {}
351+
352+
template<typename T>
353+
void f6(T&& x) {}
354+
355+
void f7([[maybe_unused]] int&& x) { x += 1; }
356+
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: rvalue reference parameter 'x' is never moved from inside the function body [cppcoreguidelines-rvalue-reference-param-not-moved]
357+
358+
void f8(__attribute__((unused)) int&& x) { x += 1; }
359+
// CHECK-MESSAGES: :[[@LINE-1]]:41: warning: rvalue reference parameter 'x' is never moved from inside the function body [cppcoreguidelines-rvalue-reference-param-not-moved]
360+
} // namespace gh68209

0 commit comments

Comments
 (0)