google / google/error-prone

Possibility for concatenation of string literals in Refaster rules

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

Description

### Description of the feature request:

I would like to be able to concatenate string literals in the suggested replacement of a Refaster rule, e.g., an existing string literal of the source code (from a parameter of the rule) with a literal that is specified in the rule itself, such that I have `"foobar"` instead of `"foo" + "bar"` as result, where `"foo"` might come from the matched source code.

### Feature requests: what underlying problem are you trying to solve with this feature?

I have a Refaster rule that replaces `checkArgument(cond, "message " + o);` with `checkArgument(cond, "message " + "%s", o);` (with an arbitrary message). I would like to get `"message %s"` in the suggested replacement.

(click here for full rule)

```
@BeforeTemplate
void eager(boolean b, @Matches(CompileTimeConstantExpressionMatcher.class) String msg1, Object o1) {
Preconditions.checkArgument(b, msg1 + o1);
}

@AfterTemplate
void lazy(boolean b, String msg1, Object o1) {
Preconditions.checkArgument(b, msg1 + "%s", o1);
}
```

### Suggested solution

Another magic method `Refaster.stringConcat(String... parts)` that could be used as `Refaster.stringConcat(msg, "%s")` and would create a string literal with the concatenation of `msg` and `"%s"` if `msg` is a literal.

### What version of Error Prone are you using?

2.3.4

### Have you found anything relevant by searching the web?

No.

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.