-
Notifications
You must be signed in to change notification settings - Fork 389
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
Assign response from withTypeParameters
in SpacesVisitor
#5173
Conversation
How did you find these cases? I would quite like to annotate all with-methods as |
This was for now just a silly local recipe with some false positives, that I had thought to add to rewrite-rewrite & PR checks eventually: import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.marker.SearchResult;
public class UnusedWither extends Recipe {
private static final MethodMatcher WITH_MATCHER = new MethodMatcher("*..*#with*(..)", true);
@Override
public String getDisplayName() {
return "Find unused withers";
}
@Override
public String getDescription() {
return "Find wither methods called, but their return value is not used.";
}
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
JavaIsoVisitor<ExecutionContext> visitor = new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (WITH_MATCHER.matches(method) &&
getCursor().getParentTreeCursor().getValue() instanceof J.Block) {
return SearchResult.found(method);
}
return super.visitMethodInvocation(method, ctx);
}
};
return Preconditions.check(new UsesMethod<>(WITH_MATCHER), visitor);
}
} You'll notice that it picks up any with named method, including things like AssertJ assertion methods like |
I would quite like to have the methods declared as pure, as then IDEA would show a warning when calling these methods as if they had a side effect. |
Agree that'd be nice, but that would likely mean either create our own meta annotation for With methods, which then adds that contract, or add that contract to each usage of the Lombok With annotation. Both seem slightly out of scope for this small fix; can we get this in already? |
Definitely, yes. I didn't mean to stop the PR from getting merged. |
Good catch Tim! |
Thanks! There's one more similar case in |
I have a local recipe that still has some false positives, but already was able to find these two cases where the response to an
@With
annotated method call went unchecked.