[clang-tools-extra] [clang-tidy] New bugprone-derived-method-shadowing-base-method (PR #154746)

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 7 22:42:28 PDT 2025


================
@@ -0,0 +1,124 @@
+// RUN: %check_clang_tidy %s bugprone-derived-method-shadowing-base-method %t
+
+class Base 
+{
+    void method();
+    void methodWithArg(int I);
+
+    virtual Base* getThis() = 0;
+};
+
+class A : public Base
+{
+public:
+    void method();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'A::method' hides same method in 'Base' [bugprone-derived-method-shadowing-base-method]
+};
+
+// only declaration should be checked
+void A::method()
+{    
+}
+
+class B
+{
+public:
+    void method();
+};
+
+class D: public Base
+{
+
+};
+
+// test indirect inheritance
+class E : public D
+{
+public:
+    void method();
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'E::method' hides same method in 'Base' [bugprone-derived-method-shadowing-base-method]
+};
+
+class H : public Base
+{
+public:
+    Base* getThis() override;
+    Base const* getThis() const;
+};
+
+class I : public Base
+{
+public:
+    // test with inline implementation
+    void method()
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'I::method' hides same method in 'Base' [bugprone-derived-method-shadowing-base-method]
+    {
+
+    }
+};
+
+class J : public Base
+{
+public:
+    Base* getThis() final;
+};
+
+template<typename T>
+class TemplateBase
+{
+public:
+   virtual void size() const = 0;
+};
+    
+template<typename T>
+class K : public TemplateBase<T>
+{
+public:
+    void size() const final;
+};
+
+class L : public Base
+{
+public:
+// not same signature (take const ref) but still ambiguous
+    void methodWithArg(int const& I);
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'L::methodWithArg' hides same method in 'Base' [bugprone-derived-method-shadowing-base-method]
+
+    void methodWithArg(int const I);
----------------
t-a-james wrote:

Yep, this works just fine - added a unit test

https://github.com/llvm/llvm-project/pull/154746


More information about the cfe-commits mailing list