[clang-tools-extra] [clang-tidy] Add readability-pointer-to-ref check (PR #182068)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 18 23:03:29 PST 2026
================
@@ -0,0 +1,199 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "PointerToRefCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+/// Visitor that classifies how a pointer parameter is used in a function body.
+/// It detects:
+/// - Dereferences (operator*, operator->)
+/// - Null checks (if(ptr), ptr != nullptr, assert(ptr), etc.)
+/// - Pointer arithmetic
+/// - Being passed to other functions as a pointer
+/// - Address-of or array subscript usage
+class PointerUsageVisitor : public RecursiveASTVisitor<PointerUsageVisitor> {
+public:
+ explicit PointerUsageVisitor(const ParmVarDecl *Param) : Param(Param) {}
+
+ bool isDereferenced() const { return Dereferenced; }
+ bool isNullChecked() const { return NullChecked; }
+ bool isUsedAsPointer() const { return UsedAsPointer; }
+
+ bool VisitUnaryOperator(const UnaryOperator *UO) {
+ if (UO->getOpcode() == UO_Deref && refersToParam(UO->getSubExpr()))
+ Dereferenced = true;
+ return true;
----------------
zeyi2 wrote:
IMO we should handle `&` here? WDYT?
```cpp
if (UO->getOpcode() == UO_AddrOf && refersToParam(SubExp))
UsedAsPointer = true;
```
https://github.com/llvm/llvm-project/pull/182068
More information about the cfe-commits
mailing list