================
@@ -0,0 +1,50 @@
+.. title:: clang-tidy - readability-pointer-to-ref
+
+readability-pointer-to-ref
+==========================
+
+Finds function parameters that are pointers but are always dereferenced without
+null checks, suggesting they should be references instead.
+
+Using references instead of pointers for non-nullable parameters makes the
+contract explicit: the caller must provide a valid object. This improves
+readability and can prevent null pointer bugs.
+
+.. code-block:: c++
+
+ // Before -- pointer that is never null-checked
+ void process(Foo *P) {
+ P->bar();
+ P->X = 42;
+ }
+
+ // After -- use a reference instead
+ void process(Foo &P) {
+ P.bar();
+ P.X = 42;
+ }
+
+The check will not flag a parameter if it:
+
+- is checked for null (``if (P)``, ``P != nullptr``, etc.),
+- is passed to another function as a pointer argument,
+- is used in pointer arithmetic or array subscript,
+- is a ``void`` pointer or pointer to an incomplete type,
+- is a parameter of a virtual method,
+- is a function pointer,
+- is unnamed,
+- is in an ``extern "C"`` function,
+- is used in a ``delete`` expression,
+- is returned from the function,
+- is stored to another variable,
+- is passed to a constructor,
+- is captured by a lambda,
+- has its address taken (``&P``),
+- is compared to another non-null pointer, or
+- is never dereferenced in the function body.
----------------
zwuis wrote:
I think it is not necessary to document things obvious, e.g. "checked for null".
https://github.com/llvm/llvm-project/pull/182068
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits