[clang-tools-extra] 661d2ce - Fix modernize-use-nodiscard for classes marked [[nodiscard]]

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 30 10:45:46 PDT 2019


Author: Aaron Ballman
Date: 2019-10-30T13:45:37-04:00
New Revision: 661d2ce619e05dc47a9a232333c01dcba445cd13

URL: https://github.com/llvm/llvm-project/commit/661d2ce619e05dc47a9a232333c01dcba445cd13
DIFF: https://github.com/llvm/llvm-project/commit/661d2ce619e05dc47a9a232333c01dcba445cd13.diff

LOG: Fix modernize-use-nodiscard for classes marked [[nodiscard]]

Current implementation suggests to add [[nodiscard]] to methods even if the
return type is marked already as [[nodiscard]].

Patch by Eugene Sedykh.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/modernize-use-nodiscard.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp
index d2ac7d529ce6..f9d72f5d62c2 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp
@@ -100,7 +100,9 @@ void UseNodiscardCheck::registerMatchers(MatchFinder *Finder) {
       cxxMethodDecl(
           allOf(isConst(), isDefinitionOrInline(),
                 unless(anyOf(
-                    returns(voidType()), isNoReturn(), isOverloadedOperator(),
+                    returns(voidType()),
+                    returns(hasDeclaration(decl(hasAttr(clang::attr::WarnUnusedResult)))),
+                    isNoReturn(), isOverloadedOperator(),
                     isVariadic(), hasTemplateReturnType(),
                     hasClassMutableFields(), isConversionOperator(),
                     hasAttr(clang::attr::WarnUnusedResult),

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-nodiscard.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-nodiscard.cpp
index a571f0963637..7340713eefa4 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-nodiscard.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-nodiscard.cpp
@@ -23,6 +23,8 @@ typedef unsigned my_unsigned;
 typedef unsigned &my_unsigned_reference;
 typedef const unsigned &my_unsigned_const_reference;
 
+struct NO_DISCARD NoDiscardStruct{};
+
 class Foo {
 public:
     using size_type = unsigned;
@@ -160,6 +162,9 @@ class Foo {
 
     // Do not add ``[[nodiscard]]`` to conversion functions.
     // explicit operator bool() const { return true; }
+
+    // Do not add ``[[nodiscard]]`` to functions returning types marked [[nodiscard]].
+    NoDiscardStruct f50() const;
 };
 
 // Do not add ``[[nodiscard]]`` to Lambda.


        


More information about the cfe-commits mailing list