openrewrite / openrewrite/rewrite-static-analysis

org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals creates unnecessary duplicates

Open
#351 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
62
Forks
112
Avg merge
1d 19h
Merged PRs (30d)
40

Description

What is the smallest, simplest way to reproduce the problem?

class A {
  public static final Map<String, String> test = Map.of("Android");
  public static final String ANDROID = "Android";
  void foo() {
   System.out.println("Android");
   }
}

What did you expect to see?

class A {
  public static final String ANDROID = "Android";
  public static final Map<String, String> test = Map.of(ANROID);
  void foo() {
   System.out.println(ANDROID);
   }
}

What did you see instead?

class A {
  private static final String ANDROID_1 = "Android";
  public static final Map<String, String> test = Map.of(ANROID_1);
  public static final String ANDROID = ANROID_1;
  void foo() {
   System.out.println(ANDROID);
   }
}

What is the full stack trace of any errors you encountered?

stacktrace output here

Are you interested in contributing a fix to OpenRewrite?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the ReplaceDuplicateStringLiterals recipe and reproduce the Java snippet from the issue. Trace how the existing ANDROID declaration and the generated ANDROID_1 symbol are selected; done means the repeated literal reuses ANDROID without creating an unnecessary duplicate.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.