[clang-tools-extra] [clang-tidy] New checker: modernize.use-std-bit to detect std::has_one_bit idiom (PR #185435)

Yanzuo Liu via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 9 10:31:45 PDT 2026


================
@@ -0,0 +1,84 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "UseStdBitCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseStdBitCheck::registerMatchers(MatchFinder *Finder) {
+  const auto MakeBinaryOperatorMatcher = [](auto Op) {
+    return [=](const auto &LHS, const auto &RHS) {
+      return binaryOperator(
+          hasOperatorName(Op),
+          hasOperands(ignoringParenImpCasts(LHS), ignoringParenImpCasts(RHS)));
+    };
+  };
+
+  const auto LogicalAnd = MakeBinaryOperatorMatcher("&&");
+  const auto Sub = MakeBinaryOperatorMatcher("-");
----------------
zwuis wrote:

IIUC this matcher matches both `v - 1` and `1 - v`. Ditto for `CmpGt`.

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


More information about the cfe-commits mailing list