sedykh.eugene created this revision.
sedykh.eugene added reviewers: MyDeveloperDay, JonasToth.
sedykh.eugene added a project: clang-tools-extra.
Herald added subscribers: cfe-commits, mgehre, xazax.hun.
Herald added a project: clang.

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

Try this:

struct [[nodiscard]] S{};

class C{

  S method() const; --> suggests adding [[nodiscard]]

};

This small diff fixes this incorrect behaviour.

This is my first timid try to contribute to open source, so please help me with 
this piece of code. Maybe there are better ways.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D69388

Files:
  clang-tidy/modernize/UseNodiscardCheck.cpp
  test/clang-tidy/checkers/modernize-use-nodiscard.cpp


Index: test/clang-tidy/checkers/modernize-use-nodiscard.cpp
===================================================================
--- test/clang-tidy/checkers/modernize-use-nodiscard.cpp
+++ test/clang-tidy/checkers/modernize-use-nodiscard.cpp
@@ -23,6 +23,8 @@
 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 @@
 
     // 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.
Index: clang-tidy/modernize/UseNodiscardCheck.cpp
===================================================================
--- clang-tidy/modernize/UseNodiscardCheck.cpp
+++ clang-tidy/modernize/UseNodiscardCheck.cpp
@@ -94,16 +94,20 @@
   auto FunctionObj =
       cxxRecordDecl(hasAnyName("::std::function", "::boost::function"));
 
+  using clang::attr::WarnUnusedResult;
+
   // Find all non-void const methods which have not already been marked to
   // warn on unused result.
   Finder->addMatcher(
       cxxMethodDecl(
           allOf(isConst(), isDefinitionOrInline(),
                 unless(anyOf(
-                    returns(voidType()), isNoReturn(), isOverloadedOperator(),
+                    returns(voidType()),
+                    returns(hasDeclaration(decl(hasAttr(WarnUnusedResult)))),
+                    isNoReturn(), isOverloadedOperator(),
                     isVariadic(), hasTemplateReturnType(),
                     hasClassMutableFields(), isConversionOperator(),
-                    hasAttr(clang::attr::WarnUnusedResult),
+                    hasAttr(WarnUnusedResult),
                     hasType(isInstantiationDependentType()),
                     hasAnyParameter(anyOf(
                         parmVarDecl(anyOf(hasType(FunctionObj),


Index: test/clang-tidy/checkers/modernize-use-nodiscard.cpp
===================================================================
--- test/clang-tidy/checkers/modernize-use-nodiscard.cpp
+++ test/clang-tidy/checkers/modernize-use-nodiscard.cpp
@@ -23,6 +23,8 @@
 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 @@
 
     // 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.
Index: clang-tidy/modernize/UseNodiscardCheck.cpp
===================================================================
--- clang-tidy/modernize/UseNodiscardCheck.cpp
+++ clang-tidy/modernize/UseNodiscardCheck.cpp
@@ -94,16 +94,20 @@
   auto FunctionObj =
       cxxRecordDecl(hasAnyName("::std::function", "::boost::function"));
 
+  using clang::attr::WarnUnusedResult;
+
   // Find all non-void const methods which have not already been marked to
   // warn on unused result.
   Finder->addMatcher(
       cxxMethodDecl(
           allOf(isConst(), isDefinitionOrInline(),
                 unless(anyOf(
-                    returns(voidType()), isNoReturn(), isOverloadedOperator(),
+                    returns(voidType()),
+                    returns(hasDeclaration(decl(hasAttr(WarnUnusedResult)))),
+                    isNoReturn(), isOverloadedOperator(),
                     isVariadic(), hasTemplateReturnType(),
                     hasClassMutableFields(), isConversionOperator(),
-                    hasAttr(clang::attr::WarnUnusedResult),
+                    hasAttr(WarnUnusedResult),
                     hasType(isInstantiationDependentType()),
                     hasAnyParameter(anyOf(
                         parmVarDecl(anyOf(hasType(FunctionObj),
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to