[clang-tools-extra] 5b5b75b - [clang-tidy] Ignore implcit casts in cppcoreguidelines-owning-memory

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 22 12:26:50 PDT 2023


Author: Piotr Zegar
Date: 2023-07-22T19:24:54Z
New Revision: 5b5b75bfd4d469d56db7980c578214c26288faa9

URL: https://github.com/llvm/llvm-project/commit/5b5b75bfd4d469d56db7980c578214c26288faa9
DIFF: https://github.com/llvm/llvm-project/commit/5b5b75bfd4d469d56db7980c578214c26288faa9.diff

LOG: [clang-tidy] Ignore implcit casts in cppcoreguidelines-owning-memory

Add getCheckTraversalKind to the check in order
to ignore some implicit casts when matching
expresions.

Fixes: #63994

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D156031

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.h
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/owning-memory.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.h
index 4dd2f569d0f2e2..3ab8f34b580f95 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.h
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.h
@@ -37,6 +37,9 @@ class OwningMemoryCheck : public ClangTidyCheck {
 
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  std::optional<TraversalKind> getCheckTraversalKind() const override {
+    return TK_IgnoreUnlessSpelledInSource;
+  }
 
 private:
   bool handleDeletion(const ast_matchers::BoundNodes &Nodes);

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 0f41c2dfceae6a..274759172689e8 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -330,6 +330,10 @@ Changes in existing checks
   to emit warnings only on classes that are copyable/movable, as required by the
   corresponding rule.
 
+- Improved :doc:`cppcoreguidelines-owning-memory
+  <clang-tidy/checks/cppcoreguidelines/owning-memory>` check now finds more
+  issues, especially those related to implicit casts.
+
 - Deprecated C.48 enforcement from :doc:`cppcoreguidelines-prefer-member-initializer
   <clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>`. Please use
   :doc:`cppcoreguidelines-use-default-member-init

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/owning-memory.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/owning-memory.cpp
index 2669d66627a503..eb8ad1b8b87925 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/owning-memory.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/owning-memory.cpp
@@ -382,3 +382,16 @@ void test_templates() {
   template_function(IntOwner1);  // Ok, but not actually ok, since type deduction removes owner
   template_function(stack_ptr1); // Bad, but type deduction gets it wrong
 }
+
+namespace PR63994 {
+  struct A {
+    virtual ~A() {}
+  };
+
+  struct B : public A {};
+
+  A* foo(int x) {
+    return new B;
+    // CHECK-NOTES: [[@LINE-1]]:5: warning: returning a newly created resource of type 'A *' or 'gsl::owner<>' from a function whose return type is not 'gsl::owner<>'
+  }
+}


        


More information about the cfe-commits mailing list