[clang-tools-extra] r343578 - [clang-tidy] Ignore singe bit bitfield -> bool conversion in readability-implicit-bool-conversion
Alexander Kornienko via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 2 04:38:41 PDT 2018
Author: alexfh
Date: Tue Oct 2 04:38:41 2018
New Revision: 343578
URL: http://llvm.org/viewvc/llvm-project?rev=343578&view=rev
Log:
[clang-tidy] Ignore singe bit bitfield -> bool conversion in readability-implicit-bool-conversion
Modified:
clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
clang-tools-extra/trunk/docs/clang-tidy/checks/readability-implicit-bool-conversion.rst
clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-conversion.cpp
Modified: clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolConversionCheck.cpp?rev=343578&r1=343577&r2=343578&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolConversionCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolConversionCheck.cpp Tue Oct 2 04:38:41 2018
@@ -266,6 +266,7 @@ void ImplicitBoolConversionCheck::regist
auto exceptionCases =
expr(anyOf(allOf(isMacroExpansion(), unless(isNULLMacroExpansion())),
+ has(ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1)))))),
hasParent(explicitCastExpr())));
auto implicitCastFromBool = implicitCastExpr(
anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/readability-implicit-bool-conversion.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/readability-implicit-bool-conversion.rst?rev=343578&r1=343577&r2=343578&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/readability-implicit-bool-conversion.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/readability-implicit-bool-conversion.rst Tue Oct 2 04:38:41 2018
@@ -66,7 +66,9 @@ example:
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,
Modified: clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-conversion.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-conversion.cpp?rev=343578&r1=343577&r2=343578&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-conversion.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-implicit-bool-conversion.cpp Tue Oct 2 04:38:41 2018
@@ -437,3 +437,24 @@ void useOfUserConversion() {
}
} // 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
More information about the cfe-commits
mailing list