This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
ClosedPublic

Authored by kuhar on Apr 23 2017, 3:01 AM.

Details

Summary

When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.

Eg.

std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);

make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.

Diff Detail

Repository
rL LLVM

Event Timeline

kuhar created this revision.Apr 23 2017, 3:01 AM

please note this enhancement in the ReleaseNotes.

clang-tidy/modernize/UseEmplaceCheck.cpp
93 ↗(On Diff #96298)

is the new line here necessary? i think it looks better if the .bind is on this line.

97 ↗(On Diff #96298)

here, on line 100 and 89: shouldnt the matchers be upper case since they are variables? Iam unsure about that.

Prazek added inline comments.Apr 23 2017, 4:33 AM
clang-tidy/modernize/UseEmplaceCheck.cpp
93 ↗(On Diff #96298)

Better question is "is it clang formated?"

97 ↗(On Diff #96298)

True, all the matchers should have upper case, but it would be better to send it in separate patch

Prazek added inline comments.Apr 23 2017, 4:37 AM
test/clang-tidy/modernize-use-emplace.cpp
284 ↗(On Diff #96298)

I would add here test like:

class X {
  X(std:;pair<int, int> a) {}
};

std::vector<X> v;
v.push_back(make_pair(42, 42));

I guess as long as X ctor is not explicit this can happen, and we can't transform it to
emplace.back(42, 42)

kuhar updated this revision to Diff 96329.Apr 23 2017, 3:48 PM

Add test for pair constructor argument, mention changes in release notes.

kuhar marked 2 inline comments as done.Apr 23 2017, 3:51 PM
kuhar added inline comments.
clang-tidy/modernize/UseEmplaceCheck.cpp
93 ↗(On Diff #96298)

It was clang-formatted, but I do agree that it looked a bit weird. I removed the extra newline.

test/clang-tidy/modernize-use-emplace.cpp
284 ↗(On Diff #96298)

Nice idea for a test case, added.

kuhar updated this revision to Diff 96332.Apr 23 2017, 3:57 PM
kuhar marked an inline comment as done.

Update modernize-use-emplace rst docs.

kuhar added inline comments.Apr 24 2017, 4:28 AM
test/clang-tidy/modernize-use-emplace.cpp
284 ↗(On Diff #96298)

A better test case would be to make X's ctor take a pair by const reference or rvalue reference. This way it wouldn't produce an extra CxxConstructExpr and will be currently (incorrectly) matched. This needs to be fixed.

kuhar updated this revision to Diff 96384.Apr 24 2017, 5:22 AM

Don't remove make_pair calls when inserted type is different than std::pair.

kuhar marked 2 inline comments as done.Apr 24 2017, 5:23 AM
JDevlieghere added inline comments.
clang-tidy/modernize/UseEmplaceCheck.cpp
115 ↗(On Diff #96384)

It's highly recommended to put some kind of error message in the assertion statement, as per the LLVM coding standards.

130 ↗(On Diff #96384)

Can't we use the ternary operator here?

140 ↗(On Diff #96384)

Same here?

147 ↗(On Diff #96384)

I think this can be const.

kuhar updated this revision to Diff 96622.Apr 25 2017, 12:39 PM
kuhar marked 4 inline comments as done.

Added const where possible, moved from if-else to ternary.

Thanks for the suggestions, Jonas. Fixed.

Prazek added inline comments.Apr 25 2017, 1:43 PM
clang-tidy/modernize/UseEmplaceCheck.cpp
22 ↗(On Diff #96622)

extend the anonymous namespace to have static strings (and remove static)

90 ↗(On Diff #96622)

Why ignoringParenImpCasts instead of ingnoringImplicit?

115 ↗(On Diff #96384)

would it be better to change it to
!innerCtorCall && !MakePairCall && "No .."

kuhar updated this revision to Diff 96634.Apr 25 2017, 2:38 PM

Use igoringImplicit instead of ignoringParenImpCasts. Options moved to the anonymous namespace.

kuhar marked 2 inline comments as done.Apr 25 2017, 2:42 PM
kuhar added inline comments.
clang-tidy/modernize/UseEmplaceCheck.cpp
115 ↗(On Diff #96384)

I don't think that this logic would work here. !first && !second ensures both are null.

Prazek added inline comments.Apr 25 2017, 3:05 PM
clang-tidy/modernize/UseEmplaceCheck.cpp
115 ↗(On Diff #96384)

Yea, I forgot how to De Morgan

kuhar added a comment.Apr 27 2017, 4:19 AM

I run clang-tidy with this version of modernize-use-emplace on the whole llvm + clang + clang tools extra tree and it emitted ~500 fixits, ~100 of them were make_pair ones.
The whole codebase compiled fine and there were no new test failures with check-all.

Prazek accepted this revision.Apr 27 2017, 12:33 PM

LGTM

This revision is now accepted and ready to land.Apr 27 2017, 12:33 PM
This revision was automatically updated to reflect the committed changes.