[clang-tools-extra] r351751 - [clang-tidy] Work around http://llvm.org/PR40392

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 21 08:26:54 PST 2019


Author: alexfh
Date: Mon Jan 21 08:26:54 2019
New Revision: 351751

URL: http://llvm.org/viewvc/llvm-project?rev=351751&view=rev
Log:
[clang-tidy] Work around http://llvm.org/PR40392

The readability-else-after-return check should be smarter about cases where the
variable defined in the condition is used in the `else` branch. This patch makes
it just ignore such cases, but alternative solutions may be better (added a
FIXME).

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/ElseAfterReturnCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/readability-else-after-return.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/ElseAfterReturnCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/ElseAfterReturnCheck.cpp?rev=351751&r1=351750&r2=351751&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/ElseAfterReturnCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/ElseAfterReturnCheck.cpp Mon Jan 21 08:26:54 2019
@@ -18,16 +18,22 @@ namespace tidy {
 namespace readability {
 
 void ElseAfterReturnCheck::registerMatchers(MatchFinder *Finder) {
-  const auto ControlFlowInterruptorMatcher =
+  const auto InterruptsControlFlow =
       stmt(anyOf(returnStmt().bind("return"), continueStmt().bind("continue"),
                  breakStmt().bind("break"),
                  expr(ignoringImplicit(cxxThrowExpr().bind("throw")))));
   Finder->addMatcher(
       compoundStmt(forEach(
           ifStmt(unless(isConstexpr()),
-                 hasThen(stmt(
-                     anyOf(ControlFlowInterruptorMatcher,
-                           compoundStmt(has(ControlFlowInterruptorMatcher))))),
+                 // FIXME: Explore alternatives for the
+                 // `if (T x = ...) {... return; } else { <use x> }`
+                 // pattern:
+                 //   * warn, but don't fix;
+                 //   * fix by pulling out the variable declaration out of
+                 //     the condition.
+                 unless(hasConditionVariableStatement(anything())),
+                 hasThen(stmt(anyOf(InterruptsControlFlow,
+                                    compoundStmt(has(InterruptsControlFlow))))),
                  hasElse(stmt().bind("else")))
               .bind("if"))),
       this);

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-else-after-return.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-else-after-return.cpp?rev=351751&r1=351750&r2=351751&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-else-after-return.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-else-after-return.cpp Mon Jan 21 08:26:54 2019
@@ -105,3 +105,15 @@ void foo() {
     }
   }
 }
+
+extern int *g();
+extern void h(int **x);
+
+int *decl_in_condition() {
+  if (int *x = g()) {
+    return x;
+  } else {
+    h(&x);
+    return x;
+  }
+}




More information about the cfe-commits mailing list