-
Notifications
You must be signed in to change notification settings - Fork 71
RemoveUnusedParams #560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
RemoveUnusedParams #560
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well done!
try polish.
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
This one done: Do we cover |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/resources/META-INF/rewrite/examples.yml
- lines 660-659
- lines 705-721
- lines 1254-1273
- lines 1306-1305
- lines 2337-2336
- lines 2352-2364
- lines 2801-2800
- lines 2820-2831
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/RemoveUnusedParamsTest.java
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/RemoveUnusedParamsTest.java
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/resources/META-INF/rewrite/examples.yml
- lines 660-659
- lines 705-721
- lines 1254-1273
- lines 1306-1305
- lines 2337-2336
- lines 2352-2364
- lines 2801-2800
- lines 2820-2831
src/test/java/org/openrewrite/staticanalysis/RemoveUnusedParamsTest.java
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/RemoveUnusedParamsTest.java
Show resolved
Hide resolved
RemoveUnusedParams only handles unused parameters. Unused assignments would need their own recipe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/resources/META-INF/rewrite/examples.yml
- lines 328-345
- lines 660-659
- lines 705-721
- lines 1254-1273
- lines 1306-1305
- lines 2251-2250
- lines 2337-2336
- lines 2352-2364
- lines 2801-2800
- lines 2820-2831
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
…s.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try avoid feature envy (& DRY) giving single point of truth (SPOT)
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new test case - cover constructor as well.
src/test/java/org/openrewrite/staticanalysis/RemoveUnusedParamsTest.java
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/RemoveUnusedParamsTest.java
Show resolved
Hide resolved
Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments Updated review comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/resources/META-INF/rewrite/examples.yml
- lines 328-345
- lines 660-659
- lines 705-721
- lines 1254-1273
- lines 1306-1305
- lines 2251-2250
- lines 2337-2336
- lines 2352-2364
- lines 2801-2800
- lines 2820-2831
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
…s.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…s.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last polish.
Done very well, could already merge.
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/RemoveUnusedParamsTest.java
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/RemoveUnusedParamsTest.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/resources/META-INF/rewrite/examples.yml
- lines 328-345
- lines 660-659
- lines 705-721
- lines 1254-1273
- lines 1306-1305
- lines 2251-2250
- lines 2337-2336
- lines 2352-2364
- lines 2801-2800
- lines 2820-2831
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uncle bob would be proud.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/resources/META-INF/rewrite/examples.yml
- lines 328-345
- lines 660-659
- lines 705-721
- lines 1254-1273
- lines 1306-1305
- lines 2251-2250
- lines 2337-2336
- lines 2352-2364
- lines 2801-2800
- lines 2820-2831
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
@Pankraz76 Thanks! I’ve opened Issue #564 to track adding the UnusedAssignment recipe. Please feel free to drop any additional examples or feedback over there! |
Thanks both! I'm not likely to get to a review this week with travel ahead, but perhaps @MBoegers can have a look while I'm out. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick first round of review; great that you worked out you need a scanning recipe already; My comments are mostly to make better use of utilities and conventions we have in the project.
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
private J.MethodDeclaration collectOverrideSignature(final J.MethodDeclaration m, final Accumulator acc) { | ||
m.getLeadingAnnotations().stream() | ||
.map(J.Annotation::getSimpleName) | ||
.filter(OVERRIDE_ANNOTATION::equals) | ||
.findAny() | ||
.ifPresent(annotationName -> acc.add(buildSignature(m))); | ||
return m; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than look for an override annotation, we have a method called isOverride
on the type that should be used here instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed
private boolean isShadowed(final String name, final Deque<Set<String>> shadowStack) { | ||
return shadowStack.stream().anyMatch(scope -> scope.contains(name)); | ||
} | ||
|
||
private boolean isDeclaredAsParameter(final String name, final J.MethodDeclaration m) { | ||
return m.getParameters().stream() | ||
.filter(p -> p instanceof J.VariableDeclarations) | ||
.flatMap(p -> ((J.VariableDeclarations) p).getVariables().stream()) | ||
.anyMatch(v -> v.getSimpleName().equals(name)); | ||
} | ||
|
||
private List<Statement> filterUnusedParameters(final J.MethodDeclaration m, final Set<String> usedParams) { | ||
return m.getParameters().stream() | ||
.flatMap(param -> param instanceof J.VariableDeclarations ? | ||
filterDeclaration((J.VariableDeclarations) param, usedParams) | ||
: Stream.of(param)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Stream<Statement> filterDeclaration(final J.VariableDeclarations decl, final Set<String> usedParams) { | ||
return Optional.of(decl) | ||
.filter(d -> !d.getLeadingAnnotations().isEmpty()) | ||
.map(d -> Stream.<Statement>of(d)) | ||
.orElseGet(() -> pruneByUsage(decl, usedParams)); | ||
} | ||
|
||
private Stream<Statement> pruneByUsage(final J.VariableDeclarations decl, final Set<String> usedParams) { | ||
return Optional.of(collectUsedParameters(decl, usedParams)) | ||
.filter(kept -> !kept.isEmpty()) | ||
.map(kept -> Stream.<Statement>of(decl.withVariables(kept))) | ||
.orElseGet(Stream::empty); | ||
} | ||
|
||
private List<J.VariableDeclarations.NamedVariable> collectUsedParameters(final J.VariableDeclarations decl, final Set<String> usedParams) { | ||
return decl.getVariables().stream() | ||
.filter(v -> usedParams.contains(v.getSimpleName())) | ||
.collect(Collectors.toList()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general we try to avoid the streams API for as much as possible, especially when applied to every LST element of a particular type, as it leads to poor performance at scale with a lot of object allocations. Keep in kind that we're targeting Java 8 still, so any performance improvements in newer Java versions do not automatically apply.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood, I replaced those streams with plain loops
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might consider rule for this, with checkstyle custom imports. This is never ending story other way.
Sorry forgot about stream limitation. Its random impl. detail, at least functional method scope can be obtained.
This case could be new a migration recipe as well.
In both ways: giving lambda from list and backwards like seen here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/resources/META-INF/rewrite/examples.yml
- lines 328-345
- lines 660-659
- lines 705-721
- lines 1254-1273
- lines 1306-1305
- lines 2251-2250
- lines 2337-2336
- lines 2352-2364
- lines 2801-2800
- lines 2820-2831
src/main/java/org/openrewrite/staticanalysis/RemoveUnusedParams.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/resources/META-INF/rewrite/examples.yml
- lines 328-345
- lines 660-659
- lines 705-721
- lines 1254-1273
- lines 1306-1305
- lines 2251-2250
- lines 2337-2336
- lines 2352-2364
- lines 2801-2800
- lines 2820-2831
import java.time.Duration; | ||
import java.util.ArrayDeque; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import java.time.Duration; | |
import java.util.ArrayDeque; | |
import java.util.ArrayDeque; |
return m.getBody() != null | ||
&& m.getMethodType() != null | ||
&& !m.hasModifier(J.Modifier.Type.Native) | ||
&& m.getLeadingAnnotations().isEmpty() | ||
&& !acc.contains(buildSignature(m)); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return m.getBody() != null | |
&& m.getMethodType() != null | |
&& !m.hasModifier(J.Modifier.Type.Native) | |
&& m.getLeadingAnnotations().isEmpty() | |
&& !acc.contains(buildSignature(m)); | |
} | |
return m.getBody() != null && | |
m.getMethodType() != null && | |
!m.hasModifier(J.Modifier.Type.Native) && | |
m.getLeadingAnnotations().isEmpty() && | |
!acc.contains(buildSignature(m)); | |
return m.getSimpleName() + "#" + | |
m.getMethodType().getParameterTypes().stream() |
return m.getSimpleName() + "#" | ||
+ m.getMethodType().getParameterTypes().stream() | ||
.map(Object::toString) | ||
.collect(Collectors.joining(",")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use MethodMatcher.methodPattern
here instead of composing this yourself.
What’s changed?
I’ve added a new recipe class, RemoveUnusedParams, that automatically removes parameters from Java methods when they’re declared but never referenced in the method body. It still respects:
@Deprecated
)Alongside the main recipe, I’ve expanded the unit tests to cover:
What’s your motivation?
Over time, code tends to accumulate unused parameters—leftovers from refactoring, changing requirements, or evolving APIs. They add noise, make signatures harder to read, and can confuse future maintainers. This recipe:
Anything in particular you'd like reviewers to focus on?
JavaIsoVisitor
pass—does it still scale on large projects?Anyone you would like to review specifically?
Have you considered any alternatives or workarounds?
MethodMatcher
or type-based override detection; this felt like overkill for a simple annotation check.Any additional context
mvn test
.Checklist