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

Static import is removed and leaves ambiguous reference behind #4450

Merged
merged 14 commits into from
Sep 6, 2024

Conversation

Laurens-W
Copy link
Contributor

@Laurens-W Laurens-W commented Aug 27, 2024

What's your motivation?

RemoveUnusedImports recipe should not remove static import when this leaves an ambiguous reference behind

Anyone you would like to review specifically?

@timtebeek

Have you considered any alternatives or workarounds?

Any additional context

static types were not being added to the typesInUse object on a CompilationUnit

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@Laurens-W Laurens-W added the bug Something isn't working label Aug 27, 2024
@Laurens-W Laurens-W self-assigned this Aug 27, 2024
Copy link
Contributor

@github-actions github-actions bot left a 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:

  • rewrite-test/src/test/java/org/openrewrite/test/internal/RewriteTestTest.java
    • lines 107-107

@timtebeek
Copy link
Contributor

Capturing the failure here:

expected:

  package org.test;	
  	
  import static org.a.ABC.*;	
  import static org.b.DEF.*;	
  import static org.a.ABC.ALL;	
  	
  public class Test {	
    private String abc = ALL;	
    private String a = A;	
    private String b = B;	
    private String c = C;	
    private String d = D;	
    private String e = E;	
    private String f = F;	
  }

but was:

  package org.test;	
  	
  import static org.a.ABC.*;	
  import static org.b.DEF.*;	
  	
  public class Test {	
    private String abc = ALL;	
    private String a = A;	
    private String b = B;	
    private String c = C;	
    private String d = D;	
    private String e = E;	
    private String f = F;	
  }

Copy link
Contributor

@github-actions github-actions bot left a 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:

  • rewrite-test/src/test/java/org/openrewrite/test/internal/RewriteTestTest.java
    • lines 107-107

@timtebeek
Copy link
Contributor

This indeed seems to match the case reported, thanks! Lets look at what we can do to fix the issue.

@timtebeek
Copy link
Contributor

Perhaps good to briefly evaluate if we can fold in a fix for this issue as well

@timtebeek timtebeek self-assigned this Sep 3, 2024
@Laurens-W Laurens-W force-pushed the remove-unused-imports-ambiguous branch from 209ce4d to 27fd9a1 Compare September 4, 2024 13:35
@timtebeek timtebeek marked this pull request as ready for review September 6, 2024 08:34
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

I've reverted the earlier change to TypesInUse, as in reality it's not that type that's used, but the variables that were already part of TypesInUse. Using just the imports, the variables, and the owner of those variables I was able to do a change local to RemoveUnusedImports to detect ambiguous member references from wildcards, and not make changes in those cases. At the same time I covered an additional case where we can clear out one explicit import due to wildcards imports while keeping another explicit import.

This should ensure there's minimal impact from the change, as we don't want to incorrectly assign types as used, and risk those being left over as imports elsewhere when in reality not used.

Thanks for getting this started!

@timtebeek timtebeek merged commit c339007 into main Sep 6, 2024
2 checks passed
@timtebeek timtebeek deleted the remove-unused-imports-ambiguous branch September 6, 2024 17:06
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.

2 participants