[clang-tools-extra] [clang-tidy] Add check bugprone-misleading-setter-of-reference (PR #132242)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Tue May 6 06:56:38 PDT 2025
================
@@ -6,42 +6,42 @@ bugprone-misleading-setter-of-reference
Finds setter-like member functions that take a pointer parameter and set a
(non-const) reference member of the same class with the pointed value.
+The checker detects public member functions that have a single parameter (which
+is a pointer) and contain a single (maybe overloaded) assignment operator call.
+The assignment should set a member variable with the dereference of the
+parameter pointer. The member variable can have any visibility.
+
The fact that a setter function takes a pointer might cause the belief that an
internal reference (if it would be a pointer) is changed instead of the
pointed-to (or referenced) value.
-Only member functions are detected which have a single parameter and contain a
-single (maybe overloaded) assignment operator call. The changed member variable
-must be private (or protected) for the checker to detect the fault (a public
-member can be changed anyway without a set function).
-
Example:
.. code-block:: c++
- class Widget {
- int& ref_; // non-const reference member
+ class MyClass {
+ int &InternalRef; // non-const reference member
public:
- Widget(int &value) : ref_(value) {}
+ MyClass(int &Value) : InternalRef(Value) {}
- // Warning: Potentially dangerous setter that could lead to unintended behaviour
- void setRef(int *ptr) {
- ref_ = *ptr; // This assigns to the referenced value, not changing what ref_ references
+ // Warning: This setter could lead to unintended behaviour.
+ void setRef(int *Value) {
+ InternalRef = *Value; // This assigns to the referenced value, not changing what ref_ references.
----------------
NagyDonat wrote:
The comment still uses the old name `ref_`, please update it.
https://github.com/llvm/llvm-project/pull/132242
More information about the cfe-commits
mailing list