google / google/error-prone

Invalid Refaster suggestion because parentheses are not added

Open
#4,866 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

I want to use the following Refaster rule:

```java
class StringFormat {

@BeforeTemplate
String format(String format, @Repeated Object more) {
return String.format(format, more);
}

@AfterTemplate
String formatted(String format, @Repeated Object more) {
return format.formatted(more);
}
}
```

However, when applied to code where `format` is an expression, not just a literal, the suggestion replacement becomes invalid because Refaster fails to add parentheses around `format` when they are necessary.
Example:
```java
public class Test {

public static void main(String[] args) {
System.out.println(String.format("%s " + "another string", "test"));
}
}
```
is rewritten to
```java
public class Test {

public static void main(String[] args) {
System.out.println("%s " + "another string".formatted("test"));
}
}
```
But it needs to be `("%s " + "another string").formatted("test")`. I can manually add parentheses to `@AfterTemplate`, but then I get them all the time, even when not necessary.

I think that Refaster already adds parentheses in other cases if required, but the case of a receiver of a method call seems to be unhandled.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.