Skip to content

Commit ce9dfb1

Browse files
committed
more review suggestion fixes
1 parent 9c2306e commit ce9dfb1

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ New checks
109109
Finds lambda captures that capture the ``this`` pointer and store it as class
110110
members without handle the copy and move constructors and the assignments.
111111

112+
- New :doc:`bugprone-misleading-setter-of-reference
113+
<clang-tidy/checks/bugprone/misleading-setter-of-reference>` check.
114+
115+
Finds setter-like member functions that take a pointer parameter and set a
116+
reference member of the same class with the pointed value.
117+
112118
- New :doc:`bugprone-unintended-char-ostream-output
113119
<clang-tidy/checks/bugprone/unintended-char-ostream-output>` check.
114120

clang-tools-extra/docs/clang-tidy/checks/bugprone/misleading-setter-of-reference.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ bugprone-misleading-setter-of-reference
44
=======================================
55

66
Finds 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

1413
The fact that a setter function takes a pointer might cause the belief that an
1514
internal 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

4342
Possible 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).

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Clang-Tidy Checks
109109
:doc:`bugprone-lambda-function-name <bugprone/lambda-function-name>`,
110110
:doc:`bugprone-macro-parentheses <bugprone/macro-parentheses>`, "Yes"
111111
:doc:`bugprone-macro-repeated-side-effects <bugprone/macro-repeated-side-effects>`,
112+
:doc:`bugprone-misleading-setter-of-reference <bugprone/misleading-setter-of-reference>`,
112113
:doc:`bugprone-misplaced-operator-in-strlen-in-alloc <bugprone/misplaced-operator-in-strlen-in-alloc>`, "Yes"
113114
:doc:`bugprone-misplaced-pointer-arithmetic-in-alloc <bugprone/misplaced-pointer-arithmetic-in-alloc>`, "Yes"
114115
:doc:`bugprone-misplaced-widening-cast <bugprone/misplaced-widening-cast>`,

clang-tools-extra/test/clang-tidy/checkers/bugprone/misleading-setter-of-reference.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ class TemplateTest {
9999
}
100100
};
101101

102-
char CharValue;
103-
TemplateTest<char> TTChar{CharValue};
102+
void f_TemplateTest(char *Value) {
103+
char CharValue;
104+
TemplateTest<char> TTChar{CharValue};
105+
TTChar.setValue(Value);
106+
}
104107

105108
template <typename T>
106109
class AddMember {

0 commit comments

Comments
 (0)