From 5b5b75bfd4d469d56db7980c578214c26288faa9 Mon Sep 17 00:00:00 2001 From: Piotr Zegar Date: Sat, 22 Jul 2023 19:24:53 +0000 Subject: [PATCH] [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 --- .../clang-tidy/cppcoreguidelines/OwningMemoryCheck.h | 3 +++ clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ .../clang-tidy/checkers/cppcoreguidelines/owning-memory.cpp | 13 +++++++++++++ 3 files changed, 20 insertions(+) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.h index 4dd2f56..3ab8f34 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.h +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.h @@ -37,6 +37,9 @@ public: void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + std::optional 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 0f41c2d..2747591 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 + ` check now finds more + issues, especially those related to implicit casts. + - Deprecated C.48 enforcement from :doc:`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 2669d66..eb8ad1b 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<>' + } +} -- 2.7.4