140 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-derived-method-shadowing-base-method %t2 3class Base 4{5 void method();6 void methodWithArg(int I);7 8 virtual Base* getThis() = 0;9};10 11class A : public Base12{13public:14 void method();15// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'A::method' shadows method with the same name in class 'Base' [bugprone-derived-method-shadowing-base-method]16// CHECK-MESSAGES: :5:5: note: previous definition of 'method' is here17};18 19// only declaration should be checked20void A::method()21{ 22}23 24class B25{26public:27 void method();28};29 30class D: public Base31{32 33};34 35// test indirect inheritance36class E : public D37{38public:39 void method();40// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'E::method' shadows method with the same name in class 'Base' [bugprone-derived-method-shadowing-base-method]41};42 43class H : public Base44{45public:46 Base* getThis() override;47 Base const* getThis() const;48};49 50class I : public Base51{52public:53 // test with inline implementation54 void method()55// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'I::method' shadows method with the same name in class 'Base' [bugprone-derived-method-shadowing-base-method]56 {57 58 }59};60 61class J : public Base62{63public:64 Base* getThis() final;65};66 67template<typename T>68class TemplateBase69{70public:71 virtual void size() const = 0;72};73 74template<typename T>75class K : public TemplateBase<T>76{77public:78 void size() const final;79};80 81class L : public Base82{83public:84// not same signature (take const ref) but still ambiguous85 void methodWithArg(int const& I);86// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'L::methodWithArg' shadows method with the same name in class 'Base' [bugprone-derived-method-shadowing-base-method]87 88 void methodWithArg(int const I);89// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'L::methodWithArg' shadows method with the same name in class 'Base' [bugprone-derived-method-shadowing-base-method]90 91 void methodWithArg(int *I);92 void methodWithArg(int const* I);93};94 95class M : public Base96{97public:98 static void method();99};100 101class N : public Base102{103public:104 template<typename T>105 void methodWithArg(T I);106 // TODO: Templates are not handled yet107 template<> void methodWithArg<int>(int I);108};109 110namespace std{111 struct thread{112 void join();113 };114}115 116struct O: public std::thread{117 void join();118};119 120struct P: public std::thread, Base{121 void join();122 void method();123 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'P::method' shadows method with the same name in class 'Base' [bugprone-derived-method-shadowing-base-method]124};125 126class Q : public Base127{128public:129 typedef int MyInt;130// not same signature (take const ref) but still ambiguous131 void methodWithArg(MyInt const& I);132// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'Q::methodWithArg' shadows method with the same name in class 'Base' [bugprone-derived-method-shadowing-base-method]133 134 void methodWithArg(MyInt const I);135// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'Q::methodWithArg' shadows method with the same name in class 'Base' [bugprone-derived-method-shadowing-base-method]136 137 void methodWithArg(MyInt *I);138 void methodWithArg(MyInt const* I);139};140