Skip to content

Imports only used in blocks which may not be formatted are reported as unused #2696

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

Closed
paul-dingemans opened this issue Jun 12, 2024 · 0 comments · Fixed by #2720 or #2725
Closed

Imports only used in blocks which may not be formatted are reported as unused #2696

paul-dingemans opened this issue Jun 12, 2024 · 0 comments · Fixed by #2720 or #2725
Milestone

Comments

@paul-dingemans
Copy link
Collaborator

Based on nbadal/ktlint-intellij-plugin#535 (comment)

In ktlint 1.3.0 an import which is only used inside a block which is excluded from formatting, is reported as being an unused import. Removing the import results in code which can not be compiled.

import bar.Bar1
import bar.Bar2

fun foo123() {
    // @formatter:off
    // Assume that class Bar1 is defined in package. The import
    // for this class is reported as unused
    Bar1()
    // @formatter:on

    // Assume that class Bar1 is defined in package. The import
    //    // for this class is not reported as unused
    Bar2()
}
@paul-dingemans paul-dingemans added this to the 1.3.1 milestone Jun 12, 2024
paul-dingemans added a commit that referenced this issue Jun 26, 2024
Imports which are only used in code blocks which are suppressed for ktlint should not be reported as unused as removal results in compilation errors.

Refactored the code so that a rule can be marked with interface `IgnoreKtlintSuppressions` to indicate that all suppression for this rule are to be ignored.

Closes #2696
paul-dingemans added a commit that referenced this issue Jul 2, 2024
Imports which are only used in code blocks which are suppressed for ktlint should not be reported as unused as removal results in compilation errors.

Refactored the code so that a rule can be marked with interface `IgnoreKtlintSuppressions` to indicate that all suppression for this rule are to be ignored.

* Refactor rebuilding suppression locator

The former SuppressionLocator lambda and object SuppressionLocatorBuilder are now replaced with the class SuppressionLocator. Upon each check whether a ASTNode has to be suppressed, it is also determined whether it is needed to rebuild the suppression hints. This is only needed when the code that is represented by the AST has been changed.

The CodeFormatter no longer has to rebuild the suppression locator after autocorrect. This responsibility is now moved into the RuleExecutionContext and SuppressionLocator itself.

Closes #2696
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant