Refaster block expression template fails to match a single statement that is not wrapped in a block
- Dominant language
- Java
- Stars
- 7.2k
- Forks
- 820
- Avg merge
- 5h 9m
- Merged PRs (30d)
- 50
Description
I'm using `error_prone_refaster-2.24.1.jar` and `error_prone_core-2.24.1-with-dependencies.jar` to refactor code. I noticed that the block do not get applied in some cases. I'll explain with an example. Here the class that I want to refactor:
```
import java.util.*;
public class RefasterExample {
public void method1() {
if (true) System.out.println("bad");
if (true) { System.out.println("good"); }
}
public void method2() {
if (true) Arrays.asList("bad");
if (true) { Arrays.asList("good"); }
}
}
```
I tried the following template:
```
static class ExampleRule1 {
@BeforeTemplate void match(String o) {
System.out.println(o);
}
@AfterTemplate void optimizedMethod(String o) {
System.out.println("debug: " + o);
}
}
```
However, it only matches the "good" line of method1:
```
@@ -3,5 +3,5 @@
public void method1() {
if (true) System.out.println("bad");
- if (true) { System.out.println("good"); }
+ if (true) { System.out.println("debug: " + "good"); }
}
```
When adding a `return` to the `match`, the template fails to match altogether. Adding a `return` to `optimizedMethod`, but not to `match` leads to an exception (not sure whether it's worth investigating, as adding the return there does not make much sense).
---
However, an expression template matches single statements without braces, e.g.,
```
static class ExampleRule2 {
@BeforeTemplate List match(String o) {
return Arrays.asList(o);
}
@AfterTemplate List optimizedMethod(String o) {
return Arrays.asList("debug: " + o);
}
}
```
leads to the expected result:
```
@@ -7,6 +8,6 @@
public void method2() {
- if (true) Arrays.asList("bad");
- if (true) { Arrays.asList("good"); }
+ if (true) Arrays.asList("debug: " + "bad");
+ if (true) { Arrays.asList("debug: " + "good"); }
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.