Skip to content

Commit 7cc1bfa

Browse files
authored
[clang-tidy][modernize-return-braced-init-list]fix false-positives (#68491)
1 parent 8763343 commit 7cc1bfa

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

clang-tools-extra/clang-tidy/modernize/ReturnBracedInitListCheck.cpp

+20-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ReturnBracedInitListCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
#include "clang/ASTMatchers/ASTMatchers.h"
1213
#include "clang/Lex/Lexer.h"
1314
#include "clang/Tooling/FixIt.h"
1415

@@ -17,11 +18,27 @@ using namespace clang::ast_matchers;
1718
namespace clang::tidy::modernize {
1819

1920
void ReturnBracedInitListCheck::registerMatchers(MatchFinder *Finder) {
20-
// Skip list initialization and constructors with an initializer list.
21+
auto SemanticallyDifferentContainer = allOf(
22+
hasDeclaration(
23+
// Container(size_type count, const T &value,
24+
// const Allocator &alloc = Allocator());
25+
cxxConstructorDecl(parameterCountIs(3),
26+
hasParameter(0, hasType(qualType(hasCanonicalType(
27+
isInteger())))))),
28+
hasType(cxxRecordDecl(hasAnyName("::std::basic_string", "::std::vector",
29+
"::std::deque", "::std::forward_list",
30+
"::std::list"))));
31+
2132
auto ConstructExpr =
2233
cxxConstructExpr(
23-
unless(anyOf(hasDeclaration(cxxConstructorDecl(isExplicit())),
24-
isListInitialization(), hasDescendant(initListExpr()))))
34+
unless(anyOf(
35+
// Skip explicit constructor.
36+
hasDeclaration(cxxConstructorDecl(isExplicit())),
37+
// Skip list initialization and constructors with an initializer
38+
// list.
39+
isListInitialization(), hasDescendant(initListExpr()),
40+
// Skip container `vector(size_type, const T&, ...)`.
41+
SemanticallyDifferentContainer)))
2542
.bind("ctor");
2643

2744
Finder->addMatcher(

clang-tools-extra/docs/ReleaseNotes.rst

+5
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ Changes in existing checks
269269
<clang-tidy/checks/modernize/loop-convert>` to support for-loops with
270270
iterators initialized by free functions like ``begin``, ``end``, or ``size``.
271271

272+
- Improved :doc:`modernize-return-braced-init-list
273+
<clang-tidy/checks/modernize/return-braced-init-list>` check to ignore
274+
false-positives when constructing the container with ``count`` copies of
275+
elements with value ``value``.
276+
272277
- Improved :doc:`modernize-use-equals-delete
273278
<clang-tidy/checks/modernize/use-equals-delete>` check to ignore
274279
false-positives when special member function is actually used or implicit.

clang-tools-extra/test/clang-tidy/checkers/modernize/return-braced-init-list.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ class initializer_list {
3030
};
3131

3232
template <typename T>
33+
struct allocator {};
34+
35+
template <typename T, typename Allocator = ::std::allocator<T>>
3336
class vector {
3437
public:
35-
vector(T) {}
36-
vector(std::initializer_list<T>) {}
38+
vector(T);
39+
vector(size_t, T, const Allocator &alloc = Allocator());
40+
vector(std::initializer_list<T>);
3741
};
38-
}
42+
} // namespace std
3943

4044
class Bar {};
4145

@@ -98,12 +102,26 @@ Foo f6() {
98102
return Foo(b6, 1);
99103
}
100104

101-
std::vector<int> f7() {
105+
std::vector<int> vectorWithOneParameter() {
102106
int i7 = 1;
103107
return std::vector<int>(i7);
104108
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
105109
}
106110

111+
std::vector<int> vectorIntWithTwoParameter() {
112+
return std::vector<int>(1, 2);
113+
}
114+
115+
std::vector<double> vectorDoubleWithTwoParameter() {
116+
return std::vector<double>(1, 2.1);
117+
}
118+
struct A {};
119+
std::vector<A> vectorRecordWithTwoParameter() {
120+
A a{};
121+
return std::vector<A>(1, a);
122+
}
123+
124+
107125
Bar f8() {
108126
return {};
109127
}

0 commit comments

Comments
 (0)