[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
Tue Aug 22 08:47:10 PDT 2017


tbourvon added a comment.

Some more precisions I kept out of the revision body for clarity:

The checker contains a configuration option to set the maximum line length for the fixed return statement, so that we make sure this checker always contributes to improving readability and not the contrary.
The top-most non-trivial expression of the return statement has to be a comparison operator.

This checker tries to catch as many corner cases as possible. Most if not all of them are showcased in the tests, but the main ones are:

- It handles double variable declarations like this:

  auto Var1 = 1;
  auto Var2 = 2;
  return (Var1 < Var2);

so that the checker doesn't have to be run twice to fix these cases.

- It always preserves order of execution:

  auto Var = step1();
  return (step2() > Var);

will correctly be transformed into:

  return (step1() < step2()); // notice the comparison operator inversion



- It never duplicates code execution:

  auto Var = foo();
  return (Var == Var);

will not be matched.

I did not feel it was a good idea to include this in the user-facing checker documentation because it seems to me that the user of the checker does not need to worry about these exceptions as long as they are covered. The user should only be interested in the idea behind the checker and how it can help.


https://reviews.llvm.org/D37014





More information about the cfe-commits mailing list