Skip to content
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

Merged
merged 1 commit into from
Mar 14, 2025

Conversation

timtebeek
Copy link
Contributor

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.

@timtebeek timtebeek added the bug Something isn't working label Mar 13, 2025
@timtebeek timtebeek requested a review from sambsnyd March 13, 2025 22:22
@timtebeek timtebeek self-assigned this Mar 13, 2025
@knutwannheden
Copy link
Contributor

How did you find these cases? I would quite like to annotate all with-methods as @Contract(pure=true) and we could indeed do that using @With(onMethod=...). I have also contemplated using the Lombok SPI to register our own handler for@With (or possibly a custom annotation) to make it aware of padding using JLeftPadded etc. That could then automatically declare the methods as pure.

@timtebeek
Copy link
Contributor Author

timtebeek commented Mar 14, 2025

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 withMessageContaining, as well as with-ers that return this instead of a new copy. Still it was helpful to identify these cases, and the one in rewrite-python, as it's an easy mistake to make:

@knutwannheden
Copy link
Contributor

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.

@timtebeek
Copy link
Contributor Author

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?

@knutwannheden
Copy link
Contributor

Definitely, yes. I didn't mean to stop the PR from getting merged.

@timtebeek timtebeek merged commit 7176624 into main Mar 14, 2025
2 checks passed
@timtebeek timtebeek deleted the assign-wither-response branch March 14, 2025 17:41
@sambsnyd
Copy link
Member

Good catch Tim!

@timtebeek
Copy link
Contributor Author

Thanks! There's one more similar case in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants