Don't use VarDecl replacements on range for loops. (#604)

This commit is contained in:
Jon Meow
2021-06-29 09:41:15 -07:00
committed by GitHub
parent b60ef90d62
commit 8e31e46c66
9 changed files with 64 additions and 59 deletions
+5 -1
View File
@@ -14,7 +14,11 @@ namespace Carbon {
VarDecl::VarDecl(std::map<std::string, Replacements>& in_replacements,
cam::MatchFinder* finder)
: Matcher(in_replacements) {
finder->addMatcher(cam::varDecl().bind(Label), this);
// Skip range-based for loops in this var-inserter.
finder->addMatcher(cam::varDecl(cam::unless(cam::hasParent(cam::declStmt(
cam::hasParent(cam::cxxForRangeStmt())))))
.bind(Label),
this);
}
// Returns a string for the type.
@@ -129,7 +129,8 @@ TEST_F(VarDeclTest, ParamsConst) {
}
TEST_F(VarDeclTest, ParamStruct) {
// This is to ensure the 'struct' keyword doesn't get added to the call type.
// This is to ensure the 'struct' keyword doesn't get added to the qualified
// type.
constexpr char Before[] = R"cpp(
struct Circle {};
auto Draw(int times, const Circle& circle) -> bool;
@@ -155,20 +156,20 @@ TEST_F(VarDeclTest, Member) {
ExpectReplacement(Before, Before);
}
TEST_F(VarDeclTest, DISABLED_RangeFor) {
// TODO: Handle range based for loops. Test shouldn't be enabled without
// addressing this because the output is quirky and fragile.
TEST_F(VarDeclTest, RangeFor) {
constexpr char Before[] = R"cpp(
void Foo() {
int items[] = {1};
for (int i : items) {
int j;
}
}
)cpp";
constexpr char After[] = R"(
void Foo() {
var items: int[] = {1};
var items: int [1];
for (int i : items) {
var j: int;
}
}
)";