[clang-tidy] fix readability-redundant-member-init auto-fix of Function-try-block

Summary: This fixes https://bugs.llvm.org/show_bug.cgi?id=39310

Reviewers: malcolm.parsons, ioeric

Reviewed By: malcolm.parsons

Subscribers: xazax.hun

Tags: #clang-format, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D74800
This commit is contained in:
Alexander Lanin 2020-02-19 22:58:54 +00:00 committed by Nathan James
parent 38419b8afa
commit 709fd989b6
4 changed files with 57 additions and 4 deletions

View File

@ -418,3 +418,17 @@ NegativeTemplateExisting<double> nted(0);
};
MACRO();
class FunctionTryBlock {
public:
FunctionTryBlock() try : i(5), k(8) {}
// CHECK-FIXES: FunctionTryBlock() try {}
catch (...) {}
private:
int i, k;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'i' [modernize-use-default-member-init]
// CHECK-MESSAGES: :[[@LINE-2]]:10: warning: use default member initializer for 'k' [modernize-use-default-member-init]
// CHECK-FIXES: int i{5}, k{8};
};

View File

@ -819,10 +819,15 @@ private:
Tok->Type = TT_BitFieldColon;
} else if (Contexts.size() == 1 &&
!Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
if (Tok->getPreviousNonComment()->isOneOf(tok::r_paren,
tok::kw_noexcept))
FormatToken *Prev = Tok->getPreviousNonComment();
if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept))
Tok->Type = TT_CtorInitializerColon;
else
else if (Prev->is(tok::kw_try)) {
// Member initializer list within function try block.
FormatToken *PrevPrev = Prev->getPreviousNonComment();
if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
Tok->Type = TT_CtorInitializerColon;
} else
Tok->Type = TT_InheritanceColon;
} else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
(Tok->Next->isOneOf(tok::r_paren, tok::comma) ||

View File

@ -1849,11 +1849,20 @@ void UnwrappedLineParser::parseTryCatch() {
if (FormatTok->is(tok::colon)) {
// We are in a function try block, what comes is an initializer list.
nextToken();
// In case identifiers were removed by clang-tidy, what might follow is
// multiple commas in sequence - before the first identifier.
while (FormatTok->is(tok::comma))
nextToken();
while (FormatTok->is(tok::identifier)) {
nextToken();
if (FormatTok->is(tok::l_paren))
parseParens();
if (FormatTok->is(tok::comma))
// In case identifiers were removed by clang-tidy, what might follow is
// multiple commas in sequence - after the first identifier.
while (FormatTok->is(tok::comma))
nextToken();
}
}

View File

@ -151,6 +151,31 @@ TEST_F(CleanupTest, CtorInitializationSimpleRedundantComma) {
EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
}
// regression test for bug 39310
TEST_F(CleanupTest, CtorInitializationSimpleRedundantCommaInFunctionTryBlock) {
std::string Code = "class A {\nA() try : , {} };";
std::string Expected = "class A {\nA() try {} };";
EXPECT_EQ(Expected, cleanupAroundOffsets({21, 23}, Code));
Code = "class A {\nA() try : x(1), {} };";
Expected = "class A {\nA() try : x(1) {} };";
EXPECT_EQ(Expected, cleanupAroundOffsets({27}, Code));
Code = "class A {\nA() try :,,,,{} };";
Expected = "class A {\nA() try {} };";
EXPECT_EQ(Expected, cleanupAroundOffsets({19}, Code));
Code = "class A {\nA() try : x(1),,, {} };";
Expected = "class A {\nA() try : x(1) {} };";
EXPECT_EQ(Expected, cleanupAroundOffsets({27}, Code));
// Do not remove every comma following a colon as it simply doesn't make
// sense in some situations.
Code = "try : , {}";
Expected = "try : , {}";
EXPECT_EQ(Expected, cleanupAroundOffsets({8}, Code));
}
TEST_F(CleanupTest, CtorInitializationSimpleRedundantColon) {
std::string Code = "class A {\nA() : =default; };";
std::string Expected = "class A {\nA() =default; };";