This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] performance-range-for-copy only for copy.
AcceptedPublic

Authored by EricWF on Apr 15 2020, 11:22 AM.

Details

Summary

Currently the range-for-copy incorrectly suggests changing a

by-value loop var to a reference to avoid copies even when:

(1) A converting constructor was used. Or,
(2) The argument to the copy constructor is not an lvalue.

For example:

for (const std::string sv : std::vector<const char*>{}) {
  // warning:
  //  the loop variable's type is not a reference type;
  //  this creates a copy in each iteration;
  //  consider making this a reference
}

In these cases we can't actually avoid the copy, and reference
binding + lifetime extending a temporary is not a "fix" we should
be suggesting.

Admittedly, cases like the example should be fixed by the clang-tidy
user, but at minimum we need to be clearer about when a copy is made
and when a user defined conversion occurs (and that conversion may
be semantically important).

Diff Detail

Event Timeline

EricWF created this revision.Apr 15 2020, 11:22 AM
fowles accepted this revision.Apr 15 2020, 12:31 PM
This revision is now accepted and ready to land.Apr 15 2020, 12:31 PM
aaron.ballman accepted this revision.Apr 27 2020, 2:09 PM

LGTM aside from some small nits

clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp
68

Formatting is off here, so run through clang-format?

clang-tools-extra/test/clang-tidy/checkers/performance-for-range-copy.cpp
50

Spurious whitespace change?

xgupta added a subscriber: xgupta.Jul 23 2023, 7:34 AM

Reverse ping, this patch was never gets committed after being accepted.

Herald added a project: Restricted Project. · View Herald TranscriptJul 23 2023, 7:34 AM

Release notes are missing, and patch not not apply gracefully.

PiotrZSL changed the repository for this revision from rCTE Clang Tools Extra to rG LLVM Github Monorepo.Jul 23 2023, 8:10 AM
PiotrZSL added inline comments.Jul 23 2023, 8:26 AM
clang-tools-extra/test/clang-tidy/checkers/performance-for-range-copy.cpp
66–67

Those tests already exist and pass in current version.
Most probably this change is obsolete.