Fix RemoveUnusedPrivateMethods deleting methods called with raw collection arguments#898
Open
habiibullahm wants to merge 1 commit into
Conversation
…ctions Fix RemoveUnusedPrivateMethods removing methods called with raw collections Match used methods with relaxed parameter types so raw call sites (e.g. Map.keySet()/values()) are not treated as unused when the declaration uses parameterized types. Fixes openrewrite#877
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…ctions
Summary
• Fixes a false positive in RemoveUnusedPrivateMethods where a private method was removed even though it was still invoked.
• Adds relaxed method matching via referencesMethod() so call sites using raw types (e.g. raw Map → Set / Collection) still match
parameterized private method signatures (Set, Collection).
• Removes the fragile Generic{ string-based workaround in favor of consistent type-based matching.
Problem
When a private method is called with values from a raw Map (templates.keySet(), templates.values()), strict JavaType.Method#equals between
the declaration and TypesInUse.getUsedMethods() fails. The recipe treated the method as unused, deleted it, and left a non-compiling call
site.
Solution
Introduce referencesMethod(declaration, used) that considers a method used when:
• Names match
• Declaring class matches
• Parameter arity matches
• Each parameter is compatible via bidirectional TypeUtils.isOfType (handles raw vs parameterized mismatches)
Test plan
◼ RemoveUnusedPrivateMethodsTest.privateMethodCalledWithRawCollectionArguments (issue reproducer)
◼ RemoveUnusedPrivateMethodsTest.privateMethodCalledWithParameterizedMapArguments
What's changed?
What's your motivation?
Anything in particular you'd like reviewers to focus on?
Anyone you would like to review specifically?
Have you considered any alternatives or workarounds?
Any additional context
Checklist