[clang-tidy] Ignore singe bit bitfield -> bool conversion in readability-implicit-bool-conversion

llvm-svn: 343578
This commit is contained in:
Alexander Kornienko 2018-10-02 11:38:41 +00:00
parent 271bcb9397
commit c5016c0a27
3 changed files with 25 additions and 1 deletions

View File

@ -266,6 +266,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
auto exceptionCases = auto exceptionCases =
expr(anyOf(allOf(isMacroExpansion(), unless(isNULLMacroExpansion())), expr(anyOf(allOf(isMacroExpansion(), unless(isNULLMacroExpansion())),
has(ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1)))))),
hasParent(explicitCastExpr()))); hasParent(explicitCastExpr())));
auto implicitCastFromBool = implicitCastExpr( auto implicitCastFromBool = implicitCastExpr(
anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating), anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),

View File

@ -66,7 +66,9 @@ example:
In general, the following conversion types are checked: In general, the following conversion types are checked:
- integer expression/literal to boolean, - integer expression/literal to boolean (conversion from a single bit bitfield
to boolean is explicitly allowed, since there's no ambiguity / information
loss in this case),
- floating expression/literal to boolean, - floating expression/literal to boolean,

View File

@ -437,3 +437,24 @@ void useOfUserConversion() {
} }
} // namespace ignoreUserDefinedConversionOperator } // namespace ignoreUserDefinedConversionOperator
namespace ignore_1bit_bitfields {
struct S {
int a;
int b : 1;
int c : 2;
};
bool f(const S& s) {
functionTaking<bool>(s.a);
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'int' -> bool
// CHECK-FIXES: functionTaking<bool>(s.a != 0);
functionTaking<bool>(s.b);
// CHECK-FIXES: functionTaking<bool>(s.b);
functionTaking<bool>(s.c);
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'int' -> bool
// CHECK-FIXES: functionTaking<bool>(s.c != 0);
}
} // namespace ignore_1bit_bitfields