Skip to content

Commit 10bdcf6

Browse files
authored
[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
1 parent 42d9901 commit 10bdcf6

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "SignedBitwiseCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
#include "clang/ASTMatchers/ASTMatchers.h"
1213

1314
using namespace clang::ast_matchers;
1415
using namespace clang::ast_matchers::internal;
@@ -29,8 +30,8 @@ void SignedBitwiseCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
2930
void SignedBitwiseCheck::registerMatchers(MatchFinder *Finder) {
3031
const auto SignedIntegerOperand =
3132
(IgnorePositiveIntegerLiterals
32-
? expr(ignoringImpCasts(hasType(isSignedInteger())),
33-
unless(integerLiteral()))
33+
? expr(ignoringImpCasts(
34+
allOf(hasType(isSignedInteger()), unless(integerLiteral()))))
3435
: expr(ignoringImpCasts(hasType(isSignedInteger()))))
3536
.bind("signed-operand");
3637

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ Changes in existing checks
259259
- Improved :doc:`google-runtime-int <clang-tidy/checks/google/runtime-int>`
260260
check performance through optimizations.
261261

262+
- Improved :doc:`hicpp-signed-bitwise <clang-tidy/checks/hicpp/signed-bitwise>`
263+
check by ignoring false positives involving positive integer literals behind
264+
implicit casts when `IgnorePositiveIntegerLiterals` is enabled.
265+
262266
- Improved :doc:`hicpp-ignored-remove-result <clang-tidy/checks/hicpp/ignored-remove-result>`
263267
check by ignoring other functions with same prefixes as the target specific
264268
functions.

clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ void examples() {
1111
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use of a signed integer operand with a binary bitwise operator
1212

1313
unsigned URes2 = URes << 1; //Ok
14+
unsigned URes3 = URes & 1; //Ok
1415

1516
int IResult;
1617
IResult = 10 & 2; //Ok
@@ -21,6 +22,8 @@ void examples() {
2122
IResult = Int << 1;
2223
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
2324
IResult = ~0; //Ok
25+
IResult = -1 & 1;
26+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator [hicpp-signed-bitwise]
2427
}
2528

2629
enum EnumConstruction {

0 commit comments

Comments
 (0)