llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-tidy Author: David Meng (davidmenggx) <details> <summary>Changes</summary> The check no longer treats variables marked [[maybe_unused]] as unused. It suggests making them a const reference instead. Fixes #<!-- -->244404. --- Full diff: https://github.com/llvm/llvm-project/pull/244978.diff 3 Files Affected: - (modified) clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitializationCheck.cpp (+4-1) - (modified) clang-tools-extra/docs/ReleaseNotes.md (+5) - (modified) clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp (+15) ``````````diff 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) { `````````` </details> https://github.com/llvm/llvm-project/pull/244978 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
