Fix reporting unused imports at file level #6390
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR was triggered by users of the IntelliJ plugin getting every unused import reported at the file level which clutters the editor: detekt/detekt-intellij-plugin#486 (comment)
This PR partly reverts the change to the emit logic of https://github.com/detekt/detekt/pull/5876/files#diff-9cf77f9ffcbf036bf5044d1ef99728b56aad97854246f3473f69d54b4b929380.
Emiting at the node is wrong in the case of the NoUnusedImports rule as in
beforeVisitChildNodes
the import usages are calculated and only reported inafterVisitChildNodes
from the file node (becausefileNode.imports
access is easy I suppose).With this revert the lines are again calculated with the offset function and lead to the same results as our MaxLineLength rule:

The other changed test cases are also now correct again e,g, the tested code for indentation rule is
val code = "fun main() {\n println()\n}"
so.hasStartSourceLocation(1, 13)
could not be correct due to the line breaks.Fixes #6105