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

Jordan R AW via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 6 11:51:10 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()))
+          .bind("op"),
+      this);
+
+  // Note, that while some of these containers check equality through the
+  // `Compare` requirement, the `Compare` requirement mandates strict weak
+  // ordering, which is unspecified behaviour anyways. So even though they
+  // aren't an issue with --icf=all, they're still discouraged.
+  Finder->addMatcher(
+      declaratorDecl(
+          hasType(hasCanonicalType(
+              recordType(hasDeclaration(classTemplateSpecializationDecl(
+                  hasAnyName(
+                      "::std::flat_map", "::std::flat_set", "::std::map",
----------------
ajordanr-google wrote:

This is a great idea. I'll take a look. Thanks!

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


More information about the cfe-commits mailing list