Skip to content

Commit e8b8f89

Browse files
committed
[clang-tidy] Simplify braced init check
The normalization of matchers means that this now works in all language modes. Differential Revision: https://reviews.llvm.org/D96135
1 parent 7dd42ec commit e8b8f89

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,14 @@ void ReturnBracedInitListCheck::registerMatchers(MatchFinder *Finder) {
2323
auto ConstructExpr =
2424
cxxConstructExpr(
2525
unless(anyOf(hasDeclaration(cxxConstructorDecl(isExplicit())),
26-
isListInitialization(), hasDescendant(initListExpr()),
27-
isInTemplateInstantiation())))
26+
isListInitialization(), hasDescendant(initListExpr()))))
2827
.bind("ctor");
2928

30-
auto CtorAsArgument = materializeTemporaryExpr(anyOf(
31-
has(ConstructExpr), has(cxxFunctionalCastExpr(has(ConstructExpr)))));
32-
3329
Finder->addMatcher(
34-
traverse(TK_AsIs,
35-
functionDecl(
36-
isDefinition(), // Declarations don't have return statements.
37-
returns(unless(anyOf(builtinType(), autoType()))),
38-
hasDescendant(returnStmt(hasReturnValue(
39-
has(cxxConstructExpr(has(CtorAsArgument)))))))
40-
.bind("fn")),
30+
returnStmt(hasReturnValue(ConstructExpr),
31+
forFunction(functionDecl(returns(unless(anyOf(builtinType(),
32+
autoType()))))
33+
.bind("fn"))),
4134
this);
4235
}
4336

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class ReturnBracedInitListCheck : public ClangTidyCheck {
2929
}
3030
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
3131
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
32+
llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
33+
return TK_IgnoreUnlessSpelledInSource;
34+
}
3235
};
3336

3437
} // namespace modernize

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// RUN: %check_clang_tidy -std=c++14 %s modernize-return-braced-init-list %t
2-
// FIXME: Fix the checker to work in C++17 mode.
1+
// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-return-braced-init-list %t
32

43
namespace std {
54
typedef decltype(sizeof(int)) size_t;

0 commit comments

Comments
 (0)