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

Alexander Kornienko alexfh at google.com
Tue Jan 13 08:20:41 PST 2015

The rule applies to other control statements as well, such as break, continue and throw (may be also goto, but who cares about this rule when a goto is used?). Can we handle them in the same check? Then the name may need to be changed.

Comment at: clang-tidy/misc/ElseAfterReturnCheck.cpp:34
@@ +33,3 @@
+  SourceLocation ElseLoc = If->getElseLoc();
+  if (ElseLoc.isInvalid()) If->dump();
+  DiagnosticBuilder Diag = diag(ElseLoc, "don't use else here");

Comment at: clang-tidy/misc/ElseAfterReturnCheck.cpp:35
@@ +34,3 @@
+  if (ElseLoc.isInvalid()) If->dump();
+  DiagnosticBuilder Diag = diag(ElseLoc, "don't use else here");
+  Diag << removeToken(ElseLoc);
Maybe explain what "here" means? How about "don't use else after a return"?

Comment at: clang-tidy/misc/MiscTidyModule.cpp:32
@@ -30,1 +31,3 @@
+    CheckFactories.registerCheck<ElseAfterReturnCheck>(
+        "misc-else-after-return");
It's definitely a readability check, and it's style-specific. Thus, I think, it's better to put this check in the readability/ directory and then include in the styles it's relevant to (currently, llvm, maybe also google, if a similar rule is added there).

Comment at: test/clang-tidy/misc-else-after-return.cpp:15
@@ +14,3 @@
+  } else { // comment
+// CHECK-MESSAGES: warning: don't use else here
+// CHECK-FIXES:  } // comment
Please add a line number to avoid incorrect matches.



More information about the cfe-commits mailing list