Skip to content
Merged
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 @@ -1046,4 +1046,113 @@ void test(Object e) {
""")
.doTest();
}

@Test
public void castVariableReassigned_brokenRefactoring() {
// TODO: b/395603588 - This refactoring is broken because 's' is reassigned,
// but pattern variables are implicitly final. It should be expectUnchanged().
helper
.allowBreakingChanges()
.addInputLines(
"Test.java",
"""
class Test {
void test(Object o) {
if (o instanceof String) {
String s = (String) o;
s = "new value";
System.out.println(s);
}
}
}
""")
.addOutputLines(
"Test.java",
"""
class Test {
void test(Object o) {
if (o instanceof String s) {

s = "new value";
System.out.println(s);
}
}
}
""")
.doTest();
}

@Test
public void scopeExpansionClobbering_brokenRefactoring() {
// TODO: b/395603588 - This refactoring is broken because the pattern variable 's'
// scope expands and clashes with 'int s = 0'. It should be expectUnchanged().
helper
.allowBreakingChanges()
.addInputLines(
"Test.java",
"""
class Test {
void test(Object o) {
if (!(o instanceof String)) {
return;
}
{
String s = (String) o;
System.out.println(s);
}
int s = 0;
}
}
""")
.addOutputLines(
"Test.java",
"""
class Test {
void test(Object o) {
if (!(o instanceof String s)) {
return;
}
{
System.out.println(s);
}
int s = 0;
}
}
""")
.doTest();
}

@Test
public void patternVariableScopeDoesNotOverlapWithLaterDeclaration() {
helper
.addInputLines(
"Test.java",
"""
class Test {
void test(Object o) {
if (o instanceof String) {
String s = (String) o;
System.out.println(s);
}
int s = 0;
System.out.println(s);
}
}
""")
.addOutputLines(
"Test.java",
"""
class Test {
void test(Object o) {
if (o instanceof String s) {

System.out.println(s);
}
int s = 0;
System.out.println(s);
}
}
""")
.doTest();
}
}
Loading