[PATCH] D37014: [clang-tidy] Add a checker to remove useless intermediate variables before return statements with comparisons

Tristan Bourvon via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 23 06:46:23 PDT 2017

tbourvon marked 14 inline comments as done.
tbourvon added inline comments.

Comment at: clang-tidy/readability/UselessIntermediateVarCheck.h:29
+      : ClangTidyCheck(Name, Context),
+        MaximumLineLength(Options.get("MaximumLineLength", 100)) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
JonasToth wrote:
> It would be nice, if there is a way to get this information from a clang-format file. 
There is no easy way to get this information unless you want to use all the LibFormat stuff that includes finding the .clang-format file, etc... It doesn't seem like it is the responsibility of this checker to take care of that.

Comment at: test/clang-tidy/readability-useless-intermediate-var.cpp:1
+// RUN: %check_clang_tidy %s readability-useless-intermediate-var %t
JonasToth wrote:
> the tests seem to be to less compared to the code volume of the check.
> situations i think should be tested:
> - initialization from a function, method call
> - initialization with a lambda
> ```
> const auto SomeVar = []() { /* super complicated stuff */ return Result; } ();
> return SomeVar;
> ```
> - template stuff -> proof that they work two, even thought it doesnt seem to be relevant, at least what i can see.
> - what happens if a "temporary" is used as an argument for a function call?
> ```
> const auto Var = std::sqrt(10);
> return std::pow(Var, 10);
> ```
> - proof that really long function calls (like STL Algorithm tends to be) are not inlined as well
Your 4th point is not something possible with this check. For now it only looks at `return` statements with comparisons.
As for the 5th, it doesn't really matter what the expression we are looking at is, so this case is already covered by the very long string literal in the last test.


More information about the cfe-commits mailing list