@@ -4,12 +4,11 @@ bugprone-misleading-setter-of-reference
44=======================================
55
66Finds setter-like member functions that take a pointer parameter and set a
7- (non-const) reference member of the same class with the pointed value.
7+ reference member of the same class with the pointed value.
88
9- The checker detects public member functions that have a single parameter (which
10- is a pointer) and contain a single (maybe overloaded) assignment operator call.
11- The assignment should set a member variable with the dereference of the
12- parameter pointer. The member variable can have any visibility.
9+ The check detects public member functions that take a single pointer parameter,
10+ and contain a single expression statement that dereferences the parameter and
11+ assigns the result to a data member with a reference type.
1312
1413The fact that a setter function takes a pointer might cause the belief that an
1514internal reference (if it would be a pointer) is changed instead of the
@@ -26,22 +25,22 @@ Example:
2625
2726 // Warning: This setter could lead to unintended behaviour.
2827 void setRef(int *Value) {
29- InternalRef = *Value; // This assigns to the referenced value, not changing what ref_ references.
28+ InternalRef = *Value; // This assigns to the referenced value, not changing what InternalRef references.
3029 }
3130 };
3231
3332 int main() {
3433 int Value1 = 42;
3534 int Value2 = 100;
3635 MyClass X(Value1);
37-
36+
3837 // This might look like it changes what InternalRef references to,
3938 // but it actually modifies Value1 to be 100.
4039 X.setRef(&Value2);
4140 }
4241
4342Possible fixes:
44- - Change the parameter type of the "set" function to non-pointer or const reference
45- type .
43+ - Change the parameter type of the "set" function to non-pointer type (for
44+ example, a const reference) .
4645 - Change the type of the member variable to a pointer and in the "set"
4746 function assign a value to the pointer (without dereference).
0 commit comments