Skip to content

Keep plain import when type still used after static-import rewrite#24

Merged
electrum merged 1 commit into
airlift:mainfrom
losipiuk:lo/fix-static-import-plain-import-drop
Jun 10, 2026
Merged

Keep plain import when type still used after static-import rewrite#24
electrum merged 1 commit into
airlift:mainfrom
losipiuk:lo/fix-static-import-plain-import-drop

Conversation

@losipiuk

Copy link
Copy Markdown
Contributor

Summary

  • StaticImportRuleNormalizer rewrites required-static-import calls (e.g. ImmutableSet.toImmutableSet()) to their unqualified form and adds the static import. It also unconditionally removed the plain import for the type, which dropped a still-needed import whenever the type was referenced elsewhere — for example ImmutableSet.of("5") next to ImmutableSet.toImmutableSet() — producing uncompilable output.
  • Fix: stop removing the plain import in this normalizer and defer to the UnusedImportNormalizer phase that already runs afterward, which removes the import only when the type is no longer referenced.
  • Updated testFormatterFixesRequiredStaticImports (its prior expectation encoded the buggy dropped imports) and added two regression tests: one for the reported ImmutableSet.of + toImmutableSet case (import kept) and a control where only toImmutableSet is used (import removed).

Test plan

  • mvnd test -pl airstyle-core -am — all 1549 tests pass, including checkstyle and license checks

@losipiuk losipiuk force-pushed the lo/fix-static-import-plain-import-drop branch from 910c317 to 1fb6104 Compare June 10, 2026 22:24
StaticImportRuleNormalizer rewrites required-static-import calls such as
ImmutableSet.toImmutableSet() to their unqualified form and adds the
static import. It also unconditionally removed the plain import for the
type, which dropped a still-needed import when the type was referenced
elsewhere (e.g. ImmutableSet.of alongside ImmutableSet.toImmutableSet),
producing uncompilable output.

Stop removing the plain import here and defer to the UnusedImportNormalizer
phase that already runs afterward, which removes it only when the type is
no longer referenced.
@losipiuk losipiuk force-pushed the lo/fix-static-import-plain-import-drop branch from 1fb6104 to 8bdae09 Compare June 10, 2026 22:24
@losipiuk

Copy link
Copy Markdown
Contributor Author

@electrum PTAL when you have a chance

@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@losipiuk, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 57 minutes and 9 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 891f449e-d035-45c8-be99-5151b51ded4a

📥 Commits

Reviewing files that changed from the base of the PR and between 910c317 and 8bdae09.

📒 Files selected for processing (2)
  • airstyle-core/src/main/java/io/airlift/airstyle/normalizer/StaticImportRuleNormalizer.java
  • airstyle-core/src/test/java/io/airlift/airstyle/normalizer/TestStaticImportRuleNormalizer.java
📝 Walkthrough

Walkthrough

This PR modifies StaticImportRuleNormalizer to defer removal of plain imports when rewriting qualified static method calls to static-import form. Previously, the normalizer would rewrite the call, add the static import, and immediately queue the plain import for removal. Now it leaves the plain import in place, with a comment indicating it will be handled by a later unused-import cleanup step. Test expectations are updated to reflect this change, and two new tests validate the behavior: one confirms plain imports are retained when the type is still referenced, the other confirms they are removed when only needed for static-import support.

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes an import-removal bug in the formatter pipeline where StaticImportRuleNormalizer could drop a still-needed non-static (plain) type import when rewriting required static-import calls, leading to uncompilable output. The fix defers plain-import removal to the existing UnusedImportNormalizer phase, which can safely remove the import only when the type is truly unused.

Changes:

  • Stop removing the plain import for a type when rewriting required static-import method invocations (leave cleanup to UnusedImportNormalizer).
  • Update the existing required-static-import formatting expectation to keep still-used plain imports.
  • Add regression tests covering both “plain type still used” (import kept) and “type only used for static rewrite” (import removed by unused-import pass).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
airstyle-core/src/main/java/io/airlift/airstyle/normalizer/StaticImportRuleNormalizer.java Prevents premature removal of plain imports during required static-import rewrites, avoiding dropped-needed-import compilation failures.
airstyle-core/src/test/java/io/airlift/airstyle/normalizer/TestStaticImportRuleNormalizer.java Updates expectations and adds regression coverage for keeping/removing plain imports appropriately after static-import rewrites.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@electrum electrum merged commit 78e7443 into airlift:main Jun 10, 2026
1 check passed
ksobolew added a commit to ksobolew/airstyle that referenced this pull request Jun 17, 2026
There are various ways the type can remain used; the existing test case
covers the case when the type is used to invoke a static method (other
than the ones that are meant to be static-imported), these new test
cases covers when the type is used as a method parameter or return type.
Fortunately the fix in airlift#24 fixes those cases as well.
ksobolew added a commit to ksobolew/airstyle that referenced this pull request Jun 17, 2026
There are various ways the type can remain used; the existing test case
covers the case when the type is used to invoke a static method (other
than the ones that are meant to be static-imported), these new test
cases covers when the type is used as a method parameter or return type.
Fortunately the fix in airlift#24 fixes those cases as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants