-
Notifications
You must be signed in to change notification settings - Fork 507
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
-F produces false-positive with experimental rules enabled #1367
Comments
I can not reproduce the problem with current development version of ktlint. Can you please verify that your problem is solved with latest snapshot version? |
EDIT: No idea how it succeeded for me the first time I tried... Updated the fork to latest snaphot version and it's still failing. |
Could it be that you expectations are not correct? The message below will be printed when running without option
When running with option
|
Hmm. You are mislead a bit. Let me try to explain my test. The code I've provided in "expected behaviour" does not contain unused imports. Neither does the automatically fixed result. Yet, ktlint reports that there is an unused import (as demonstrated in ExperimentalCLITest I've provided). I found out that no unused imports rule does not parse EnumThree correctly. Under "refs" variable it contains "EnumThree," and thus reporting "EnumThree" as unused import. One possible fix would be to remove commas when cleaning up ref names. But as I am not too familiar with ktlint code I'm hesitant to create an pull request. 😊 |
We seem indeed not to be on the same page.
Note the usage of I rewrote your test case to a normal unit test as using the CLI test to run multiple rules is not required anymore. It is now possible to run a selected list of rules in a single unit test. As a result it is easier to assert results of linting and formatting. Compared to your original example, I have added an import for EnumFour which without any doubts is unused. As you can see in the "formattedCode", the trailing comma was added and EnumThree has not been removed. I think this is the correct output to be expected. Agree?
|
☝️ this expects code to output "Unused import" error but it should not!
Your example with EnumFour is correct but you are not checking LintError output of If we add these lines to your code
then we can see that ktlint still reports an unused import although it is not there. TLDR; |
Thanks for you persistence. Espically you last comment, the screen dump and especially the nice red arrow made finally clear to me what you meant. This is indeed very strange behavior and is related which is caused by the combination of those two rules. It might explain some other problems we have with unused imports. As far as I now can see, the problem is cause at following lines:
Due to the trailing comma rule the reference EnumThree for variable bar3 is transformed to REFERENCE_EXPRESSION. So at a certain moment the vnode variable at this source line consisting of two elements:
So well done for tracking down the bug and helping me to understand it. Fixing it, should not be that hard from this point on. |
… find linting errors. In this process, no unused import are found because the trailing comma is not yet added to variable "bar3". Then in the next stage the rules are run consecutively. Nof the trailing comma rule is adding a trailing comma after the type of variable "bar3". When the no-unused-import rule runs after the trailing-comma rule, it was incorrectly seen as part of the type of variable "bar3" and a reference "EnumThree," (with the trailing comma was added) which in turn resulted in not recognizing that the import of EnumThree actually was used. Closes pinterest#1367
#1368) * When running format mode, the rules are first executed in parallel to find linting errors. In this process, no unused import are found because the trailing comma is not yet added to variable "bar3". Then in the next stage the rules are run consecutively. Nof the trailing comma rule is adding a trailing comma after the type of variable "bar3". When the no-unused-import rule runs after the trailing-comma rule, it was incorrectly seen as part of the type of variable "bar3" and a reference "EnumThree," (with the trailing comma was added) which in turn resulted in not recognizing that the import of EnumThree actually was used. Closes #1367 * Update changelog Co-authored-by: Paul Dingemans <[email protected]> Co-authored-by: Sha Sha Chu <[email protected]>
Expected Behavior
Automatic formatting should add trailing comma and succeed.
Observed Behavior
Trailing comma is added correctly but a false-positive error is thrown.
TrailingCommaTest.kt:4:1: Unused import
Steps to Reproduce
I have created a test case which fails at the moment.
https://github.com/krisgerhard/ktlint/tree/incorrect-experimental-formatting-showcase
Added test file:
ExperimentalCLITest
https://github.com/krisgerhard/ktlint/blob/incorrect-experimental-formatting-showcase/ktlint/src/test/kotlin/com/pinterest/ktlint/ExperimentalCLITest.ktThe text was updated successfully, but these errors were encountered: