https://github.com/davidmenggx created 
https://github.com/llvm/llvm-project/pull/244978

The check no longer treats variables marked [[maybe_unused]] as unused. It 
suggests making them a const reference instead.

Fixes #244404.

>From a97e2577953bef0eec3951eef808217b663366f2 Mon Sep 17 00:00:00 2001
From: David Meng <[email protected]>
Date: Sat, 21 Nov 2026 17:43:14 -0800
Subject: [PATCH] [clang-tidy] Don't suggest removing [[maybe_unused]]
 variables in performance-unnecessary-copy-initialization

The check no longer treats variables marked [[maybe_unused]] as unused.
It suggests making them a const reference instead.

Fixes #244404.
---
 .../UnnecessaryCopyInitializationCheck.cpp        |  5 ++++-
 clang-tools-extra/docs/ReleaseNotes.md            |  5 +++++
 .../unnecessary-copy-initialization.cpp           | 15 +++++++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git 
a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitializationCheck.cpp
 
b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitializationCheck.cpp
index c8372a75fb4877..4dfb7be615f805 100644
--- 
a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitializationCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitializationCheck.cpp
@@ -182,7 +182,10 @@ static bool isInitializingVariableImmutable(
 
 static bool isVariableUnused(const VarDecl &Var, const Stmt &BlockStmt,
                              ASTContext &Context) {
-  return allDeclRefExprs(Var, BlockStmt, Context).empty();
+  // A variable marked `[[maybe_unused]]` should not be treated as unused.
+  // Suggest a const reference instead.
+  return !Var.hasAttr<UnusedAttr>() &&
+         allDeclRefExprs(Var, BlockStmt, Context).empty();
 }
 
 static const SubstTemplateTypeParmType *
diff --git a/clang-tools-extra/docs/ReleaseNotes.md 
b/clang-tools-extra/docs/ReleaseNotes.md
index 30a58ae13a511d..cab821f08657a5 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -401,6 +401,11 @@ infrastructure are described first, followed by 
tool-specific sections.
   avoid offering fix-its for string literals originating from macro
   expansions.
 
+- Improved {doc}`performance-unnecessary-copy-initialization
+  <clang-tidy/checks/performance/unnecessary-copy-initialization>` check to
+  no longer suggest removing unused variables marked `[[maybe_unused]]`. It
+  suggests a const reference instead.
+
 - Improved {doc}`readability-convert-member-functions-to-static
   <clang-tidy/checks/readability/convert-member-functions-to-static>` check by
   fixing a crash when checking a const-qualified method declared with 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 4818e9ddfc86f7..0929b9ecc00653 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
@@ -771,6 +771,21 @@ void positiveUnusedReferenceIsRemoved() {
   // CHECK-FIXES: // Comments on a new line should not be deleted.
 }
 
+void positiveMaybeUnusedIsNotRemoved() {
+  [[maybe_unused]] auto FromCall = ExpensiveTypeReference();
+  // CHECK-MESSAGES: [[@LINE-1]]:25: warning: the variable 'FromCall' of type 
'ExpensiveToCopyType' is copy-constructed from a const reference but is only 
used as const reference; consider making it a const reference 
[performance-unnecessary-copy-initialization]
+  // CHECK-FIXES: {{\[\[maybe_unused\]\]}} const auto& FromCall = 
ExpensiveTypeReference();
+
+  ExpensiveToCopyType Orig;
+  [[maybe_unused]] auto FromVar{Orig};
+  // CHECK-MESSAGES: [[@LINE-1]]:25: warning: local copy 'FromVar' of the 
variable 'Orig' of type 'ExpensiveToCopyType' is never modified; consider 
avoiding the copy [performance-unnecessary-copy-initialization]
+  // CHECK-FIXES: {{\[\[maybe_unused\]\]}} const auto& FromVar{Orig};
+
+  __attribute__((unused)) auto GnuSpelling = Orig;
+  // CHECK-MESSAGES: [[@LINE-1]]:32: warning: local copy 'GnuSpelling' of the 
variable 'Orig' of type 'ExpensiveToCopyType' is never modified; consider 
avoiding the copy [performance-unnecessary-copy-initialization]
+  // CHECK-FIXES: __attribute__((unused)) const auto& GnuSpelling = Orig;
+}
+
 void positiveLoopedOverObjectIsConst() {
   const Container<ExpensiveToCopyType> Orig;
   for (const auto &Element : Orig) {

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to