[PATCH] D92179: [clang-tidy] Catch more unwanted implicit conversions in performance-implicit-conversion-in-loop

Clement Courbet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 26 06:48:37 PST 2020


courbet created this revision.
courbet added a reviewer: pilki.
Herald added subscribers: llvm-commits, xazax.hun, mgorny.
Herald added projects: clang, LLVM.
courbet requested review of this revision.

It turns out that there is no reason to restrict to loop variables. This
allows catching stuff like:

  const std::pair<uint32, SomeLargeObject>& kv = *map.begin();

(did you notice the missing `const uint32_t`) ?

On our codebase, this roughly doubles the number of warnings compared to
the loop-only case.

The warnings mainly fall in two categories:

1. Lifetime extended temporary:

  const std::function<...>& c = [](...) {};

Which is better written as one of:

  const std::function<...> c = [](...) {};  // explicit
  const auto c = [](...) {};  // no std::function



2. Copy + lifetime extension.

  const std::pair<uint32, SomeLargeObject>& kv = *map.begin();

Better written as:

  // no copy:
  const std::pair<const uint32, SomeLargeObject>& kv = *map.begin();
  const auto& kv = *map.begin();
  // explicit
  const std::pair<uint32, SomeLargeObject> kv = *map.begin();
  // explicit, allows automatic move.
  std::pair<uint32, SomeLargeObject> kv = *map.begin();

And rename the check.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92179

Files:
  clang-tools-extra/clang-tidy/performance/CMakeLists.txt
  clang-tools-extra/clang-tidy/performance/ImplicitConversionCheck.cpp
  clang-tools-extra/clang-tidy/performance/ImplicitConversionCheck.h
  clang-tools-extra/clang-tidy/performance/ImplicitConversionInLoopCheck.cpp
  clang-tools-extra/clang-tidy/performance/ImplicitConversionInLoopCheck.h
  clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/performance-implicit-cast-in-loop.rst
  clang-tools-extra/docs/clang-tidy/checks/performance-implicit-conversion-in-loop.rst
  clang-tools-extra/docs/clang-tidy/checks/performance-implicit-conversion.rst
  clang-tools-extra/test/clang-tidy/checkers/performance-implicit-conversion-in-loop.cpp
  llvm/utils/gn/secondary/clang-tools-extra/clang-tidy/performance/BUILD.gn

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92179.307860.patch
Type: text/x-patch
Size: 17097 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201126/c23710bf/attachment.bin>


More information about the llvm-commits mailing list