Skip to content

VariableNameUtils#generateVariableName() should consider following statements #2776

Closed
@knutwannheden

Description

@knutwannheden

When using VariableNameUtils#generateVariableName() to generate variable names to be inserted at a given position in the code (like it is being used in the ZipSlip recipe of rewrite-java-security, which is the only use I found), then the logic should also consider the statements that follow the given cursor (which will roughly be the insertion point for the variable), otherwise when a new variable is inserted it could conflict with following variables.

In the following example the utility would for a base name of s calculate the name s1 even if that conflicts with a variable that follows. Thus, inserting a variable would render the class uncompilable.

class A {
  void test() {
    String s = null;
    ; // <-- insert new variable with base name "s" here
    String s1 = null;
  }
}

I am currently facing this issue in the InstanceOfPatternMatch recipe.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions