This is an archive of the discontinued LLVM Phabricator instance.

Add check for unneeded copies of locals
ClosedPublic

Authored by fowles on Mar 14 2016, 8:45 AM.

Details

Summary

Extends the UnnecessaryCopyInitialization to detect copies of local variables and parameters that are unneeded.

Diff Detail

Repository
rL LLVM

Event Timeline

fowles updated this revision to Diff 50602.Mar 14 2016, 8:45 AM
fowles retitled this revision from to Add check for unneeded copies of locals.
fowles updated this object.
alexfh edited edge metadata.Mar 15 2016, 6:55 AM

Please always add 'cfe-commits' to Subscribers when you send a patch for review (I've added it now).

Substantial comments later.

Thank you for the patch! Looks mostly good, a few style nits.

clang-tidy/performance/UnnecessaryCopyInitialization.cpp
21 ↗(On Diff #50602)

No need for clang::tidy:: here.

43 ↗(On Diff #50602)

I guess, there's no need for ast_matchers:: here.

127 ↗(On Diff #50602)

nit: I'd use a semicolon instead of the comma here, like in other clang-tidy messages.

clang-tidy/performance/UnnecessaryCopyInitialization.h
29 ↗(On Diff #50602)

LLVM style is to put *'s and &'s to the variable. Looks like your clang-format didn't understand it should use LLVM style. You might want to explicitly specify -style=llvm next time.

fowles marked 4 inline comments as done.Mar 16 2016, 2:51 PM
fowles updated this revision to Diff 50878.Mar 16 2016, 2:53 PM
fowles edited edge metadata.
fowles updated this revision to Diff 50879.Mar 16 2016, 2:59 PM
fowles updated this revision to Diff 50927.Mar 17 2016, 6:49 AM
hokein added inline comments.Mar 21 2016, 7:10 AM
clang-tidy/performance/UnnecessaryCopyInitialization.cpp
87 ↗(On Diff #50927)

I prefer to move the anonymous namespace under namespace performance { statement.

test/clang-tidy/performance-unnecessary-copy-initialization.cpp
232 ↗(On Diff #50927)

It would be nice to add a test case with copy_2 like:

ExpensiveToCopyType orig;
ExpensiveToCopyType copy_1 = orig;
ExpensiveToCopyType copy_2 = copy_1;
copy_1.constMethod();
copy_2.constMethod();
orig.constMethod();
fowles updated this revision to Diff 51215.Mar 21 2016, 12:45 PM
fowles marked 2 inline comments as done.
hokein accepted this revision.Mar 22 2016, 6:35 AM
hokein edited edge metadata.

LGTM.

This revision is now accepted and ready to land.Mar 22 2016, 6:35 AM
alexfh accepted this revision.Mar 22 2016, 7:58 AM
alexfh edited edge metadata.

LG

clang-tidy/performance/UnnecessaryCopyInitialization.cpp
21 ↗(On Diff #51215)

nit: Alternatively, you could return llvm::Optional<FixItHint> and apply it in the caller. Might result in a bit better separation of responsibilities.

fowles updated this revision to Diff 51289.Mar 22 2016, 8:01 AM
fowles edited edge metadata.

rebased to latest. Can someone submit this for me I don't have commit bits.

fowles added inline comments.Mar 22 2016, 8:02 AM
clang-tidy/performance/UnnecessaryCopyInitialization.cpp
21 ↗(On Diff #51289)

I am inclined to just leave it as is for the moment, this is already isolated code that doesn't escape the current file.

This revision was automatically updated to reflect the committed changes.

@fowles, I have commit the patch for you.