[PATCH] clang-tidy: Add initial check for "Don't use else after return".

Alexander Kornienko alexfh at google.com
Wed Jan 14 11:28:06 PST 2015


================
Comment at: clang-tidy/readability/ElseAfterReturnCheck.cpp:22
@@ +21,3 @@
+      ifStmt(
+          hasThen(stmt(anyOf(returnStmt(), compoundStmt(has(returnStmt()))))),
+          hasElse(stmt().bind("else"))).bind("if"),
----------------
Please add a FIXME to support continue, break and throw.

================
Comment at: clang-tidy/readability/ElseAfterReturnCheck.cpp:35
@@ +34,3 @@
+  if (ElseLoc.isInvalid())
+    If->dump();
+  DiagnosticBuilder Diag = diag(ElseLoc, "don't use else after return");
----------------
Looks like a debug code. Is it needed?

================
Comment at: clang-tidy/readability/ElseAfterReturnCheck.cpp:40
@@ +39,3 @@
+  // FIXME: Removing the braces isn't always safe. Do a more careful analysis.
+  if (const CompoundStmt *CS = Result.Nodes.getNodeAs<CompoundStmt>("else"))
+    Diag << removeToken(CS->getLBracLoc()) << removeToken(CS->getRBracLoc());
----------------
const auto *CS?

================
Comment at: clang-tidy/readability/ElseAfterReturnCheck.cpp:41
@@ +40,3 @@
+  if (const CompoundStmt *CS = Result.Nodes.getNodeAs<CompoundStmt>("else"))
+    Diag << removeToken(CS->getLBracLoc()) << removeToken(CS->getRBracLoc());
+}
----------------
Please add a FIXME to come up with a good way to ensure that the formatting is updated for the whole compound statement. I guess that the "format changed lines" strategy will fail here. Maybe we'll need to remove some leading whitespace from each line to ensure it's treated as changed.

http://reviews.llvm.org/D6927

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the cfe-commits mailing list