[PATCH] D54941: [clang-tidy] Ignore bool -> single bit bitfield conversion in readability-implicit-bool-conversion
Malcolm Parsons via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 27 02:53:20 PST 2018
malcolm.parsons created this revision.
malcolm.parsons added reviewers: alexfh, aaron.ballman, hokein.
Herald added subscribers: cfe-commits, xazax.hun.
There is no ambiguity / information loss in this conversion
Repository:
rCTE Clang Tools Extra
https://reviews.llvm.org/D54941
Files:
clang-tidy/readability/ImplicitBoolConversionCheck.cpp
docs/clang-tidy/checks/readability-implicit-bool-conversion.rst
test/clang-tidy/readability-implicit-bool-conversion.cpp
Index: test/clang-tidy/readability-implicit-bool-conversion.cpp
===================================================================
--- test/clang-tidy/readability-implicit-bool-conversion.cpp
+++ test/clang-tidy/readability-implicit-bool-conversion.cpp
@@ -444,14 +444,27 @@
int a;
int b : 1;
int c : 2;
+
+ S(bool a, bool b, bool c) : a(a), b(b), c(c) {}
+ // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: implicit conversion bool -> 'int'
+ // CHECK-MESSAGES: :[[@LINE-2]]:45: warning: implicit conversion bool -> 'int'
+ // CHECK-FIXES: S(bool a, bool b, bool c) : a(static_cast<int>(a)), b(b), c(static_cast<int>(c)) {}
};
-bool f(const S& s) {
+bool f(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);
+ s.a = true;
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: implicit conversion bool -> 'int'
+ // CHECK-FIXES: s.a = 1;
+ s.b = true;
+ // CHECK-FIXES: s.b = true;
+ s.c = true;
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: implicit conversion bool -> 'int'
+ // CHECK-FIXES: s.c = 1;
functionTaking<bool>(s.c);
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: implicit conversion 'int' -> bool
// CHECK-FIXES: functionTaking<bool>(s.c != 0);
Index: docs/clang-tidy/checks/readability-implicit-bool-conversion.rst
===================================================================
--- docs/clang-tidy/checks/readability-implicit-bool-conversion.rst
+++ docs/clang-tidy/checks/readability-implicit-bool-conversion.rst
@@ -74,7 +74,8 @@
- pointer/pointer to member/``nullptr``/``NULL`` to boolean,
-- boolean expression/literal to integer,
+- boolean expression/literal to integer (conversion from boolean to a single
+ bit bitfield is explicitly allowed),
- boolean expression/literal to floating.
Index: clang-tidy/readability/ImplicitBoolConversionCheck.cpp
===================================================================
--- clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -306,6 +306,11 @@
auto boolOpAssignment =
binaryOperator(anyOf(hasOperatorName("|="), hasOperatorName("&=")),
hasLHS(expr(hasType(booleanType()))));
+ auto bitfieldAssignment = binaryOperator(
+ hasLHS(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1))))));
+ auto bitfieldConstruct = cxxConstructorDecl(hasDescendant(cxxCtorInitializer(
+ withInitializer(equalsBoundNode("implicitCastFromBool")),
+ forField(hasBitWidth(1)))));
Finder->addMatcher(
implicitCastExpr(
implicitCastFromBool,
@@ -313,14 +318,15 @@
// in such context:
// bool_expr_a == bool_expr_b
// bool_expr_a != bool_expr_b
- unless(hasParent(binaryOperator(
- anyOf(boolComparison, boolXor, boolOpAssignment)))),
+ unless(hasParent(binaryOperator(anyOf(
+ boolComparison, boolXor, boolOpAssignment, bitfieldAssignment)))),
+ implicitCastExpr().bind("implicitCastFromBool"),
+ unless(hasParent(bitfieldConstruct)),
// Check also for nested casts, for example: bool -> int -> float.
anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
anything()),
unless(isInTemplateInstantiation()),
- unless(hasAncestor(functionTemplateDecl())))
- .bind("implicitCastFromBool"),
+ unless(hasAncestor(functionTemplateDecl()))),
this);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54941.175438.patch
Type: text/x-patch
Size: 3649 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181127/b886f2f3/attachment.bin>
More information about the cfe-commits
mailing list