[clang-tools-extra] [clang-tidy] Add misc-function-pointer-equality lint (PR #239430)

Jordan R AW via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 13 13:47:28 PST 2026


================
@@ -0,0 +1,75 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "FunctionPointerEqualityCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::performance {
+
+void FunctionPointerEqualityCheck::registerMatchers(MatchFinder *Finder) {
+  const auto FunctionPointerType = hasCanonicalType(pointsTo(functionType()));
+
+  // Matching ptr == ptr, but not ptr == nullptr, or ptr == integer (function
+  // pointers to 1 and -1 are frequently used for "invalid" function pointers).
+  //
+  Finder->addMatcher(
+      binaryOperator(hasAnyOperatorName("==", "!="),
+                     hasLHS(hasType(FunctionPointerType)),
+                     hasRHS(hasType(FunctionPointerType)),
+                     unless(hasEitherOperand(ignoringParenCasts(
+                         anyOf(nullPointerConstant(), hasType(isInteger()))))),
+                     unless(isInTemplateInstantiation()))
----------------
ajordanr-google wrote:

I have added an unless clause to avoid matching static_assert, as well as some common unevaluated contexts.

https://github.com/llvm/llvm-project/pull/239430


More information about the cfe-commits mailing list