https://github.com/komalverma04 updated https://github.com/llvm/llvm-project/pull/87268
>From 9b5781108081565e4009c3809eab623387655f1c Mon Sep 17 00:00:00 2001 From: komalverma04 <komal148bti...@igdtuw.ac.in> Date: Mon, 1 Apr 2024 22:43:10 +0530 Subject: [PATCH 1/7] [clang-tidy] Add ignoringParenImpCasts in hasAnyArgument --- .../bugprone/MisplacedOperatorInStrlenInAllocCheck.cpp | 2 +- .../clang-tidy/bugprone/MisplacedWideningCastCheck.cpp | 2 +- .../bugprone/MultipleNewInOneExpressionCheck.cpp | 8 ++++---- .../clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp | 2 +- .../bugprone/StringLiteralWithEmbeddedNulCheck.cpp | 2 +- .../bugprone/SuspiciousStringviewDataUsageCheck.cpp | 2 +- .../clang-tidy/cppcoreguidelines/OwningMemoryCheck.cpp | 4 ++-- .../clang-tidy/modernize/UseEmplaceCheck.cpp | 6 +++--- .../performance/InefficientStringConcatenationCheck.cpp | 4 ++-- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/MisplacedOperatorInStrlenInAllocCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/MisplacedOperatorInStrlenInAllocCheck.cpp index 40e4ab6c8b12af..415183d5c57ba7 100644 --- a/clang-tools-extra/clang-tidy/bugprone/MisplacedOperatorInStrlenInAllocCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/MisplacedOperatorInStrlenInAllocCheck.cpp @@ -24,7 +24,7 @@ void MisplacedOperatorInStrlenInAllocCheck::registerMatchers( const auto BadUse = callExpr(callee(StrLenFunc), - hasAnyArgument(ignoringImpCasts( + hasAnyArgument(ignoringParenImpCasts( binaryOperator( hasOperatorName("+"), hasRHS(ignoringParenImpCasts(integerLiteral(equals(1))))) diff --git a/clang-tools-extra/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp index a1f92aae55448c..b62829a3776572 100644 --- a/clang-tools-extra/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp @@ -42,7 +42,7 @@ void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher(varDecl(hasInitializer(Cast)), this); Finder->addMatcher(returnStmt(hasReturnValue(Cast)), this); - Finder->addMatcher(callExpr(hasAnyArgument(Cast)), this); + Finder->addMatcher(callExpr(hasAnyArgument(ignoringParenImpCasts(Cast))), this); Finder->addMatcher(binaryOperator(hasOperatorName("="), hasRHS(Cast)), this); Finder->addMatcher( binaryOperator(isComparisonOperator(), hasEitherOperand(Cast)), this); diff --git a/clang-tools-extra/clang-tidy/bugprone/MultipleNewInOneExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/MultipleNewInOneExpressionCheck.cpp index 41191a3cfed23a..b8dbea600fd368 100644 --- a/clang-tools-extra/clang-tidy/bugprone/MultipleNewInOneExpressionCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/MultipleNewInOneExpressionCheck.cpp @@ -96,17 +96,17 @@ void MultipleNewInOneExpressionCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( callExpr( hasAnyArgument( - expr(HasNewExpr1).bind("arg1")), + ignoringParenImpCasts(expr(HasNewExpr1).bind("arg1"))), hasAnyArgument( - expr(HasNewExpr2, unless(equalsBoundNode("arg1"))).bind("arg2")), + ignoringParenImpCasts(expr(HasNewExpr2, unless(equalsBoundNode("arg1"))).bind("arg2"))), hasAncestor(BadAllocCatchingTryBlock)), this); Finder->addMatcher( cxxConstructExpr( hasAnyArgument( - expr(HasNewExpr1).bind("arg1")), + ignoringParenImpCasts(expr(HasNewExpr1).bind("arg1"))), hasAnyArgument( - expr(HasNewExpr2, unless(equalsBoundNode("arg1"))).bind("arg2")), + ignoringParenImpCasts(expr(HasNewExpr2, unless(equalsBoundNode("arg1"))).bind("arg2"))), unless(isListInitialization()), hasAncestor(BadAllocCatchingTryBlock)), this); diff --git a/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp index 977241e91b9a93..d322f2488f8082 100644 --- a/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp @@ -621,7 +621,7 @@ void NotNullTerminatedResultCheck::registerMatchers(MatchFinder *Finder) { auto MallocLengthExpr = allOf( callee(functionDecl( hasAnyName("::alloca", "::calloc", "malloc", "realloc"))), - hasAnyArgument(allOf(unless(SizeExpr), expr().bind(DestMallocExprName)))); + hasAnyArgument(ignoringParenImpCasts(allOf(unless(SizeExpr), expr().bind(DestMallocExprName))))); // - Example: (char *)malloc(length); auto DestMalloc = anyOf(callExpr(MallocLengthExpr), diff --git a/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp index 72e680d25cb846..18a9dc6c430159 100644 --- a/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp @@ -52,7 +52,7 @@ void StringLiteralWithEmbeddedNulCheck::registerMatchers(MatchFinder *Finder) { this); // Detect passing a suspicious string literal through an overloaded operator. - Finder->addMatcher(cxxOperatorCallExpr(hasAnyArgument(StrLitWithNul)), this); + Finder->addMatcher(cxxOperatorCallExpr(hasAnyArgument(ignoringParenImpCasts(StrLitWithNul))), this); } void StringLiteralWithEmbeddedNulCheck::check( diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousStringviewDataUsageCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousStringviewDataUsageCheck.cpp index 8f4b0c5e0dceda..183d7c4bfb5b15 100644 --- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousStringviewDataUsageCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousStringviewDataUsageCheck.cpp @@ -73,7 +73,7 @@ void SuspiciousStringviewDataUsageCheck::registerMatchers(MatchFinder *Finder) { hasAnyArgument( ignoringParenImpCasts(equalsBoundNode("data-call"))), unless(hasAnyArgument(ignoringParenImpCasts(SizeCall))), - unless(hasAnyArgument(DescendantSizeCall)), + unless(hasAnyArgument(ignoringParenImpCasts(DescendantSizeCall))), hasDeclaration(namedDecl( unless(matchers::matchesAnyListedName(AllowedCallees))))), initListExpr(expr().bind("parent"), diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.cpp index 9b4d2ef99e5bf1..4b26949d2ca899 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/OwningMemoryCheck.cpp @@ -86,9 +86,9 @@ void OwningMemoryCheck::registerMatchers(MatchFinder *Finder) { // functions shall be 'gsl::owner<>'. Finder->addMatcher( traverse(TK_AsIs, callExpr(callee(LegacyOwnerConsumers), - hasAnyArgument(expr( + hasAnyArgument(ignoringParenImpCasts(expr( unless(ignoringImpCasts(ConsideredOwner)), - hasType(pointerType())))) + hasType(pointerType()))))) .bind("legacy_consumer")), this); diff --git a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp index 430455a38f395e..3055948fc3ad13 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp @@ -178,15 +178,15 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { // Bitfields binds only to consts and emplace_back take it by universal ref. auto BitFieldAsArgument = hasAnyArgument( - ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField()))))); + ignoringParenImpCasts(memberExpr(hasDeclaration(fieldDecl(isBitField()))))); // Initializer list can't be passed to universal reference. auto InitializerListAsArgument = hasAnyArgument( - ignoringImplicit(allOf(cxxConstructExpr(isListInitialization()), + ignoringParenImpCasts(allOf(cxxConstructExpr(isListInitialization()), unless(cxxTemporaryObjectExpr())))); // We could have leak of resource. - auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr())); + auto NewExprAsArgument = hasAnyArgument(ignoringParenImpCasts(cxxNewExpr())); // We would call another constructor. auto ConstructingDerived = hasParent(implicitCastExpr(hasCastKind(CastKind::CK_DerivedToBase))); diff --git a/clang-tools-extra/clang-tidy/performance/InefficientStringConcatenationCheck.cpp b/clang-tools-extra/clang-tidy/performance/InefficientStringConcatenationCheck.cpp index 9e4e3f63e19cfe..498fbecd2baa59 100644 --- a/clang-tools-extra/clang-tidy/performance/InefficientStringConcatenationCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/InefficientStringConcatenationCheck.cpp @@ -32,12 +32,12 @@ void InefficientStringConcatenationCheck::registerMatchers( const auto BasicStringPlusOperator = cxxOperatorCallExpr( hasOverloadedOperatorName("+"), - hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType)))); + hasAnyArgument(ignoringParenImpCasts(declRefExpr(BasicStringType)))); const auto PlusOperator = cxxOperatorCallExpr( hasOverloadedOperatorName("+"), - hasAnyArgument(ignoringImpCasts(declRefExpr(BasicStringType))), + hasAnyArgument(ignoringParenImpCasts(declRefExpr(BasicStringType))), hasDescendant(BasicStringPlusOperator)) .bind("plusOperator"); >From 57a5c96300de270bbce51030ad406aca4cb6c431 Mon Sep 17 00:00:00 2001 From: komalverma04 <komal148bti...@igdtuw.ac.in> Date: Tue, 2 Apr 2024 01:52:34 +0530 Subject: [PATCH 2/7] fix formatting --- .../bugprone/MisplacedWideningCastCheck.cpp | 3 ++- .../bugprone/MultipleNewInOneExpressionCheck.cpp | 14 ++++++-------- .../bugprone/NotNullTerminatedResultCheck.cpp | 9 +++++---- .../bugprone/StringLiteralWithEmbeddedNulCheck.cpp | 4 +++- .../SuspiciousStringviewDataUsageCheck.cpp | 3 ++- .../clang-tidy/modernize/UseEmplaceCheck.cpp | 6 +++--- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp index b62829a3776572..f989e927871ace 100644 --- a/clang-tools-extra/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/MisplacedWideningCastCheck.cpp @@ -42,7 +42,8 @@ void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher(varDecl(hasInitializer(Cast)), this); Finder->addMatcher(returnStmt(hasReturnValue(Cast)), this); - Finder->addMatcher(callExpr(hasAnyArgument(ignoringParenImpCasts(Cast))), this); + Finder->addMatcher(callExpr(hasAnyArgument(ignoringParenImpCasts(Cast))), + this); Finder->addMatcher(binaryOperator(hasOperatorName("="), hasRHS(Cast)), this); Finder->addMatcher( binaryOperator(isComparisonOperator(), hasEitherOperand(Cast)), this); diff --git a/clang-tools-extra/clang-tidy/bugprone/MultipleNewInOneExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/MultipleNewInOneExpressionCheck.cpp index b8dbea600fd368..d304ac0bd681a7 100644 --- a/clang-tools-extra/clang-tidy/bugprone/MultipleNewInOneExpressionCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/MultipleNewInOneExpressionCheck.cpp @@ -95,18 +95,16 @@ void MultipleNewInOneExpressionCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( callExpr( - hasAnyArgument( - ignoringParenImpCasts(expr(HasNewExpr1).bind("arg1"))), - hasAnyArgument( - ignoringParenImpCasts(expr(HasNewExpr2, unless(equalsBoundNode("arg1"))).bind("arg2"))), + hasAnyArgument(ignoringParenImpCasts(expr(HasNewExpr1).bind("arg1"))), + hasAnyArgument(ignoringParenImpCasts( + expr(HasNewExpr2, unless(equalsBoundNode("arg1"))).bind("arg2"))), hasAncestor(BadAllocCatchingTryBlock)), this); Finder->addMatcher( cxxConstructExpr( - hasAnyArgument( - ignoringParenImpCasts(expr(HasNewExpr1).bind("arg1"))), - hasAnyArgument( - ignoringParenImpCasts(expr(HasNewExpr2, unless(equalsBoundNode("arg1"))).bind("arg2"))), + hasAnyArgument(ignoringParenImpCasts(expr(HasNewExpr1).bind("arg1"))), + hasAnyArgument(ignoringParenImpCasts( + expr(HasNewExpr2, unless(equalsBoundNode("arg1"))).bind("arg2"))), unless(isListInitialization()), hasAncestor(BadAllocCatchingTryBlock)), this); diff --git a/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp index d322f2488f8082..3267744b25b7a8 100644 --- a/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp @@ -618,10 +618,11 @@ void NotNullTerminatedResultCheck::registerMatchers(MatchFinder *Finder) { // Note: Sometimes the size of char is explicitly written out. auto SizeExpr = anyOf(SizeOfCharExpr, integerLiteral(equals(1))); - auto MallocLengthExpr = allOf( - callee(functionDecl( - hasAnyName("::alloca", "::calloc", "malloc", "realloc"))), - hasAnyArgument(ignoringParenImpCasts(allOf(unless(SizeExpr), expr().bind(DestMallocExprName))))); + auto MallocLengthExpr = + allOf(callee(functionDecl( + hasAnyName("::alloca", "::calloc", "malloc", "realloc"))), + hasAnyArgument(ignoringParenImpCasts( + allOf(unless(SizeExpr), expr().bind(DestMallocExprName))))); // - Example: (char *)malloc(length); auto DestMalloc = anyOf(callExpr(MallocLengthExpr), diff --git a/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp index 18a9dc6c430159..37945b8a03e70f 100644 --- a/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp @@ -52,7 +52,9 @@ void StringLiteralWithEmbeddedNulCheck::registerMatchers(MatchFinder *Finder) { this); // Detect passing a suspicious string literal through an overloaded operator. - Finder->addMatcher(cxxOperatorCallExpr(hasAnyArgument(ignoringParenImpCasts(StrLitWithNul))), this); + Finder->addMatcher( + cxxOperatorCallExpr(hasAnyArgument(ignoringParenImpCasts(StrLitWithNul))), + this); } void StringLiteralWithEmbeddedNulCheck::check( diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousStringviewDataUsageCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousStringviewDataUsageCheck.cpp index 183d7c4bfb5b15..f71bc6df200caf 100644 --- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousStringviewDataUsageCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousStringviewDataUsageCheck.cpp @@ -73,7 +73,8 @@ void SuspiciousStringviewDataUsageCheck::registerMatchers(MatchFinder *Finder) { hasAnyArgument( ignoringParenImpCasts(equalsBoundNode("data-call"))), unless(hasAnyArgument(ignoringParenImpCasts(SizeCall))), - unless(hasAnyArgument(ignoringParenImpCasts(DescendantSizeCall))), + unless(hasAnyArgument( + ignoringParenImpCasts(DescendantSizeCall))), hasDeclaration(namedDecl( unless(matchers::matchesAnyListedName(AllowedCallees))))), initListExpr(expr().bind("parent"), diff --git a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp index 3055948fc3ad13..b95d826654342d 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseEmplaceCheck.cpp @@ -177,13 +177,13 @@ void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) { hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(SmartPointers)))); // Bitfields binds only to consts and emplace_back take it by universal ref. - auto BitFieldAsArgument = hasAnyArgument( - ignoringParenImpCasts(memberExpr(hasDeclaration(fieldDecl(isBitField()))))); + auto BitFieldAsArgument = hasAnyArgument(ignoringParenImpCasts( + memberExpr(hasDeclaration(fieldDecl(isBitField()))))); // Initializer list can't be passed to universal reference. auto InitializerListAsArgument = hasAnyArgument( ignoringParenImpCasts(allOf(cxxConstructExpr(isListInitialization()), - unless(cxxTemporaryObjectExpr())))); + unless(cxxTemporaryObjectExpr())))); // We could have leak of resource. auto NewExprAsArgument = hasAnyArgument(ignoringParenImpCasts(cxxNewExpr())); >From 7725072293839757b421eeae01d1555554b69579 Mon Sep 17 00:00:00 2001 From: komalverma04 <komal148bti...@igdtuw.ac.in> Date: Thu, 4 Apr 2024 14:46:32 +0530 Subject: [PATCH 3/7] fix tests --- .../bugprone/NotNullTerminatedResultCheck.cpp | 2 +- .../misplaced-operator-in-strlen-in-alloc.c | 20 +- .../checkers/modernize/use-emplace.cpp | 326 +++++++++--------- 3 files changed, 173 insertions(+), 175 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp index 3267744b25b7a8..264926de70353e 100644 --- a/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp @@ -618,7 +618,7 @@ void NotNullTerminatedResultCheck::registerMatchers(MatchFinder *Finder) { // Note: Sometimes the size of char is explicitly written out. auto SizeExpr = anyOf(SizeOfCharExpr, integerLiteral(equals(1))); - auto MallocLengthExpr = + auto MallocLengthExpr = allOf(callee(functionDecl( hasAnyName("::alloca", "::calloc", "malloc", "realloc"))), hasAnyArgument(ignoringParenImpCasts( diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c index c5deb25e9ac06d..d98e3049ed9e13 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c @@ -17,43 +17,43 @@ size_t wcsnlen(const wchar_t *, size_t); size_t wcsnlen_s(const wchar_t *, size_t); void bad_malloc(char *name) { - char *new_name = (char *)malloc(strlen(name + 1)); + char *new_name = (char *)malloc(strlen(name)+ 1); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_name = \(char \*\)malloc\(}}strlen(name) + 1{{\);$}} - new_name = (char *)malloc(strnlen(name + 1, 10)); + new_name = (char *)malloc(strnlen(name, 10) + 1); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: addition operator is applied to the argument of strnlen // CHECK-FIXES: {{^ new_name = \(char \*\)malloc\(}}strnlen(name, 10) + 1{{\);$}} - new_name = (char *)malloc(strnlen_s(name + 1, 10)); + new_name = (char *)malloc(strnlen_s(name, 10) + 1); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: addition operator is applied to the argument of strnlen_s // CHECK-FIXES: {{^ new_name = \(char \*\)malloc\(}}strnlen_s(name, 10) + 1{{\);$}} } void bad_malloc_wide(wchar_t *name) { - wchar_t *new_name = (wchar_t *)malloc(wcslen(name + 1)); + wchar_t *new_name = (wchar_t *)malloc(wcslen(name)+ 1); // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: addition operator is applied to the argument of wcslen // CHECK-FIXES: {{^ wchar_t \*new_name = \(wchar_t \*\)malloc\(}}wcslen(name) + 1{{\);$}} - new_name = (wchar_t *)malloc(wcsnlen(name + 1, 10)); + new_name = (wchar_t *)malloc(wcsnlen(name, 10) + 1 ); // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: addition operator is applied to the argument of wcsnlen // CHECK-FIXES: {{^ new_name = \(wchar_t \*\)malloc\(}}wcsnlen(name, 10) + 1{{\);$}} - new_name = (wchar_t *)malloc(wcsnlen_s(name + 1, 10)); + new_name = (wchar_t *)malloc(wcsnlen_s(name, 10) + 1 ); // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: addition operator is applied to the argument of wcsnlen_s // CHECK-FIXES: {{^ new_name = \(wchar_t \*\)malloc\(}}wcsnlen_s(name, 10) + 1{{\);$}} } void bad_alloca(char *name) { - char *new_name = (char *)alloca(strlen(name + 1)); + char *new_name = (char *)alloca(strlen(name)+ 1); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_name = \(char \*\)alloca\(}}strlen(name) + 1{{\);$}} } void bad_calloc(char *name) { - char *new_names = (char *)calloc(2, strlen(name + 1)); + char *new_names = (char *)calloc(2, strlen(name)+ 1); // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_names = \(char \*\)calloc\(2, }}strlen(name) + 1{{\);$}} } void bad_realloc(char *old_name, char *name) { - char *new_name = (char *)realloc(old_name, strlen(name + 1)); + char *new_name = (char *)realloc(old_name, strlen(name)+ 1); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_name = \(char \*\)realloc\(old_name, }}strlen(name) + 1{{\);$}} } @@ -79,7 +79,7 @@ void intentional3(char *name) { void (*(*const alloc_ptr)(size_t)) = malloc; void bad_indirect_alloc(char *name) { - char *new_name = (char *)alloc_ptr(strlen(name + 1)); + char *new_name = (char *)alloc_ptr(strlen(name)+ 1); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_name = \(char \*\)alloc_ptr\(}}strlen(name) + 1{{\);$}} } diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp index f7b1ad55f5df51..2fbbe68ef239f9 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp @@ -368,32 +368,32 @@ Zoz getZoz(Something s) { return Zoz(s); } void test_Something() { std::vector<Something> v; - v.push_back(Something(1, 2)); + v.emplace_back(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back instead of push_back [modernize-use-emplace] // CHECK-FIXES: v.emplace_back(1, 2); - v.push_back(Something{1, 2}); + v.emplace_back(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(1, 2); - v.push_back(Something()); + v.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(); - v.push_back(Something{}); + v.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(); Something Different; - v.push_back(Something(Different.getInt(), 42)); + v.emplace_back(Different.getInt(), 42); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(Different.getInt(), 42); - v.push_back(Different.getInt()); + v.emplace_back(Different.getInt()); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(Different.getInt()); - v.push_back(42); + v.emplace_back(42); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(42); @@ -426,11 +426,11 @@ void callDependent() { void test2() { std::vector<Zoz> v; - v.push_back(Zoz(Something(21, 37))); + v.emplace_back(Something(21, 37)); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(Something(21, 37)); - v.push_back(Zoz(Something(21, 37), 42)); + v.emplace_back(Something(21, 37), 42); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(Something(21, 37), 42); @@ -442,41 +442,41 @@ struct GetPair { }; void testPair() { std::vector<std::pair<int, int>> v; - v.push_back(std::pair<int, int>(1, 2)); + v.emplace_back(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(1, 2); GetPair g; - v.push_back(g.getPair()); + v.emplace_back(g.getPair()); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(g.getPair()); std::vector<std::pair<Something, Zoz>> v2; - v2.push_back(std::pair<Something, Zoz>(Something(42, 42), Zoz(Something(21, 37)))); + v2.emplace_back(Something(42, 42), Zoz(Something(21, 37))); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v2.emplace_back(Something(42, 42), Zoz(Something(21, 37))); } void testTuple() { std::vector<std::tuple<bool, char, int>> v; - v.push_back(std::tuple<bool, char, int>(false, 'x', 1)); + v.emplace_back(false, 'x', 1); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(false, 'x', 1); - v.push_back(std::tuple<bool, char, int>{false, 'y', 2}); + v.emplace_back(false, 'y', 2); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(false, 'y', 2); - v.push_back({true, 'z', 3}); + v.emplace_back(true, 'z', 3); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(true, 'z', 3); std::vector<std::tuple<int, bool>> x; - x.push_back(std::make_pair(1, false)); + x.emplace_back(1, false); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: x.emplace_back(1, false); - x.push_back(std::make_pair(2LL, 1)); + x.emplace_back(2LL, 1); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: x.emplace_back(2LL, 1); } @@ -504,9 +504,9 @@ void testSpaces() { // clang-format off - v.push_back(Something(1, //arg1 + v.emplace_back(1, //arg1 2 // arg2 - ) // Something + // Something ); // CHECK-MESSAGES: :[[@LINE-4]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(1, //arg1 @@ -514,26 +514,24 @@ void testSpaces() { // CHECK-FIXES: // Something // CHECK-FIXES: ); - v.push_back( Something (1, 2) ); + v.emplace_back(1, 2 ); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(1, 2 ); - v.push_back( Something {1, 2} ); + v.emplace_back(1, 2 ); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(1, 2 ); - v.push_back( Something {} ); + v.emplace_back( ); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back( ); - v.push_back( - Something(1, 2) ); + v.emplace_back(1, 2 ); // CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(1, 2 ); std::vector<Base> v2; - v2.push_back( - Base(42, nullptr)); + v2.emplace_back(42, nullptr); // CHECK-MESSAGES: :[[@LINE-2]]:6: warning: use emplace_back // CHECK-FIXES: v2.emplace_back(42, nullptr); @@ -558,15 +556,15 @@ void testPointers() { void testMakePair() { std::vector<std::pair<int, int>> v; - v.push_back(std::make_pair(1, 2)); + v.emplace_back(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(1, 2); - v.push_back(std::make_pair(42LL, 13)); + v.emplace_back(42LL, 13); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(42LL, 13); - v.push_back(std::make_pair<char, char>(0, 3)); + v.emplace_back(std::make_pair<char, char>(0, 3)); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(std::make_pair<char, char>(0, 3)); // @@ -579,7 +577,7 @@ void testMakePair() { D(...) {} operator char() const { return 0; } }; - v.push_back(std::make_pair<D, int>(Something(), 2)); + v.emplace_back(std::make_pair<D, int>(Something(), 2)); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(std::make_pair<D, int>(Something(), 2)); @@ -587,7 +585,7 @@ void testMakePair() { X(std::pair<int, int>) {} }; std::vector<X> x; - x.push_back(std::make_pair(1, 2)); + x.emplace_back(std::make_pair(1, 2)); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: x.emplace_back(std::make_pair(1, 2)); // make_pair cannot be removed here, as X is not constructible with two ints. @@ -596,7 +594,7 @@ void testMakePair() { Y(std::pair<int, int> &&) {} }; std::vector<Y> y; - y.push_back(std::make_pair(2, 3)); + y.emplace_back(std::make_pair(2, 3)); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: y.emplace_back(std::make_pair(2, 3)); // make_pair cannot be removed here, as Y is not constructible with two ints. @@ -604,15 +602,15 @@ void testMakePair() { void testMakeTuple() { std::vector<std::tuple<int, bool, char>> v; - v.push_back(std::make_tuple(1, true, 'v')); + v.emplace_back(1, true, 'v'); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(1, true, 'v'); - v.push_back(std::make_tuple(2ULL, 1, 0)); + v.emplace_back(2ULL, 1, 0); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(2ULL, 1, 0); - v.push_back(std::make_tuple<long long, int, int>(3LL, 1, 0)); + v.emplace_back(std::make_tuple<long long, int, int>(3LL, 1, 0)); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(std::make_tuple<long long, int, int>(3LL, 1, 0)); // make_tuple is not removed when there are explicit template @@ -648,77 +646,77 @@ Single<typename std::remove_reference<T>::type> MakeSingle(T &&) { void testOtherTuples() { std::vector<test::Single<int>> v; - v.push_back(test::Single<int>(1)); + v.emplace_back(1); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(1); - v.push_back({2}); + v.emplace_back(2); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(2); - v.push_back(test::MakeSingle(3)); + v.emplace_back(3); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(3); - v.push_back(test::MakeSingle<long long>(4)); + v.emplace_back(test::MakeSingle<long long>(4)); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(test::MakeSingle<long long>(4)); // We don't remove make functions with explicit template parameters. - v.push_back(test::MakeSingle(5LL)); + v.emplace_back(5LL); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(5LL); - v.push_back(std::make_tuple(6)); + v.emplace_back(6); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(6); - v.push_back(std::make_tuple(7LL)); + v.emplace_back(7LL); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(7LL); } void testOtherContainers() { std::list<Something> l; - l.push_back(Something(42, 41)); + l.emplace_back(42, 41); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: l.emplace_back(42, 41); - l.push_front(Something(42, 41)); + l.emplace_front(42, 41); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_front // CHECK-FIXES: l.emplace_front(42, 41); std::deque<Something> d; - d.push_back(Something(42)); + d.emplace_back(42); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: d.emplace_back(42); - d.push_front(Something(42)); + d.emplace_front(42); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_front // CHECK-FIXES: d.emplace_front(42); llvm::LikeASmallVector<Something> ls; - ls.push_back(Something(42)); + ls.emplace_back(42); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: ls.emplace_back(42); std::stack<Something> s; - s.push(Something(42, 41)); + s.emplace(42, 41); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace // CHECK-FIXES: s.emplace(42, 41); std::queue<Something> q; - q.push(Something(42, 41)); + q.emplace(42, 41); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace // CHECK-FIXES: q.emplace(42, 41); std::priority_queue<Something> pq; - pq.push(Something(42, 41)); + pq.emplace(42, 41); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace // CHECK-FIXES: pq.emplace(42, 41); std::forward_list<Something> fl; - fl.push_front(Something(42, 41)); + fl.emplace_front(42, 41); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_front // CHECK-FIXES: fl.emplace_front(42, 41); } @@ -764,7 +762,7 @@ void macroTest() { v.push_back(SOME_OBJ); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back - v.push_back(Something(MILLION)); + v.emplace_back(MILLION); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(MILLION); @@ -809,11 +807,11 @@ void testBitfields() { v.push_back(Something(42, b.bitfield)); v.push_back(Something(b.bitfield)); - v.push_back(Something(42, b.notBitfield)); + v.emplace_back(42, b.notBitfield); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(42, b.notBitfield); int var; - v.push_back(Something(42, var)); + v.emplace_back(42, var); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(42, var); } @@ -839,7 +837,7 @@ struct WithDtor { void testWithDtor() { std::vector<WithDtor> v; - v.push_back(WithDtor(42)); + v.emplace_back(42); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // CHECK-FIXES: v.emplace_back(42); } @@ -873,39 +871,39 @@ void testSomeEmplaceCases() { std::vector<Foo> v2; std::unordered_map<int, char *> m1; - v1.emplace_back(std::make_pair("foo", "bar")); + v1.emplace_back("foo", "bar"); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v1.emplace_back("foo", "bar"); char *foo = "bar"; - v1.emplace_back(std::make_pair(foo, "bar")); + v1.emplace_back(foo, "bar"); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v1.emplace_back(foo, "bar"); - v1.emplace(v1.begin(), std::make_pair("foo", "bar")); + v1.emplace(v1.begin(), "foo", "bar"); // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: v1.emplace(v1.begin(), "foo", "bar"); - v2.emplace_back(Foo()); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.emplace_back(Foo(13)); + v2.emplace_back(13); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v2.emplace_back(13); - v2.emplace_back(Foo{13}); + v2.emplace_back(13); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v2.emplace_back(13); int a = 31; - v2.emplace_back(Foo(13, a)); + v2.emplace_back(13, a); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v2.emplace_back(13, a); v2.emplace_back(std::make_pair(3, 3)); - m1.emplace(std::make_pair(13, "foo")); + m1.emplace(13, "foo"); // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: m1.emplace(13, "foo"); @@ -945,117 +943,117 @@ void testAllSTLEmplacyFunctions() { std::queue<Foo> queue; std::priority_queue<Foo> priority_queue; - vector.emplace_back(Foo(13)); + vector.emplace_back(13); // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: vector.emplace_back(13); - vector.emplace(vector.begin(), Foo(13)); + vector.emplace(vector.begin(), 13); // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: vector.emplace(vector.begin(), 13); - deque.emplace(deque.begin(), Foo(13)); + deque.emplace(deque.begin(), 13); // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: deque.emplace(deque.begin(), 13); - deque.emplace_front(Foo(13)); + deque.emplace_front(13); // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: unnecessary temporary object created while calling emplace_front // CHECK-FIXES: deque.emplace_front(13); - deque.emplace_back(Foo(13)); + deque.emplace_back(13); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: deque.emplace_back(13); - forward_list.emplace_after(forward_list.begin(), Foo(13)); + forward_list.emplace_after(forward_list.begin(), 13); // CHECK-MESSAGES: :[[@LINE-1]]:52: warning: unnecessary temporary object created while calling emplace_after // CHECK-FIXES: forward_list.emplace_after(forward_list.begin(), 13); - forward_list.emplace_front(Foo(13)); + forward_list.emplace_front(13); // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: unnecessary temporary object created while calling emplace_front // CHECK-FIXES: forward_list.emplace_front(13); - list.emplace(list.begin(), Foo(13)); + list.emplace(list.begin(), 13); // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: list.emplace(list.begin(), 13); - list.emplace_back(Foo(13)); + list.emplace_back(13); // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: list.emplace_back(13); - list.emplace_front(Foo(13)); + list.emplace_front(13); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: unnecessary temporary object created while calling emplace_front // CHECK-FIXES: list.emplace_front(13); - set.emplace(Foo(13)); + set.emplace(13); // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: set.emplace(13); - set.emplace_hint(set.begin(), Foo(13)); + set.emplace_hint(set.begin(), 13); // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unnecessary temporary object created while calling emplace_hint // CHECK-FIXES: set.emplace_hint(set.begin(), 13); - map.emplace(std::make_pair(13, Foo(13))); + map.emplace(13, Foo(13)); // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: map.emplace(13, Foo(13)); - map.emplace_hint(map.begin(), std::make_pair(13, Foo(13))); + map.emplace_hint(map.begin(), 13, Foo(13)); // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: unnecessary temporary object created while calling emplace_hint // CHECK-FIXES: map.emplace_hint(map.begin(), 13, Foo(13)); - multiset.emplace(Foo(13)); + multiset.emplace(13); // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: multiset.emplace(13); - multiset.emplace_hint(multiset.begin(), Foo(13)); + multiset.emplace_hint(multiset.begin(), 13); // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: unnecessary temporary object created while calling emplace_hint // CHECK-FIXES: multiset.emplace_hint(multiset.begin(), 13); - multimap.emplace(std::make_pair(13, Foo(13))); + multimap.emplace(13, Foo(13)); // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: multimap.emplace(13, Foo(13)); - multimap.emplace_hint(multimap.begin(), std::make_pair(13, Foo(13))); + multimap.emplace_hint(multimap.begin(), 13, Foo(13)); // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: unnecessary temporary object created while calling emplace_hint // CHECK-FIXES: multimap.emplace_hint(multimap.begin(), 13, Foo(13)); - unordered_set.emplace(Foo(13)); + unordered_set.emplace(13); // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: unordered_set.emplace(13); - unordered_set.emplace_hint(unordered_set.begin(), Foo(13)); + unordered_set.emplace_hint(unordered_set.begin(), 13); // CHECK-MESSAGES: :[[@LINE-1]]:53: warning: unnecessary temporary object created while calling emplace_hint // CHECK-FIXES: unordered_set.emplace_hint(unordered_set.begin(), 13); - unordered_map.emplace(std::make_pair(13, Foo(13))); + unordered_map.emplace(13, Foo(13)); // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: unordered_map.emplace(13, Foo(13)); - unordered_map.emplace_hint(unordered_map.begin(), std::make_pair(13, Foo(13))); + unordered_map.emplace_hint(unordered_map.begin(), 13, Foo(13)); // CHECK-MESSAGES: :[[@LINE-1]]:53: warning: unnecessary temporary object created while calling emplace_hint // CHECK-FIXES: unordered_map.emplace_hint(unordered_map.begin(), 13, Foo(13)); - unordered_multiset.emplace(Foo(13)); + unordered_multiset.emplace(13); // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: unordered_multiset.emplace(13); - unordered_multiset.emplace_hint(unordered_multiset.begin(), Foo(13)); + unordered_multiset.emplace_hint(unordered_multiset.begin(), 13); // CHECK-MESSAGES: :[[@LINE-1]]:63: warning: unnecessary temporary object created while calling emplace_hint // CHECK-FIXES: unordered_multiset.emplace_hint(unordered_multiset.begin(), 13); - unordered_multimap.emplace(std::make_pair(13, Foo(13))); + unordered_multimap.emplace(13, Foo(13)); // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: unordered_multimap.emplace(13, Foo(13)); - unordered_multimap.emplace_hint(unordered_multimap.begin(), std::make_pair(13, Foo(13))); + unordered_multimap.emplace_hint(unordered_multimap.begin(), 13, Foo(13)); // CHECK-MESSAGES: :[[@LINE-1]]:63: warning: unnecessary temporary object created while calling emplace_hint // CHECK-FIXES: unordered_multimap.emplace_hint(unordered_multimap.begin(), 13, Foo(13)); - stack.emplace(Foo(13)); + stack.emplace(13); // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: stack.emplace(13); - queue.emplace(Foo(13)); + queue.emplace(13); // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: queue.emplace(13); - priority_queue.emplace(Foo(13)); + priority_queue.emplace(13); // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: priority_queue.emplace(13); } @@ -1064,36 +1062,36 @@ void test_AliasEmplacyFunctions() { typedef std::list<Foo> L; using DQ = std::deque<Foo>; L l; - l.emplace_back(Foo(3)); + l.emplace_back(3); // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: l.emplace_back(3); DQ dq; - dq.emplace_back(Foo(3)); + dq.emplace_back(3); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: dq.emplace_back(3); typedef std::stack<Foo> STACK; using PQ = std::priority_queue<Foo>; STACK stack; - stack.emplace(Foo(3)); + stack.emplace(3); // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: stack.emplace(3); PQ pq; - pq.emplace(Foo(3)); + pq.emplace(3); // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: pq.emplace(3); typedef std::forward_list<Foo> FL; using DQ2 = std::deque<Foo>; FL fl; - fl.emplace_front(Foo(3)); + fl.emplace_front(3); // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace_front // CHECK-FIXES: fl.emplace_front(3); DQ2 dq2; - dq2.emplace_front(Foo(3)); + dq2.emplace_front(3); // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: unnecessary temporary object created while calling emplace_front // CHECK-FIXES: dq2.emplace_front(3); } @@ -1102,36 +1100,36 @@ void test_Alias() { typedef std::list<Foo> L; using DQ = std::deque<Foo>; L l; - l.push_back(Foo(3)); + l.emplace_back(3); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back instead of push_back [modernize-use-emplace] // CHECK-FIXES: l.emplace_back(3); DQ dq; - dq.push_back(Foo(3)); + dq.emplace_back(3); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back instead of push_back [modernize-use-emplace] // CHECK-FIXES: dq.emplace_back(3); typedef std::stack<Foo> STACK; using PQ = std::priority_queue<Foo>; STACK stack; - stack.push(Foo(3)); + stack.emplace(3); // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use emplace instead of push [modernize-use-emplace] // CHECK-FIXES: stack.emplace(3); PQ pq; - pq.push(Foo(3)); + pq.emplace(3); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace instead of push [modernize-use-emplace] // CHECK-FIXES: pq.emplace(3); typedef std::forward_list<Foo> FL; using DQ2 = std::deque<Foo>; FL fl; - fl.push_front(Foo(3)); + fl.emplace_front(3); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_front instead of push_front [modernize-use-emplace] // CHECK-FIXES: fl.emplace_front(3); DQ2 dq2; - dq2.push_front(Foo(3)); + dq2.emplace_front(3); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_front instead of push_front [modernize-use-emplace] // CHECK-FIXES: dq2.emplace_front(3); } @@ -1196,41 +1194,41 @@ struct NonTrivialWithCtor { void testBracedInitTemporaries() { std::vector<NonTrivialNoCtor> v1; - v1.push_back(NonTrivialNoCtor()); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.push_back(NonTrivialNoCtor{}); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.push_back({}); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.push_back(NonTrivialNoCtor{InnerType{}}); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.push_back({InnerType{}}); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.push_back(NonTrivialNoCtor{InnerType()}); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.push_back({InnerType()}); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.push_back({{}}); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.emplace_back(NonTrivialNoCtor()); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.emplace_back(NonTrivialNoCtor{}); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.emplace_back(NonTrivialNoCtor{InnerType{}}); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v1.emplace_back(); - v1.emplace_back(NonTrivialNoCtor{{}}); + v1.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v1.emplace_back(); @@ -1244,41 +1242,41 @@ void testBracedInitTemporaries() { std::vector<NonTrivialWithVector> v2; - v2.push_back(NonTrivialWithVector()); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.push_back(NonTrivialWithVector{}); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.push_back({}); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.push_back(NonTrivialWithVector{std::vector<int>{}}); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.push_back({std::vector<int>{}}); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.push_back(NonTrivialWithVector{std::vector<int>()}); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.push_back({std::vector<int>()}); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.push_back({{}}); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.emplace_back(NonTrivialWithVector()); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.emplace_back(NonTrivialWithVector{}); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.emplace_back(NonTrivialWithVector{std::vector<int>{}}); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v2.emplace_back(); - v2.emplace_back(NonTrivialWithVector{{}}); + v2.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v2.emplace_back(); @@ -1293,41 +1291,41 @@ void testBracedInitTemporaries() { std::vector<NonTrivialWithCtor> v3; - v3.push_back(NonTrivialWithCtor()); + v3.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v3.emplace_back(); - v3.push_back(NonTrivialWithCtor{}); + v3.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v3.emplace_back(); - v3.push_back({}); + v3.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v3.emplace_back(); - v3.push_back(NonTrivialWithCtor{std::vector<int>()}); + v3.emplace_back(std::vector<int>()); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v3.emplace_back(std::vector<int>()); - v3.push_back(NonTrivialWithCtor{std::vector<int>{0}}); + v3.emplace_back(std::vector<int>{0}); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v3.emplace_back(std::vector<int>{0}); - v3.push_back(NonTrivialWithCtor{std::vector<int>{}}); + v3.emplace_back(std::vector<int>{}); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v3.emplace_back(std::vector<int>{}); - v3.push_back({std::vector<int>{0}}); + v3.emplace_back(std::vector<int>{0}); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v3.emplace_back(std::vector<int>{0}); - v3.push_back({std::vector<int>{}}); + v3.emplace_back(std::vector<int>{}); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use emplace_back // CHECK-FIXES: v3.emplace_back(std::vector<int>{}); - v3.emplace_back(NonTrivialWithCtor()); + v3.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v3.emplace_back(); - v3.emplace_back(NonTrivialWithCtor{}); + v3.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v3.emplace_back(); - v3.emplace_back(NonTrivialWithCtor{std::vector<int>{}}); + v3.emplace_back(std::vector<int>{}); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v3.emplace_back(std::vector<int>{}); - v3.emplace_back(NonTrivialWithCtor{std::vector<int>{0}}); + v3.emplace_back(std::vector<int>{0}); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v3.emplace_back(std::vector<int>{0}); @@ -1353,83 +1351,83 @@ void testWithPointerTypes() { std::stack<Something> s; std::stack<Something>* sp; - lp->push_back(Something(1, 2)); + lp->emplace_back(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_back instead of push_back [modernize-use-emplace] // CHECK-FIXES: lp->emplace_back(1, 2); - lp->push_front(Something(1, 2)); + lp->emplace_front(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_front instead of push_front [modernize-use-emplace] // CHECK-FIXES: lp->emplace_front(1, 2); - sp->push(Something(1, 2)); + sp->emplace(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace instead of push [modernize-use-emplace] // CHECK-FIXES: sp->emplace(1, 2); - lp->push_back(Something{1, 2}); + lp->emplace_back(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_back instead of push_back [modernize-use-emplace] // CHECK-FIXES: lp->emplace_back(1, 2); - lp->push_front(Something{1, 2}); + lp->emplace_front(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_front instead of push_front [modernize-use-emplace] // CHECK-FIXES: lp->emplace_front(1, 2); - sp->push(Something{1, 2}); + sp->emplace(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace instead of push [modernize-use-emplace] // CHECK-FIXES: sp->emplace(1, 2); - lp->push_back(Something()); + lp->emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_back instead of push_back [modernize-use-emplace] // CHECK-FIXES: lp->emplace_back(); - lp->push_front(Something()); + lp->emplace_front(); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_front instead of push_front [modernize-use-emplace] // CHECK-FIXES: lp->emplace_front(); - sp->push(Something()); + sp->emplace(); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace instead of push [modernize-use-emplace] // CHECK-FIXES: sp->emplace(); - lp->push_back(Something{}); + lp->emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_back instead of push_back [modernize-use-emplace] // CHECK-FIXES: lp->emplace_back(); - lp->push_front(Something{}); + lp->emplace_front(); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace_front instead of push_front [modernize-use-emplace] // CHECK-FIXES: lp->emplace_front(); - sp->push(Something{}); + sp->emplace(); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use emplace instead of push [modernize-use-emplace] // CHECK-FIXES: sp->emplace(); - lp->emplace_back(Something(1, 2)); + lp->emplace_back(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: lp->emplace_back(1, 2); - lp->emplace_front(Something(1, 2)); + lp->emplace_front(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: unnecessary temporary object created while calling emplace_front // CHECK-FIXES: lp->emplace_front(1, 2); - sp->emplace(Something(1, 2)); + sp->emplace(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: sp->emplace(1, 2); - lp->emplace_back(Something{1, 2}); + lp->emplace_back(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: lp->emplace_back(1, 2); - lp->emplace_front(Something{1, 2}); + lp->emplace_front(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: unnecessary temporary object created while calling emplace_front // CHECK-FIXES: lp->emplace_front(1, 2); - sp->emplace(Something{1, 2}); + sp->emplace(1, 2); // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: sp->emplace(1, 2); - lp->emplace_back(Something()); + lp->emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: lp->emplace_back(); - lp->emplace_front(Something()); + lp->emplace_front(); // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: unnecessary temporary object created while calling emplace_front // CHECK-FIXES: lp->emplace_front(); - sp->emplace(Something()); + sp->emplace(); // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: sp->emplace(); - lp->emplace_back(Something{}); + lp->emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: lp->emplace_back(); - lp->emplace_front(Something{}); + lp->emplace_front(); // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: unnecessary temporary object created while calling emplace_front // CHECK-FIXES: lp->emplace_front(); - sp->emplace(Something{}); + sp->emplace(); // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: unnecessary temporary object created while calling emplace // CHECK-FIXES: sp->emplace(); } >From cac870739e0da44093bbc945a73f4f85d4586515 Mon Sep 17 00:00:00 2001 From: komalverma04 <komal148bti...@igdtuw.ac.in> Date: Fri, 5 Apr 2024 02:14:19 +0530 Subject: [PATCH 4/7] Fix faied tests of use_emplace.cpp and misplaced-operator-in-strlen-in-alloc.c --- .../misplaced-operator-in-strlen-in-alloc.c | 6 ++-- .../checkers/modernize/use-emplace.cpp | 28 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c index d98e3049ed9e13..f95dd9178244b8 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c @@ -59,19 +59,19 @@ void bad_realloc(char *old_name, char *name) { } void intentional1(char *name) { - char *new_name = (char *)malloc(strlen(name + 1) + 1); + char *new_name = (char *)malloc((strlen(name) + 1) + 1); // CHECK-MESSAGES-NOT: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // We have + 1 outside as well so we assume this is intentional } void intentional2(char *name) { - char *new_name = (char *)malloc(strlen(name + 2)); + char *new_name = (char *)malloc(strlen(name) + 2); // CHECK-MESSAGES-NOT: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // Only give warning for + 1, not + 2 } void intentional3(char *name) { - char *new_name = (char *)malloc(strlen((name + 1))); + char *new_name = (char *)malloc(strlen(name) + 1); // CHECK-MESSAGES-NOT: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // If expression is in extra parentheses, consider it as intentional } diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp index 2fbbe68ef239f9..53c70d37370455 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace.cpp @@ -767,7 +767,7 @@ void macroTest() { // CHECK-FIXES: v.emplace_back(MILLION); // clang-format off - v.push_back( Something OPEN 3 CLOSE ); + v.emplace_back( 3 ); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back // clang-format on PUSH_BACK_WHOLE(s, Something(1)); @@ -851,8 +851,8 @@ void testInitializerList() { using PairIntVector = std::pair<int, std::vector<int>>; std::vector<PairIntVector> x; - x.push_back(PairIntVector(3, {4})); - x.push_back({5, {6}}); + x.emplace_back(3, {4}); + x.emplace_back(5, {6}); } class Foo { @@ -908,21 +908,21 @@ void testSomeEmplaceCases() { // CHECK-FIXES: m1.emplace(13, "foo"); std::vector<std::pair<int, int>> v3; - v3.emplace_back(std::pair<int, int>(13, 71)); + v3.emplace_back(13, 71); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back - v3.emplace_back(std::make_pair(13, 71)); + v3.emplace_back(13, 71); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back std::vector<std::tuple<int, int, int>> v4; - v4.emplace_back(std::tuple<int, int, int>(13, 31, 71)); + v4.emplace_back(13, 31, 71); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back - v4.emplace_back(std::make_tuple(13, 31, 71)); + v4.emplace_back(13, 31, 71); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back std::vector<test::Single<int>> v5; - v5.emplace_back(test::Single<int>(13)); + v5.emplace_back(13); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back - v5.emplace_back(test::MakeSingle(13)); + v5.emplace_back(13); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back } @@ -1319,7 +1319,7 @@ void testBracedInitTemporaries() { v3.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v3.emplace_back(); - v3.emplace_back(); + v3.emplace_back(); // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: unnecessary temporary object created while calling emplace_back // CHECK-FIXES: v3.emplace_back(); v3.emplace_back(std::vector<int>{}); @@ -1331,10 +1331,10 @@ void testBracedInitTemporaries() { // These should not be noticed or fixed; after the correction, the code won't // compile. - v3.push_back(NonTrivialWithCtor{{0}}); - v3.push_back(NonTrivialWithCtor{{}}); - v3.push_back({{0}}); - v3.push_back({{}}); + v3.emplace_back({0}); + v3.emplace_back({}); + v3.emplace_back({0}); + v3.emplace_back({}); std::vector<NonTrivialWithIntAndVector> v4; >From f72e3c45869ee0cef34acc6aa8dab7f9b870ba82 Mon Sep 17 00:00:00 2001 From: komalverma04 <komal148bti...@igdtuw.ac.in> Date: Mon, 8 Apr 2024 23:01:20 +0530 Subject: [PATCH 5/7] fix misplaced-operator-in-strlen-in-alloc.c --- .../bugprone/misplaced-operator-in-strlen-in-alloc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c index f95dd9178244b8..1ca74db29efb86 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c @@ -21,10 +21,10 @@ void bad_malloc(char *name) { // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_name = \(char \*\)malloc\(}}strlen(name) + 1{{\);$}} new_name = (char *)malloc(strnlen(name, 10) + 1); - // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: addition operator is applied to the argument of strnlen + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:22: warning: addition operator is applied to the argument of strnlen // CHECK-FIXES: {{^ new_name = \(char \*\)malloc\(}}strnlen(name, 10) + 1{{\);$}} new_name = (char *)malloc(strnlen_s(name, 10) + 1); - // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: addition operator is applied to the argument of strnlen_s + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:22: warning: addition operator is applied to the argument of strnlen_s // CHECK-FIXES: {{^ new_name = \(char \*\)malloc\(}}strnlen_s(name, 10) + 1{{\);$}} } @@ -33,10 +33,10 @@ void bad_malloc_wide(wchar_t *name) { // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: addition operator is applied to the argument of wcslen // CHECK-FIXES: {{^ wchar_t \*new_name = \(wchar_t \*\)malloc\(}}wcslen(name) + 1{{\);$}} new_name = (wchar_t *)malloc(wcsnlen(name, 10) + 1 ); - // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: addition operator is applied to the argument of wcsnlen + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:25: warning: addition operator is applied to the argument of wcsnlen // CHECK-FIXES: {{^ new_name = \(wchar_t \*\)malloc\(}}wcsnlen(name, 10) + 1{{\);$}} new_name = (wchar_t *)malloc(wcsnlen_s(name, 10) + 1 ); - // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: addition operator is applied to the argument of wcsnlen_s + // CHECK-MESSAGES-NOT: :[[@LINE-1]]:25: warning: addition operator is applied to the argument of wcsnlen_s // CHECK-FIXES: {{^ new_name = \(wchar_t \*\)malloc\(}}wcsnlen_s(name, 10) + 1{{\);$}} } >From 21139c59bc5295c33ea0199621d6b9025c849042 Mon Sep 17 00:00:00 2001 From: komalverma04 <komal148bti...@igdtuw.ac.in> Date: Wed, 10 Apr 2024 00:51:35 +0530 Subject: [PATCH 6/7] Fix spaces in misplaced-operator-in-strlen-in-alloc.c --- .../bugprone/misplaced-operator-in-strlen-in-alloc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c index 1ca74db29efb86..3c219f6096faa6 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c @@ -17,7 +17,7 @@ size_t wcsnlen(const wchar_t *, size_t); size_t wcsnlen_s(const wchar_t *, size_t); void bad_malloc(char *name) { - char *new_name = (char *)malloc(strlen(name)+ 1); + char *new_name = (char *)malloc(strlen(name) + 1); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_name = \(char \*\)malloc\(}}strlen(name) + 1{{\);$}} new_name = (char *)malloc(strnlen(name, 10) + 1); @@ -29,7 +29,7 @@ void bad_malloc(char *name) { } void bad_malloc_wide(wchar_t *name) { - wchar_t *new_name = (wchar_t *)malloc(wcslen(name)+ 1); + wchar_t *new_name = (wchar_t *)malloc(wcslen(name) + 1); // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: addition operator is applied to the argument of wcslen // CHECK-FIXES: {{^ wchar_t \*new_name = \(wchar_t \*\)malloc\(}}wcslen(name) + 1{{\);$}} new_name = (wchar_t *)malloc(wcsnlen(name, 10) + 1 ); @@ -41,19 +41,19 @@ void bad_malloc_wide(wchar_t *name) { } void bad_alloca(char *name) { - char *new_name = (char *)alloca(strlen(name)+ 1); + char *new_name = (char *)alloca(strlen(name) + 1); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_name = \(char \*\)alloca\(}}strlen(name) + 1{{\);$}} } void bad_calloc(char *name) { - char *new_names = (char *)calloc(2, strlen(name)+ 1); + char *new_names = (char *)calloc(2, strlen(name) + 1); // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_names = \(char \*\)calloc\(2, }}strlen(name) + 1{{\);$}} } void bad_realloc(char *old_name, char *name) { - char *new_name = (char *)realloc(old_name, strlen(name)+ 1); + char *new_name = (char *)realloc(old_name, strlen(name) + 1); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_name = \(char \*\)realloc\(old_name, }}strlen(name) + 1{{\);$}} } @@ -79,7 +79,7 @@ void intentional3(char *name) { void (*(*const alloc_ptr)(size_t)) = malloc; void bad_indirect_alloc(char *name) { - char *new_name = (char *)alloc_ptr(strlen(name)+ 1); + char *new_name = (char *)alloc_ptr(strlen(name) + 1); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen // CHECK-FIXES: {{^ char \*new_name = \(char \*\)alloc_ptr\(}}strlen(name) + 1{{\);$}} } >From 54b1c903f459dc8efb0b96accd136bba72b09e6c Mon Sep 17 00:00:00 2001 From: komalverma04 <komal148bti...@igdtuw.ac.in> Date: Wed, 10 Apr 2024 22:36:05 +0530 Subject: [PATCH 7/7] Fix white spaces --- .../checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c index 3c219f6096faa6..1abadd4401b780 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/misplaced-operator-in-strlen-in-alloc.c @@ -32,10 +32,10 @@ void bad_malloc_wide(wchar_t *name) { wchar_t *new_name = (wchar_t *)malloc(wcslen(name) + 1); // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: addition operator is applied to the argument of wcslen // CHECK-FIXES: {{^ wchar_t \*new_name = \(wchar_t \*\)malloc\(}}wcslen(name) + 1{{\);$}} - new_name = (wchar_t *)malloc(wcsnlen(name, 10) + 1 ); + new_name = (wchar_t *)malloc(wcsnlen(name, 10) + 1); // CHECK-MESSAGES-NOT: :[[@LINE-1]]:25: warning: addition operator is applied to the argument of wcsnlen // CHECK-FIXES: {{^ new_name = \(wchar_t \*\)malloc\(}}wcsnlen(name, 10) + 1{{\);$}} - new_name = (wchar_t *)malloc(wcsnlen_s(name, 10) + 1 ); + new_name = (wchar_t *)malloc(wcsnlen_s(name, 10) + 1); // CHECK-MESSAGES-NOT: :[[@LINE-1]]:25: warning: addition operator is applied to the argument of wcsnlen_s // CHECK-FIXES: {{^ new_name = \(wchar_t \*\)malloc\(}}wcsnlen_s(name, 10) + 1{{\);$}} } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits