Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy][modernize-return-braced-init-list]fix false-positives #68491

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "ReturnBracedInitListCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"
#include "clang/Tooling/FixIt.h"

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

void ReturnBracedInitListCheck::registerMatchers(MatchFinder *Finder) {
// Skip list initialization and constructors with an initializer list.
auto SemanticallyDifferentContainer = allOf(
hasDeclaration(
// Container(size_type count, const T &value,
// const Allocator &alloc = Allocator());
cxxConstructorDecl(parameterCountIs(3),
hasParameter(0, hasType(qualType(hasCanonicalType(
isInteger())))))),
hasType(cxxRecordDecl(hasAnyName("::std::basic_string", "::std::vector",
"::std::deque", "::std::forward_list",
"::std::list"))));

auto ConstructExpr =
cxxConstructExpr(
unless(anyOf(hasDeclaration(cxxConstructorDecl(isExplicit())),
isListInitialization(), hasDescendant(initListExpr()))))
unless(anyOf(
// Skip explicit constructor.
hasDeclaration(cxxConstructorDecl(isExplicit())),
// Skip list initialization and constructors with an initializer
// list.
isListInitialization(), hasDescendant(initListExpr()),
// Skip container `vector(size_type, const T&, ...)`.
SemanticallyDifferentContainer)))
.bind("ctor");

Finder->addMatcher(
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ Changes in existing checks
<clang-tidy/checks/modernize/loop-convert>` to support for-loops with
iterators initialized by free functions like ``begin``, ``end``, or ``size``.

- Improved :doc:`modernize-return-braced-init-list
<clang-tidy/checks/modernize/return-braced-init-list>` check to ignore
false-positives when constructing the container with ``count`` copies of
elements with value ``value``.

- Improved :doc:`modernize-use-equals-delete
<clang-tidy/checks/modernize/use-equals-delete>` check to ignore
false-positives when special member function is actually used or implicit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ class initializer_list {
};

template <typename T>
struct allocator {};

template <typename T, typename Allocator = ::std::allocator<T>>
class vector {
public:
vector(T) {}
vector(std::initializer_list<T>) {}
vector(T);
vector(size_t, T, const Allocator &alloc = Allocator());
vector(std::initializer_list<T>);
};
}
} // namespace std

class Bar {};

Expand Down Expand Up @@ -98,12 +102,26 @@ Foo f6() {
return Foo(b6, 1);
}

std::vector<int> f7() {
std::vector<int> vectorWithOneParameter() {
int i7 = 1;
return std::vector<int>(i7);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
}

std::vector<int> vectorIntWithTwoParameter() {
return std::vector<int>(1, 2);
}

std::vector<double> vectorDoubleWithTwoParameter() {
return std::vector<double>(1, 2.1);
}
struct A {};
std::vector<A> vectorRecordWithTwoParameter() {
A a{};
return std::vector<A>(1, a);
}


Bar f8() {
return {};
}
Expand Down