1 | // RUN: %check_clang_tidy %s bugprone-misleading-setter-of-reference %t |
2 | |
3 | struct X { |
4 | X &operator=(const X &) { return *this; } |
5 | private: |
6 | int &Mem; |
7 | friend class Test1; |
8 | }; |
9 | |
10 | class Test1 { |
11 | X &MemX; |
12 | int &MemI; |
13 | protected: |
14 | long &MemL; |
15 | public: |
16 | long &MemLPub; |
17 | |
18 | Test1(X &MemX, int &MemI, long &MemL) : MemX(MemX), MemI(MemI), MemL(MemL), MemLPub(MemL) {} |
19 | void setI(int *NewValue) { |
20 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'setI' can be mistakenly used in order to change the reference 'MemI' instead of the value of it |
21 | MemI = *NewValue; |
22 | } |
23 | void setL(long *NewValue) { |
24 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'setL' can be mistakenly used in order to change the reference 'MemL' instead of the value of it |
25 | MemL = *NewValue; |
26 | } |
27 | void setX(X *NewValue) { |
28 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'setX' can be mistakenly used in order to change the reference 'MemX' instead of the value of it |
29 | MemX = *NewValue; |
30 | } |
31 | void set1(int *NewValue) { |
32 | MemX.Mem = *NewValue; |
33 | } |
34 | void set2(int *NewValue) { |
35 | MemL = static_cast<long>(*NewValue); |
36 | } |
37 | void set3(int *NewValue) { |
38 | MemI = *NewValue; |
39 | MemL = static_cast<long>(*NewValue); |
40 | } |
41 | void set4(long *NewValue, int) { |
42 | MemL = *NewValue; |
43 | } |
44 | void setLPub(long *NewValue) { |
45 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'setLPub' can be mistakenly used in order to change the reference 'MemLPub' instead of the value of it |
46 | MemLPub = *NewValue; |
47 | } |
48 | |
49 | private: |
50 | void set5(long *NewValue) { |
51 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'set5' can be mistakenly used in order to change the reference 'MemL' instead of the value of it |
52 | MemL = *NewValue; |
53 | } |
54 | }; |
55 | |
56 | class Base { |
57 | protected: |
58 | int &MemI; |
59 | }; |
60 | |
61 | class Derived : public Base { |
62 | public: |
63 | void setI(int *NewValue) { |
64 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'setI' can be mistakenly used in order to change the reference 'MemI' instead of the value of it |
65 | MemI = *NewValue; |
66 | } |
67 | }; |
68 | |
69 | using UIntRef = unsigned int &; |
70 | using UIntPtr = unsigned int *; |
71 | using UInt = unsigned int; |
72 | |
73 | class AliasTest { |
74 | UIntRef Value1; |
75 | UInt &Value2; |
76 | unsigned int &Value3; |
77 | public: |
78 | void setValue1(UIntPtr NewValue) { |
79 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'setValue1' can be mistakenly used in order to change the reference 'Value1' instead of the value of it |
80 | Value1 = *NewValue; |
81 | } |
82 | void setValue2(unsigned int *NewValue) { |
83 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'setValue2' can be mistakenly used in order to change the reference 'Value2' instead of the value of it |
84 | Value2 = *NewValue; |
85 | } |
86 | void setValue3(UInt *NewValue) { |
87 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'setValue3' can be mistakenly used in order to change the reference 'Value3' instead of the value of it |
88 | Value3 = *NewValue; |
89 | } |
90 | }; |
91 | |
92 | template <typename T> |
93 | class TemplateTest { |
94 | T &Mem; |
95 | public: |
96 | TemplateTest(T &V) : Mem{V} {} |
97 | void setValue(T *NewValue) { |
98 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'setValue' can be mistakenly used in order to change the reference 'Mem' instead of the value of it |
99 | Mem = *NewValue; |
100 | } |
101 | }; |
102 | |
103 | void f_TemplateTest(char *Value) { |
104 | char CharValue; |
105 | TemplateTest<char> TTChar{CharValue}; |
106 | TTChar.setValue(Value); |
107 | } |
108 | |
109 | template <typename T> |
110 | class AddMember { |
111 | protected: |
112 | T &Value; |
113 | }; |
114 | |
115 | class TemplateBaseTest : public AddMember<int> { |
116 | public: |
117 | void setValue(int *NewValue) { |
118 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: function 'setValue' can be mistakenly used in order to change the reference 'Value' instead of the value of it |
119 | Value = *NewValue; |
120 | } |
121 | }; |
122 | |