Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -1132,6 +1132,9 @@ break; if (Left->is(LK) && Right->is(RK)) { deleteToken(DeleteLeft ? Left : Right); + for (auto *Tok = Left->Next; Tok && Tok != Right; Tok = Tok->Next) + if (Tok->is(tok::comment)) + deleteToken(Tok); // If the right token is deleted, we should keep the left token // unchanged and pair it with the new right token. if (!DeleteLeft) Index: unittests/Format/CleanupTest.cpp =================================================================== --- unittests/Format/CleanupTest.cpp +++ unittests/Format/CleanupTest.cpp @@ -188,39 +188,34 @@ EXPECT_EQ(Expected, Result); } -// FIXME: delete comments too. -TEST_F(CleanupTest, CtorInitializationCommentAroundCommas) { - // Remove redundant commas around comment. - std::string Code = "class A {\nA() : x({1}), /* comment */, {} };"; - std::string Expected = "class A {\nA() : x({1}) /* comment */ {} };"; +TEST_F(CleanupTest, RemoveCommentsAroundDeleteCode) { + std::string Code = + "class A {\nA() : x({1}), /* comment */, /* comment */ {} };"; + std::string Expected = "class A {\nA() : x({1}) {} };"; std::vector Ranges; Ranges.push_back(tooling::Range(25, 0)); Ranges.push_back(tooling::Range(40, 0)); std::string Result = cleanup(Code, Ranges); EXPECT_EQ(Expected, Result); - // Remove trailing comma and ignore comment. - Code = "class A {\nA() : x({1}), // comment\n{} };"; - Expected = "class A {\nA() : x({1}) // comment\n{} };"; + Code = "class A {\nA() : x({1}), // comment\n {} };"; + Expected = "class A {\nA() : x({1})\n {} };"; Ranges = std::vector(1, tooling::Range(25, 0)); Result = cleanup(Code, Ranges); EXPECT_EQ(Expected, Result); - // Remove trailing comma and ignore comment. Code = "class A {\nA() : x({1}), // comment\n , y(1),{} };"; - Expected = "class A {\nA() : x({1}), // comment\n y(1){} };"; + Expected = "class A {\nA() : x({1}), y(1){} };"; Ranges = std::vector(1, tooling::Range(38, 0)); Result = cleanup(Code, Ranges); EXPECT_EQ(Expected, Result); - // Remove trailing comma and ignore comment. Code = "class A {\nA() : x({1}), \n/* comment */, y(1),{} };"; - Expected = "class A {\nA() : x({1}), \n/* comment */ y(1){} };"; + Expected = "class A {\nA() : x({1}), \n y(1){} };"; Ranges = std::vector(1, tooling::Range(40, 0)); Result = cleanup(Code, Ranges); EXPECT_EQ(Expected, Result); - // Remove trailing comma and ignore comment. Code = "class A {\nA() : , // comment\n y(1),{} };"; Expected = "class A {\nA() : // comment\n y(1){} };"; Ranges = std::vector(1, tooling::Range(17, 0));