diff --git a/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp b/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp --- a/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp +++ b/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp @@ -46,15 +46,24 @@ "::std::ios_base::openmode")); const auto IsStdBitmask = ignoringImpCasts(declRefExpr(hasType(BitmaskType))); + const auto SignNoInterferenceOp = + hasAnyOperatorName("^", "^=", "|", "|=", "&", "&="); + + const auto SignedIntegerOperandExtended = + (IgnorePositiveIntegerLiterals + ? expr(ignoringImpCasts(hasType(isSignedInteger())), + unless(anyOf(integerLiteral(), + binaryOperator(SignNoInterferenceOp)))) + : expr(ignoringImpCasts(hasType(isSignedInteger())))) + .bind("signed-operand"); + // Match binary bitwise operations on signed integer arguments. Finder->addMatcher( - binaryOperator(anyOf(hasOperatorName("^"), hasOperatorName("|"), - hasOperatorName("&"), hasOperatorName("^="), - hasOperatorName("|="), hasOperatorName("&=")), + binaryOperator(SignNoInterferenceOp, unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))), - hasEitherOperand(SignedIntegerOperand), + hasEitherOperand(SignedIntegerOperandExtended), hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger()))) .bind("binary-no-sign-interference"), this); diff --git a/clang-tools-extra/test/clang-tidy/checkers/hicpp-signed-bitwise-integer-literals.cpp b/clang-tools-extra/test/clang-tidy/checkers/hicpp-signed-bitwise-integer-literals.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/hicpp-signed-bitwise-integer-literals.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/hicpp-signed-bitwise-integer-literals.cpp @@ -21,6 +21,11 @@ IResult = Int << 1; // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator IResult = ~0; //Ok + + IResult = 2 | 4 | 8; + IResult = 2 | 4 | 8 | 16; + IResult = 2 | 4 | 8 | 16 | -1; + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator } enum EnumConstruction {