Skip to content

Possibility for concatenation of string literals in Refaster rules #1496

@PhilippWendler

Description

@PhilippWendler

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions