[PATCH] D142939: Fix handling of -> calls for modernize-use-emplace
Peter Wolf via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 30 15:08:25 PST 2023
BigPeet created this revision.
Herald added a subscriber: carlosgalvezp.
Herald added a reviewer: njames93.
Herald added a project: All.
BigPeet requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
Fixes #55869
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D142939
Files:
clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp
@@ -1334,3 +1334,40 @@
v3.push_back({{0}});
v3.push_back({{}});
}
+
+void testWithPointerTypes() {
+ std::vector<Something> v;
+ std::vector<Something>* vp = &v;
+
+ vp->push_back(Something(1, 2));
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_back instead of push_back [modernize-use-emplace]
+ // CHECK-FIXES: vp->emplace_back(1, 2);
+
+ vp->push_back(Something{1, 2});
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_back instead of push_back [modernize-use-emplace]
+ // CHECK-FIXES: vp->emplace_back(1, 2);
+
+ vp->push_back(Something());
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_back instead of push_back [modernize-use-emplace]
+ // CHECK-FIXES: vp->emplace_back();
+
+ vp->push_back(Something{});
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_back instead of push_back [modernize-use-emplace]
+ // CHECK-FIXES: vp->emplace_back();
+
+ vp->emplace_back(Something(1, 2));
+ // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace_back
+ // CHECK-FIXES: vp->emplace_back(1, 2);
+
+ vp->emplace_back(Something{1, 2});
+ // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace_back
+ // CHECK-FIXES: vp->emplace_back(1, 2);
+
+ vp->emplace_back(Something());
+ // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace_back
+ // CHECK-FIXES: vp->emplace_back();
+
+ vp->emplace_back(Something{});
+ // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace_back
+ // CHECK-FIXES: vp->emplace_back();
+}
Index: clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp
@@ -130,27 +130,35 @@
// because this requires special treatment (it could cause performance
// regression)
// + match for emplace calls that should be replaced with insertion
+
+ auto PushBackTypes = hasCanonicalType(
+ hasDeclaration(cxxRecordDecl(hasAnyName(ContainersWithPushBack))));
auto CallPushBack = cxxMemberCallExpr(
hasDeclaration(functionDecl(hasName("push_back"))),
- on(hasType(hasCanonicalType(
- hasDeclaration(cxxRecordDecl(hasAnyName(ContainersWithPushBack)))))));
+ on(anyOf(hasType(PushBackTypes),
+ hasType(pointerType(pointee(PushBackTypes))))));
- auto CallPush =
- cxxMemberCallExpr(hasDeclaration(functionDecl(hasName("push"))),
- on(hasType(hasCanonicalType(hasDeclaration(
- cxxRecordDecl(hasAnyName(ContainersWithPush)))))));
+ auto PushTypes = hasCanonicalType(
+ hasDeclaration(cxxRecordDecl(hasAnyName(ContainersWithPush))));
+ auto CallPush = cxxMemberCallExpr(
+ hasDeclaration(functionDecl(hasName("push"))),
+ on(anyOf(hasType(PushTypes), hasType(pointerType(pointee(PushTypes))))));
+ auto PushFrontTypes = hasCanonicalType(
+ hasDeclaration(cxxRecordDecl(hasAnyName(ContainersWithPushFront))));
auto CallPushFront = cxxMemberCallExpr(
hasDeclaration(functionDecl(hasName("push_front"))),
- on(hasType(hasCanonicalType(hasDeclaration(
- cxxRecordDecl(hasAnyName(ContainersWithPushFront)))))));
-
- auto CallEmplacy = cxxMemberCallExpr(
- hasDeclaration(
- functionDecl(hasAnyNameIgnoringTemplates(EmplacyFunctions))),
- on(hasType(hasCanonicalType(hasDeclaration(has(typedefNameDecl(
- hasName("value_type"), hasType(type(hasUnqualifiedDesugaredType(
- recordType().bind("value_type")))))))))));
+ on(anyOf(hasType(PushFrontTypes),
+ hasType(pointerType(pointee(PushFrontTypes))))));
+
+ auto EmplacyTypes = hasCanonicalType(hasDeclaration(has(typedefNameDecl(
+ hasName("value_type"), hasType(type(hasUnqualifiedDesugaredType(
+ recordType().bind("value_type"))))))));
+ auto CallEmplacy =
+ cxxMemberCallExpr(hasDeclaration(functionDecl(
+ hasAnyNameIgnoringTemplates(EmplacyFunctions))),
+ on(anyOf(hasType(EmplacyTypes),
+ hasType(pointerType(pointee(EmplacyTypes))))));
// We can't replace push_backs of smart pointer because
// if emplacement fails (f.e. bad_alloc in vector) we will have leak of
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142939.493432.patch
Type: text/x-patch
Size: 4851 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230130/644d6f7c/attachment-0001.bin>
More information about the cfe-commits
mailing list