saveourtool / diktat

Strict coding standard for Kotlin and a custom set of rules for detecting code smells, code style issues and bugs
https://diktat.saveourtool.com
MIT License
521 stars 39 forks source link

NPE in NewlinesRule.kt #1779

Closed illarionov closed 9 months ago

illarionov commented 10 months ago

Describe the bug

Diktat fails with NullPointerException on the following Kotlin file:

val TEST_FUNC = { _: Int, _: Set<Int>, _: Int, ->
}

Stacktrace:

08:50:58.057 [pool-1-thread-1] ERROR org.cqfn.diktat.ruleset.rules.DiktatRule - Internal error has occurred in rule [diktat-ruleset:newlines]. Please make an issue on this bug at https://github.com/saveourtool/diKTat/.
                       As a workaround you can disable these inspections in yml config: <[COMPLEX_EXPRESSION, REDUNDANT_SEMICOLON, WRONG_NEWLINES]>.
                       Root cause of the problem is in [Test.kt] file.
java.lang.NullPointerException: it.treeNext must not be null
        at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule$handleValueParameterList$1.invoke(NewlinesRule.kt:517)
        at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule$handleValueParameterList$1.invoke(NewlinesRule.kt:516)
        at kotlin.sequences.FilteringSequence$iterator$1.calcNext(Sequences.kt:171)
        at kotlin.sequences.FilteringSequence$iterator$1.hasNext(Sequences.kt:194)
        at kotlin.sequences.SequencesKt___SequencesKt.toCollection(_Sequences.kt:787)
        at kotlin.sequences.SequencesKt___SequencesKt.toMutableList(_Sequences.kt:817)
        at kotlin.sequences.SequencesKt___SequencesKt.toList(_Sequences.kt:808)
        at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule.handleValueParameterList(NewlinesRule.kt:521)
        at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule.handleList(NewlinesRule.kt:482)
        at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule.logic(NewlinesRule.kt:142)
        at org.cqfn.diktat.ruleset.rules.DiktatRule.visit(DiktatRule.kt:63)
        at org.cqfn.diktat.ruleset.rules.OrderedRuleSet$Companion$OrderedRule.visit(OrderedRuleSet.kt:92)
        at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:148)
        at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:141)
        at com.pinterest.ktlint.core.internal.VisitorProvider$concurrentVisitor$1$1.invoke(VisitorProvider.kt:102)
        at com.pinterest.ktlint.core.internal.VisitorProvider$concurrentVisitor$1$1.invoke(VisitorProvider.kt:96)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:236)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:237)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:237)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:237)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:237)
        at com.pinterest.ktlint.core.internal.VisitorProvider$concurrentVisitor$1.invoke(VisitorProvider.kt:96)
        at com.pinterest.ktlint.core.internal.VisitorProvider$concurrentVisitor$1.invoke(VisitorProvider.kt:95)
        at com.pinterest.ktlint.core.KtLint.lint(KtLint.kt:141)
        at com.pinterest.ktlint.internal.FileUtilsKt.lintFile(FileUtils.kt:174)
        at com.pinterest.ktlint.internal.KtlintCommandLine.process(KtlintCommandLine.kt:384)
        at com.pinterest.ktlint.internal.KtlintCommandLine.access$process(KtlintCommandLine.kt:48)
        at com.pinterest.ktlint.internal.KtlintCommandLine$lintFiles$3.invoke$lambda-1(KtlintCommandLine.kt:274)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at java.base/java.lang.Thread.run(Thread.java:1623)
Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.Error: Internal error in diktat application
        at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122)
        at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191)
        at com.pinterest.ktlint.internal.KtlintCommandLine.parallel(KtlintCommandLine.kt:578)
        at com.pinterest.ktlint.internal.KtlintCommandLine.parallel$default(KtlintCommandLine.kt:536)
        at com.pinterest.ktlint.internal.KtlintCommandLine.lintFiles(KtlintCommandLine.kt:283)
        at com.pinterest.ktlint.internal.KtlintCommandLine.run(KtlintCommandLine.kt:235)
        at com.pinterest.ktlint.Main.main(Main.kt:31)
Caused by: java.lang.Error: Internal error in diktat application
        at org.cqfn.diktat.ruleset.rules.DiktatRule.visit(DiktatRule.kt:74)
        at org.cqfn.diktat.ruleset.rules.OrderedRuleSet$Companion$OrderedRule.visit(OrderedRuleSet.kt:92)
        at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:148)
        at com.pinterest.ktlint.core.KtLint$lint$1.invoke(KtLint.kt:141)
        at com.pinterest.ktlint.core.internal.VisitorProvider$concurrentVisitor$1$1.invoke(VisitorProvider.kt:102)
        at com.pinterest.ktlint.core.internal.VisitorProvider$concurrentVisitor$1$1.invoke(VisitorProvider.kt:96)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:236)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:237)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:237)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:237)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:237)
        at com.pinterest.ktlint.core.internal.VisitorProvider$concurrentVisitor$1.invoke(VisitorProvider.kt:96)
        at com.pinterest.ktlint.core.internal.VisitorProvider$concurrentVisitor$1.invoke(VisitorProvider.kt:95)
        at com.pinterest.ktlint.core.KtLint.lint(KtLint.kt:141)
        at com.pinterest.ktlint.internal.FileUtilsKt.lintFile(FileUtils.kt:174)
        at com.pinterest.ktlint.internal.KtlintCommandLine.process(KtlintCommandLine.kt:384)
        at com.pinterest.ktlint.internal.KtlintCommandLine.access$process(KtlintCommandLine.kt:48)
        at com.pinterest.ktlint.internal.KtlintCommandLine$lintFiles$3.invoke$lambda-1(KtlintCommandLine.kt:274)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: java.lang.NullPointerException: it.treeNext must not be null
        at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule$handleValueParameterList$1.invoke(NewlinesRule.kt:517)
        at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule$handleValueParameterList$1.invoke(NewlinesRule.kt:516)
        at kotlin.sequences.FilteringSequence$iterator$1.calcNext(Sequences.kt:171)
        at kotlin.sequences.FilteringSequence$iterator$1.hasNext(Sequences.kt:194)
        at kotlin.sequences.SequencesKt___SequencesKt.toCollection(_Sequences.kt:787)
        at kotlin.sequences.SequencesKt___SequencesKt.toMutableList(_Sequences.kt:817)
        at kotlin.sequences.SequencesKt___SequencesKt.toList(_Sequences.kt:808)
        at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule.handleValueParameterList(NewlinesRule.kt:521)
        at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule.handleList(NewlinesRule.kt:482)
        at org.cqfn.diktat.ruleset.rules.chapter3.files.NewlinesRule.logic(NewlinesRule.kt:142)
        at org.cqfn.diktat.ruleset.rules.DiktatRule.visit(DiktatRule.kt:63)
        ... 21 more

Environment information

nulls commented 10 months ago

Hi @illarionov, thanks for bug reporting. We will look into it