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

Daniel Jasper djasper at google.com
Wed Jan 14 11:36:57 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"),
----------------
alexfh wrote:
> Please add a FIXME to support continue, break and throw.
Done.

================
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");
----------------
alexfh wrote:
> Looks like a debug code. Is it needed?
Done.

================
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());
----------------
alexfh wrote:
> const auto *CS?
Done.

================
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());
+}
----------------
alexfh wrote:
> 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.
If it doesn't work, I actually want to fix clang-format to handle this correctly. Might come up in other workflows, too and we want it to work anywhere (git clang-format, etc.).

http://reviews.llvm.org/D6927

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






More information about the cfe-commits mailing list