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
519 stars 39 forks source link

ERROR org.cqfn.diktat.ruleset.rules.DiktatRule - Internal error has occurred in rule diktat-ruleset:local-variables #1783

Closed dwojtaszek closed 7 months ago

dwojtaszek commented 9 months ago

Describe the bug

diktat exception

Expected behavior

no exception

Observed behavior

14:25:38 pool-1-thread-18 ERROR org.cqfn.diktat.ruleset.rules.DiktatRule - Internal error has occurred in rule diktat-ruleset:local-variables . Please make an issue on this bug at https://github.com/saveourtool/diKTat/.
As a workaround you can disable these inspections in yml config: < LOCAL_VARIABLE_EARLY_DECLARATION >.
Root cause of the problem is in ACM.kt file. java.util.NoSuchElementException: Sequence is empty. at kotlin.sequences.SequencesKt___SequencesKt.last(_Sequences.kt:244) at org.cqfn.diktat.ruleset.rules.chapter3.identifiers.LocalVariablesRule.checkLineNumbers(LocalVariablesRule.kt:166) at org.cqfn.diktat.ruleset.rules.chapter3.identifiers.LocalVariablesRule.handleLocalProperty(LocalVariablesRule.kt:97)
at org.cqfn.diktat.ruleset.rules.chapter3.identifiers.LocalVariablesRule.logic(LocalVariablesRule.kt:60) 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.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:1583)

Steps to Reproduce

Environment information

nulls commented 9 months ago

Hi @dwojtaszek, thanks for reporting the issue. We will look on it

DrAlexD commented 7 months ago

Hi @dwojtaszek, could you please provide the code to reproduce the error?

dwojtaszek commented 7 months ago

does not seem to reproduce for me

I got a different fail now

line 1:5 mismatched input 'and' expecting RPAREN 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.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:1583) Caused by: java.util.NoSuchElementException: Sequence is empty. at kotlin.sequences.SequencesKt___SequencesKt.last(_Sequences.kt:244) at org.cqfn.diktat.ruleset.rules.chapter3.identifiers.LocalVariablesRule.checkLineNumbers(LocalVariablesRule.kt:166) at org.cqfn.diktat.ruleset.rules.chapter3.identifiers.LocalVariablesRule.handleLocalProperty(LocalVariablesRule.kt:97) at org.cqfn.diktat.ruleset.rules.chapter3.identifiers.LocalVariablesRule.logic(LocalVariablesRule.kt:60) at org.cqfn.diktat.ruleset.rules.DiktatRule.visit(DiktatRule.kt:63) ... 17 more

orchestr7 commented 7 months ago

@dwojtaszek any chance to provide us a small snippet of code where we can reproduce an error? Sorry for that, but we are not able to understand the root cause without a reproducer?

Also which version of diktat did you use, when you got the last problem?

dwojtaszek commented 7 months ago
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.MultiGauge
import io.micrometer.core.instrument.Tags
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ActiveBinsMetric(meterRegistry: MeterRegistry, private val binRepository: BinRepository) {

    companion object {
        private const val DELAY = 15000L // 15s
        private const val ACTIVE_BINS_METRIC_NAME = "c.concurrent.bins"
        private const val NUMBER_OF_EGGS_LABEL = "numberOfEggs"
        private const val ALL_ACTIVE_BINS_LABEL = "total"
        private const val EGG_1_BUCKET_LABEL = "1"
        private const val EGG_2_BUCKET_LABEL = "2"
        private const val EGG_3_BUCKET_LABEL = "3"
        private const val EGG_4_5_BUCKET_LABEL = "4-5"
        private const val EGG_7_9_BUCKET_LABEL = "7-9"
        private const val EGG_OVER_10_BUCKET_LABEL = "10+"
        private val numberOfEggsBuckets = setOf(
            EGG_1_BUCKET_LABEL,
            EGG_2_BUCKET_LABEL,
            EGG_3_BUCKET_LABEL,
            EGG_4_5_BUCKET_LABEL,
            EGG_7_9_BUCKET_LABEL,
            EGG_OVER_10_BUCKET_LABEL,
            ALL_ACTIVE_BINS_LABEL)

    }

    private val metric = MultiGauge.builder(ACTIVE_BINS_METRIC_NAME)
        .register(meterRegistry)

    @Scheduled(fixedDelay = DELAY)
    fun queryDB() {
        metric.register(
            binRepository
                .countActiveWithPartsNumber()
                .toRangeMap()
                .map {
                    MultiGauge.Row.of(
                        Tags.of(NUMBER_OF_EGGS_LABEL, it.key),
                        it.value
                    )
                }, true)
    }

    private fun List<NumberOfBinsAndParts>.toRangeMap(): MutableMap<String, Long> {
        var total = 0L;
        val map = mutableMapOf<String, Long>()
        numberOfEggsBuckets.forEach { map[it] = 0 }

        this.forEach {
            total += it.numberOfBins
            when (it.numberOfParts) {
                1 -> map[EGG_1_BUCKET_LABEL] = it.numberOfBins
                2 -> map[EGG_2_BUCKET_LABEL] = it.numberOfBins
                3 -> map[EGG_3_BUCKET_LABEL] = it.numberOfBins
                in 4..5 -> map[EGG_4_5_BUCKET_LABEL] = it.numberOfBins
                in 7..9 -> map[EGG_7_9_BUCKET_LABEL] = it.numberOfBins
                in 10..Int.MAX_VALUE -> map[EGG_OVER_10_BUCKET_LABEL] = it.numberOfBins
            }
        }

        map[ALL_ACTIVE_BINS_LABEL] = total
        return map
    }

}
dwojtaszek commented 7 months ago

apologies for messy paste

nulls commented 7 months ago

apologies for messy paste

changed <code> to ```kotlin

dwojtaszek commented 7 months ago

thank you very much