[PATCH] D100092: [clang-tidy] cppcoreguidelines-declare-loop-variable-in-the-initializer: a new check

Stephen Kelly via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 29 13:50:34 PDT 2021


steveire added a comment.

I implemented something like this recently too. The code I was trying to refactor was something like

  auto ii = 0;
  for (ii = 0; ii < thing.size(); ++ii)
  {
      // code
  }
  // code
  for (ii = 0; ii < thing.size(); ++ii)
  {
      // code
  }
  // code
  for (ii = 0; ii < thing.size(); ++ii)
  {
      // code
  }

ie, the variable was used repeatedly, but always initialized in the loop.

I also had code like

  auto ii = 0;
  for (ii = 0; ii < thing.size(); ++ii)
  {
      // code
  }
  ii = getNumber();
  doSomething(ii);

ie, the next statement outside the loop was an assignment, so I could just change it to

  for (auto ii = 0; ii < thing.size(); ++ii)
  {
      // code
  }
  auto ii = getNumber();
  doSomething(ii);

You don't necessarily have to handle these cases in the initial version of this check, but you could consider them in the future.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D100092/new/

https://reviews.llvm.org/D100092



More information about the cfe-commits mailing list