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

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 8 07:55:10 PDT 2016


omtcyfz updated this revision to Diff 67169.
omtcyfz marked an inline comment as done.
omtcyfz added a comment.

Use shiny `eachOf` and bind everything to `"literal"` value.


https://reviews.llvm.org/D23243

Files:
  clang-tidy/modernize/UseBoolLiteralsCheck.cpp
  docs/clang-tidy/checks/modernize-use-bool-literals.rst
  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,33 @@
   // 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;{{$}}
+}
+
+void foo() {
+  bool Result;
+  Result = Value == 1 ? true : 0;
+  // CHECK-MESSAGES: :[[@LINE-1]]:32: {{.*}}
+  // CHECK-FIXES: {{^ *}}Result = Value == 1 ? true : false;{{$}}
+  Result = Value == 1 ? false : bool(0);
+  // CHECK-MESSAGES: :[[@LINE-1]]:33: {{.*}}
+  // CHECK-FIXES: {{^ *}}Result = Value == 1 ? false : false;{{$}}
+  Result = Value == 1 ? (bool)0 : false;
+  // CHECK-MESSAGES: :[[@LINE-1]]:25: {{.*}}
+  // CHECK-FIXES: {{^ *}}Result = Value == 1 ? false : false;{{$}}
+}
Index: docs/clang-tidy/checks/modernize-use-bool-literals.rst
===================================================================
--- docs/clang-tidy/checks/modernize-use-bool-literals.rst
+++ docs/clang-tidy/checks/modernize-use-bool-literals.rst
@@ -10,9 +10,11 @@
   bool p = 1;
   bool f = static_cast<bool>(1);
   std::ios_base::sync_with_stdio(0);
+  bool x = p ? 1 : 0;
 
   // transforms to
 
   bool p = true;
   bool f = true;
   std::ios_base::sync_with_stdio(false);
+  bool x = p ? true : false;
Index: clang-tidy/modernize/UseBoolLiteralsCheck.cpp
===================================================================
--- clang-tidy/modernize/UseBoolLiteralsCheck.cpp
+++ clang-tidy/modernize/UseBoolLiteralsCheck.cpp
@@ -29,10 +29,31 @@
           unless(isInTemplateInstantiation()),
           anyOf(hasParent(explicitCastExpr().bind("cast")), anything())),
       this);
+
+  Finder->addMatcher(
+      conditionalOperator(
+          hasParent(implicitCastExpr(
+              hasImplicitDestinationType(qualType(booleanType())),
+              unless(isInTemplateInstantiation()))),
+          eachOf(
+              // anyOf() is not evaluating second argument if first is evaluated
+              // as
+              // 'true'. We'd like to bind both literals if they are presents.
+              // Thus,
+              // using anyOf(BINDLITERAL(), anything()).
+              anyOf(hasTrueExpression(ignoringParenImpCasts(
+                        integerLiteral().bind("literal"))),
+                    anything()),
+              anyOf(hasFalseExpression(ignoringParenImpCasts(
+                        integerLiteral().bind("literal"))),
+                    anything()))),
+      this);
 }
 
 void UseBoolLiteralsCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>("literal");
+  if (!Literal)
+    return;
   const auto *Cast = Result.Nodes.getNodeAs<Expr>("cast");
   bool LiteralBooleanValue = Literal->getValue().getBoolValue();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23243.67169.patch
Type: text/x-patch
Size: 3362 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160808/3a676d82/attachment.bin>


More information about the cfe-commits mailing list