[clang-tidy] Handle implicit casts in hicpp-signed-bitwise for IgnorePositiveIntegerLiterals (#90621)
Improved hicpp-signed-bitwise check by ignoring false positives
involving positive integer literals behind implicit casts when
IgnorePositiveIntegerLiterals is enabled.
Closes #89367
GitOrigin-RevId: 10bdcf6b4cd37d017753b3821fbf8eb2ad924a1a
diff --git a/clang-tidy/hicpp/SignedBitwiseCheck.cpp b/clang-tidy/hicpp/SignedBitwiseCheck.cpp
index 51cc264..bf09a66 100644
--- a/clang-tidy/hicpp/SignedBitwiseCheck.cpp
+++ b/clang-tidy/hicpp/SignedBitwiseCheck.cpp
@@ -9,6 +9,7 @@
#include "SignedBitwiseCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
using namespace clang::ast_matchers;
using namespace clang::ast_matchers::internal;
@@ -29,8 +30,8 @@
void SignedBitwiseCheck::registerMatchers(MatchFinder *Finder) {
const auto SignedIntegerOperand =
(IgnorePositiveIntegerLiterals
- ? expr(ignoringImpCasts(hasType(isSignedInteger())),
- unless(integerLiteral()))
+ ? expr(ignoringImpCasts(
+ allOf(hasType(isSignedInteger()), unless(integerLiteral()))))
: expr(ignoringImpCasts(hasType(isSignedInteger()))))
.bind("signed-operand");
diff --git a/docs/ReleaseNotes.rst b/docs/ReleaseNotes.rst
index 5d6d035..c4c9df2 100644
--- a/docs/ReleaseNotes.rst
+++ b/docs/ReleaseNotes.rst
@@ -259,6 +259,10 @@
- Improved :doc:`google-runtime-int <clang-tidy/checks/google/runtime-int>`
check performance through optimizations.
+- Improved :doc:`hicpp-signed-bitwise <clang-tidy/checks/hicpp/signed-bitwise>`
+ check by ignoring false positives involving positive integer literals behind
+ implicit casts when `IgnorePositiveIntegerLiterals` is enabled.
+
- Improved :doc:`hicpp-ignored-remove-result <clang-tidy/checks/hicpp/ignored-remove-result>`
check by ignoring other functions with same prefixes as the target specific
functions.
diff --git a/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp b/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp
index edbb56f..aca7ae1 100644
--- a/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp
+++ b/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp
@@ -11,6 +11,7 @@
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use of a signed integer operand with a binary bitwise operator
unsigned URes2 = URes << 1; //Ok
+ unsigned URes3 = URes & 1; //Ok
int IResult;
IResult = 10 & 2; //Ok
@@ -21,6 +22,8 @@
IResult = Int << 1;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
IResult = ~0; //Ok
+ IResult = -1 & 1;
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator [hicpp-signed-bitwise]
}
enum EnumConstruction {