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

Do not use String equals for Groovy either #142

Merged
merged 2 commits into from
Jul 27, 2023

Conversation

timtebeek
Copy link
Contributor

What's changed?

Add a GroovyFileChecker as precondition to StringLiteralEquality.

What's your motivation?

Fixes #140

@timtebeek timtebeek added the bug Something isn't working label Jul 22, 2023
@timtebeek timtebeek requested a review from kunli2 July 22, 2023 09:24
@timtebeek timtebeek self-assigned this Jul 22, 2023

// Don't change for Groovy because in Groovy, `==` means structural equality and it's redundant to call equals().
@Test
void doNotChangeForGroovy() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a similar separate class to test Kotlin; Was wondering if we shouldn't just collapse these into @Nested test classes in StringLiteralEqualityTest in package org.openrewrite.staticanalysis.

@timtebeek timtebeek requested a review from joanvr July 24, 2023 08:12
/**
* Add a search marker if vising a Groovy file
*/
public class GroovyFileChecker<P> extends TreeVisitor<Tree, P> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably make this a bit more generic, so that it can cover even more ground.

@Value
public class IsInstanceOf<P> extends TreeVisitor<Tree, P> {
    Class<? extends Tree> treeType;

    @Nullable
    public Tree visit(@Nullable Tree tree, P p) {
        if (tree != null && treeType.isInstance(tree.getClass()) {
            return SearchResult.found(tree);
        }
        return tree;
    }
}

Then this may be a candidate for inclusion down in rewrite-core.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quite like this suggestion, thanks! Especially eyeing extended support coming up for Groovy, Kotlin, Typescript, Python, ... would help to handle this once in /rewrite and not repeat ourselves per module with such checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kunli2 Since you're a little more involved with new language development and these exclusions, where would you see this fit best? And would you be willing to take this on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logged as an improvement here, since I'd like to see this fix roll out before we get a response & rework through.
#144
Thanks for the suggestion!

Copy link
Contributor

@joanvr joanvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me. Aside from the more generic approach suggested by @shanman190 , I think it actually fixes the purpose of the PR. So, maybe we can merge this one and iterate over it when we have capacity

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
Archived in project
Development

Successfully merging this pull request may close these issues.

"Use String.equals() on String literals" should not make changes for Groovy
3 participants