[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 22:58:07 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;
+ }
+
+ bool VisitMemberExpr(const MemberExpr *ME) {
+ if (ME->isArrow() && refersToParam(ME->getBase()))
+ Dereferenced = true;
+ return true;
+ }
+
+ // Detect null checks: if(ptr), ptr == nullptr, !ptr, etc.
+ bool VisitImplicitCastExpr(const ImplicitCastExpr *ICE) {
+ if (ICE->getCastKind() == CK_PointerToBoolean &&
+ refersToParam(ICE->getSubExpr()))
+ NullChecked = true;
+ return true;
+ }
+
+ bool VisitBinaryOperator(const BinaryOperator *BO) {
+ const Expr *LHS = BO->getLHS()->IgnoreImplicit();
----------------
zeyi2 wrote:
`IgnoreParenImpCasts()`? WDYT?
https://github.com/llvm/llvm-project/pull/182068
More information about the cfe-commits
mailing list