rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

RemoveUnusedPrivateMethods removes private method still called with raw collection arguments

Open protocol7 opened this issue 7 months ago • 1 comments

What version of OpenRewrite are you using?

  • rewrite-static-analysis: 2.34.0
  • rewrite-core: 8.81.1
  • rewrite-java: 8.81.1

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

This is an edge case involving raw collection arguments. The raw Map is important here; a parameterized Map<String, String> does not reproduce the issue.

import java.util.Collection;
import java.util.Map;
import java.util.Set;

class Main {
  Main(Map templates) {
    compileTemplates(templates.keySet(), templates.values());
  }

  private Set<String> compileTemplates(
      final Set<String> compiledParam, final Collection<String> toCheck) {
    return compiledParam;
  }
}

Run org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods.

What did you expect to see?

The private compileTemplates(..) method should be preserved because it is called from the constructor.

Although the call uses raw Map#keySet() and Map#values() values, the original code compiles with unchecked/rawtype warnings.

What did you see instead?

The recipe removes the private method and its imports, but leaves the call site behind:

import java.util.Map;

class Main {
  Main(Map templates) {
    compileTemplates(templates.keySet(), templates.values());
  }
}

The rewritten code no longer compiles because compileTemplates(..) no longer exists.

protocol7 avatar May 04 '26 18:05 protocol7

“I’d like to work on this. I’ll investigate and submit a PR.”

habiibullahm avatar May 20 '26 13:05 habiibullahm