[PATCH] D26125: [clang-tidy] Fixed else-after-return warning in cascade if statement

Paweł Żukowski via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 30 01:30:49 PDT 2016


idlecode created this revision.
idlecode added reviewers: djasper, malcolm.parsons, omtcyfz.
idlecode added a subscriber: cfe-commits.

Fix applied to else in a cascade if statements changed
program semantics in cases when not all previous branches
resulted in flow change.

Fixes PR30652


https://reviews.llvm.org/D26125

Files:
  clang-tidy/readability/ElseAfterReturnCheck.cpp
  test/clang-tidy/readability-else-after-return.cpp


Index: test/clang-tidy/readability-else-after-return.cpp
===================================================================
--- test/clang-tidy/readability-else-after-return.cpp
+++ test/clang-tidy/readability-else-after-return.cpp
@@ -29,32 +29,60 @@
   else if (a > 10)
     return;
   else // comment-2
-  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use 'else' after 'return'
-  // CHECK-FIXES: {{^}}  // comment-2
+  // CHECK-FIXES-NOT: {{^}}  // comment-2
     f(0);
+
+  if (a > 0)
+    if (a < 10)
+      return;
+    else // comment-3
+    // CHECK-FIXES-NOT: {{^}}    // comment-3
+      f(0);
+  else
+    if (a > 10)
+      return;
+    else // comment-4
+    // CHECK-FIXES-NOT: {{^}}    // comment-4
+      f(0);
+
+  if (a > 0) {
+    if (a < 10)
+      return;
+    else // comment-5
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use 'else' after 'return'
+    // CHECK-FIXES: {{^}}    // comment-5
+      f(0);
+  } else {
+    if (a > 10)
+      return;
+    else // comment-6
+    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use 'else' after 'return'
+    // CHECK-FIXES: {{^}}    // comment-6
+      f(0);
+  }
 }
 
 void foo() {
   for (unsigned x = 0; x < 42; ++x) {
     if (x) {
       continue;
-    } else { // comment-3
+    } else { // comment-7
     // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use 'else' after 'continue'
-    // CHECK-FIXES: {{^}}    } // comment-3
+    // CHECK-FIXES: {{^}}    } // comment-7
       x++;
     }
     if (x) {
       break;
-    } else { // comment-4
+    } else { // comment-8
     // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use 'else' after 'break'
-    // CHECK-FIXES: {{^}}    } // comment-4
+    // CHECK-FIXES: {{^}}    } // comment-8
       x++;
     }
     if (x) {
       throw 42;
-    } else { // comment-5
+    } else { // comment-9
     // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use 'else' after 'throw'
-    // CHECK-FIXES: {{^}}    } // comment-5
+    // CHECK-FIXES: {{^}}    } // comment-9
       x++;
     }
   }
Index: clang-tidy/readability/ElseAfterReturnCheck.cpp
===================================================================
--- clang-tidy/readability/ElseAfterReturnCheck.cpp
+++ clang-tidy/readability/ElseAfterReturnCheck.cpp
@@ -22,9 +22,11 @@
   const auto ControlFlowInterruptorMatcher =
       stmt(anyOf(returnStmt().bind("return"), continueStmt().bind("continue"),
                  breakStmt().bind("break"), cxxThrowExpr().bind("throw")));
+
   Finder->addMatcher(
       stmt(forEach(
-          ifStmt(hasThen(stmt(
+          ifStmt(unless(hasParent(ifStmt())),
+                 hasThen(stmt(
                      anyOf(ControlFlowInterruptorMatcher,
                            compoundStmt(has(ControlFlowInterruptorMatcher))))),
                  hasElse(stmt().bind("else")))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26125.76328.patch
Type: text/x-patch
Size: 2837 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161030/979b6450/attachment-0001.bin>


More information about the cfe-commits mailing list