[PATCH] D23243: [clang-tidy] enhance modernize-use-bool-literals to check ternary operator

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 6 16:31:06 PDT 2016


omtcyfz created this revision.
omtcyfz added reviewers: alexfh, Eugene.Zelenko.
omtcyfz added a subscriber: cfe-commits.

Fixing bug https://llvm.org/bugs/show_bug.cgi?id=28854.

Implicit cast with ternary operator makes AST look in a way in which it feels reasonable to add second matcher to detect implicit casts in ternary operators.

https://reviews.llvm.org/D23243

Files:
  clang-tidy/modernize/UseBoolLiteralsCheck.cpp
  test/clang-tidy/modernize-use-bool-literals.cpp

Index: test/clang-tidy/modernize-use-bool-literals.cpp
===================================================================
--- test/clang-tidy/modernize-use-bool-literals.cpp
+++ test/clang-tidy/modernize-use-bool-literals.cpp
@@ -116,3 +116,20 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}
   // CHECK-FIXES: {{^ *}}IntToTrue = true;{{$}}
 }
+
+static int Value = 1;
+
+bool Function1() {
+  bool Result = Value == 1 ? 1 : 0;
+  // CHECK-MESSAGES: :[[@LINE-1]]:30: {{.*}}
+  // CHECK-MESSAGES: :[[@LINE-2]]:34: {{.*}}
+  // CHECK-FIXES: {{^ *}}bool Result = Value == 1 ? true : false;{{$}}
+  return Result;
+}
+
+bool Function2() {
+  return Value == 1 ? 1 : 0;
+  // CHECK-MESSAGES: :[[@LINE-1]]:23: {{.*}}
+  // CHECK-MESSAGES: :[[@LINE-2]]:27: {{.*}}
+  // CHECK-FIXES: {{^ *}}return Value == 1 ? true : false;{{$}}
+}
Index: clang-tidy/modernize/UseBoolLiteralsCheck.cpp
===================================================================
--- clang-tidy/modernize/UseBoolLiteralsCheck.cpp
+++ clang-tidy/modernize/UseBoolLiteralsCheck.cpp
@@ -29,25 +29,43 @@
           unless(isInTemplateInstantiation()),
           anyOf(hasParent(explicitCastExpr().bind("cast")), anything())),
       this);
+
+  Finder->addMatcher(
+      conditionalOperator(
+          hasParent(implicitCastExpr(
+              hasImplicitDestinationType(qualType(booleanType())),
+              unless(isInTemplateInstantiation()))),
+          anyOf(hasTrueExpression(ignoringParenImpCasts(
+                    integerLiteral().bind("trueBranchLiteral"))),
+                anything()),
+          anyOf(hasFalseExpression(ignoringParenImpCasts(
+                    integerLiteral().bind("falseBranchLiteral"))),
+                anything())),
+      this);
 }
 
 void UseBoolLiteralsCheck::check(const MatchFinder::MatchResult &Result) {
-  const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>("literal");
-  const auto *Cast = Result.Nodes.getNodeAs<Expr>("cast");
-  bool LiteralBooleanValue = Literal->getValue().getBoolValue();
+  for (const auto &BindingName :
+       {"literal", "trueBranchLiteral", "falseBranchLiteral"}) {
+    const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>(BindingName);
+    if (!Literal)
+      continue;
+    const auto *Cast = Result.Nodes.getNodeAs<Expr>("cast");
+    bool LiteralBooleanValue = Literal->getValue().getBoolValue();
 
-  if (Literal->isInstantiationDependent())
-    return;
+    if (Literal->isInstantiationDependent())
+      continue;
 
-  const Expr *Expression = Cast ? Cast : Literal;
+    const Expr *Expression = Cast ? Cast : Literal;
 
-  auto Diag =
-      diag(Expression->getExprLoc(),
-           "converting integer literal to bool, use bool literal instead");
+    auto Diag =
+        diag(Expression->getExprLoc(),
+             "converting integer literal to bool, use bool literal instead");
 
-  if (!Expression->getLocStart().isMacroID())
-    Diag << FixItHint::CreateReplacement(
-        Expression->getSourceRange(), LiteralBooleanValue ? "true" : "false");
+    if (!Expression->getLocStart().isMacroID())
+      Diag << FixItHint::CreateReplacement(
+          Expression->getSourceRange(), LiteralBooleanValue ? "true" : "false");
+  }
 }
 
 } // namespace modernize


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23243.67089.patch
Type: text/x-patch
Size: 3264 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160806/00a20f74/attachment.bin>


More information about the cfe-commits mailing list