https://kotlinlang.org logo
Join Slack
Powered by
# detekt
  • m

    maarten ha

    05/02/2025, 12:03 PM
    How can I setup Detekt with ktlint correctly that when I use the reformat shortcut in Intelij it formats to the Detekt configuration? Both Detekt and ktlint plugins are installed in my IDE. Current gradle configuration
    Copy code
    tasks {
        withType<io.gitlab.arturbosch.detekt.Detekt> {
            parallel = true
            config.setFrom(files("/config/detekt/detekt.yml"))
            buildUponDefaultConfig = true
            jvmTarget = "$projectJvmTarget"
            autoCorrect = true
    
            setSource(files("src/main/kotlin", "src/test/kotlin"))
            setOf(
                "**/*.kt",
                "**/*.kts",
                ".*/resources/.*",
                ".*/build/.*",
                "/versions.gradle.kts",
            ).forEach {
                include(it)
            }
    
            reports {
                html.required.set(true)
                html.outputLocation.set(file("reports/detekt/detekt.html"))
    
                sarif.required.set(true)
                sarif.outputLocation.set(file("reports/detekt/detekt.sarif"))
            }
    
            outputs.dir("reports/detekt/")
        }
    }
    
    dependencies {
    ...
     detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.23.8")
    }
    For the detekt config I just ran the generate config task. Have not changed anything in it
    s
    d
    • 3
    • 2
  • h

    hfhbd

    05/15/2025, 8:29 AM
    Does Detekt support processing files with 2.2.0-Beta2 and enabled context parameters?
    b
    • 2
    • 1
  • o

    Oleksandr Balan

    06/11/2025, 10:08 AM
    Hey 👋 I would like to set up a custom Gradle task that runs Detekt with type resolution, using a single custom rule. This needs to work in a multi-modular project that includes both JVM and Android library modules. I'm currently struggling with setting up the correct classpath (I've read it can be tricky). Has anyone dealt with a similar setup and can share how they configured it? Thanks for any advice.
    x
    • 2
    • 3
  • e

    Ernestas

    06/19/2025, 5:48 AM
    Hey, as I understand the detekt plugin is not able to show live warnings for rules that require type resolution. I would still like those rules to be checked during development, is there a way to configure all rules to run on each build? I tried creating a task dependency, but
    build.gradle
    does not see the task -
    detektMain
    , only -
    detekt
    is available. I can still run the
    detektMain
    task manually without problems though.
  • m

    Maxime Michel

    07/09/2025, 11:46 AM
    Hey guys, I have a very very specific issue with Detekt currently and I'm wondering if any eagle-eyed amongst you would be able to help me identify the source of the problem and hopefully a possible fix ^^ I'm currently working on a KMP project and this project uses Compose Multiplatform (CMP). I have also set up convention plugins to help centralise the behaviour of each module. Here is a global overview of the project structure omitting anything that doesn't help understand the issue.
    Copy code
    my-project:
    -> android
       |-> androidApp (Android application module)
       |-> other android specific modules
    -> build-logic
       |-> convention
           |-> Detekt.kt (Detekt specific configurations are contained here)
           |-> MultiplatformLibraryConventionPlugin.kt
    -> ios (iOS application)
    -> kmp
       |-> data
       |-> domain
       |-> presentation
    -> testing
       |-> detekt
       |-> konsist
    This is a somewhat generic KMP project structure I think (but this works without issues so I'm not planning on changing this structure). The contents of the
    :testing:detekt
    module is attached as an image. I followed the documentation and updated my
    Detekt.kt
    file to apply (or so I thought) the new rule I created:
    Copy code
    internal fun Project.configureDetekt() {
        configure<DetektExtension> {
            source.from(files("$projectDir/src"))
            config.from(files(
                "$rootDir/config/quality/detekt/detekt-config.yml",
                "$rootDir/testing/detekt/src/main/resources/config/config.yml",
            ))
            baseline = file("$rootDir/config/quality/detekt/baseline.xml")
            autoCorrect = true
        }
    
        tasks.withType<Detekt> {
            exclude("**/test/")
            exclude(".*/tmp/.*")
            dependsOn(":testing:detekt:assemble")
        }
    
        dependencies {
            CONFIGURATION_DETEKT_PLUGINS(libs.findLibrary("detekt.cli").get())
            CONFIGURATION_DETEKT_PLUGINS(libs.findLibrary("detekt.formatting").get())
            CONFIGURATION_DETEKT_PLUGINS(libs.findLibrary("detekt.libraries").get())
            CONFIGURATION_DETEKT_PLUGINS(project(":testing:detekt"))
        }
    }
    In my custom rule, I added an override for the
    visitKtFile
    function that simply prints the file name but this does nothing and I see no logs when running the detekt task.
    • 1
    • 1
  • v

    Vanessa Johnson

    07/14/2025, 1:19 PM
    Hi, I have a PR up with some work to integrate the new problems api as a part of GSOC and any feedback would be appreciated! Thanks. The link will be in 🧵
    • 1
    • 1
  • e

    Eugen Martynov

    07/14/2025, 2:02 PM
    Does detekt have for body expression functions that return Unit and have no explicit return type specification?
    ✅ 1
    b
    • 2
    • 11
  • a

    Aditya Bhaskar

    07/14/2025, 5:17 PM
    Hey folks, I'm updating our projects to Kotlin 2.2.0 and detekt appears to be fairly incompatible. I'm getting lots of false
    UnusedPrivateMember
    and
    NullableToStringCall
    errors, and a few ISEs:
    Copy code
    > A failure occurred while executing io.gitlab.arturbosch.detekt.invoke.DetektWorkAction
       > java.lang.IllegalStateException: Analyzing something.kt led to an exception.
         Location: java.base/java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source)
         The original exception message was: java.lang.NullPointerException
         Running detekt '1.23.8' on Java '21.0.6+-13391695-b895.109' on OS 'Mac OS X'
         If the exception message does not help, please feel free to create an issue on our GitHub page.
    Given this PR is still draft, is this to be expected?
    b
    g
    n
    • 4
    • 20
  • r

    Rob

    07/27/2025, 4:34 PM
    Hello All! I've published a ruleset that ensures correct handling of coroutine cancellation in catch blocks. Please try it out and provide feedback. Thanks! https://github.com/zirman/robs-rules/
    K 1
    b
    s
    • 3
    • 14
  • j

    juliocbcotta

    08/05/2025, 2:11 PM
    I am migrating my project o kotlin 2.2.0 and getting lots of false positive... for instance UnusedPrivateMember for
    Copy code
    private fun FSProductToAdd.toOrderPipeModel(): OPProductToAdd {
        return OPProductToAdd(
            productId = productId,
            quantity = quantity
        )
    }
    even thought that is called inside of the class... I also got a bunch of RedundantSuspendModifier
    Copy code
    private suspend fun register() = suspendCoroutine { cont ->
    // do integration with callbacks from tracking library
    }
    also UnreachableCode
    Copy code
    internal fun decodeJwt(token: String): JSONObject? {
        var stringToDecode = (token.split('.').getOrNull(1) ?: return null)
            .replace('-', '+')
            .replace('_', '/')
    
        @Suppress("MagicNumber")
        when (stringToDecode.codePointCount(0, stringToDecode.lastIndex) % 4) {
            2 -> stringToDecode += "=="
            3 -> stringToDecode += '='
            else -> {
                // nothing to do
            }
        }
    
    --> line 32    return try {
            stringToDecode.decodeBase64()?.utf8()?.let(::JSONObject)
        } catch (e: JSONException) {
            Timber.e(e, "Tried to decode a JSON Web Token that is not JSON!")
            null
        }
    }
    saying
    JwtDecoder.kt:32:5: This expression is unreachable code which should either be used or removed. [UnreachableCode]
    ➕ 1
    b
    d
    • 3
    • 4
  • u

    ursus

    08/27/2025, 3:13 PM
    Is there a rule about
    max file lines
    ? (Can't find it) That's something I presonally care more about than number of functions, lines per class etc
    b
    s
    • 3
    • 6
  • s

    segunfamisa

    09/01/2025, 12:33 PM
    hey folks, i notice that detekt baselines don't have any information about the location source of the current issues. I don't know if this was considered in the past and decided against, but I think it could lead to false positives. for example, if I take ForbiddenSuppress rule and I have
    MaximumLineLength
    forbidden and the baseline updated
    Copy code
    @Suppress("MaximumLineLength")
    class Bar
    If for any reason, I have to modify that suppress rule, to something like:
    Copy code
    @Suppress("MaximumLineLength", "RandomOtherRule")
    class Bar
    This entire line is going to be reported despite the fact that a new rule suppression is introduced, and the "already known" issue was not changed
    b
    • 2
    • 6
  • r

    Ryan Ulep

    09/03/2025, 1:01 PM
    Hey all. Question (as I can't find the thread after some searching and scrolling): is there a planned release date for Detekt 2.0? Do we know what features are going to be included (e.g. Kotlin 2+ language support, compiler plugin support, etc)?
    g
    e
    • 3
    • 9
  • s

    seb

    09/04/2025, 5:16 PM
    @gammax looks like auto-fix is broken, at least in the IDE plugin: https://github.com/detekt/detekt-intellij-plugin/issues/533
  • g

    gammax

    09/04/2025, 5:20 PM
    I don’t have much context on the IdE plugin sadly
  • s

    seb

    09/04/2025, 5:36 PM
    I may be one of the biggest contributors lol
    ❤️ 2
  • s

    seb

    09/04/2025, 5:36 PM
    I need to check if the Gradle plugin actually does auto-fix
  • s

    seb

    09/04/2025, 5:36 PM
    Or if the auto-fix mechanism is completely broken
  • s

    seb

    09/04/2025, 6:02 PM
    Looks like setting
    detekt.autoCorrect
    in Gradle also doesn't do auto-correct when I run
    :detekt
    . Is there some other thing I need to do to enable auto-correct for the rule?
  • e

    eygraber

    09/04/2025, 7:09 PM
    I'm seeing a few instances of this in the logs when running detekt 2.0.0-alpha.0
    There were 11 compiler errors found during legacy compiler analysis. This affects accuracy of reporting.
    Do I need to use the compiler plugin to resolve those?
    b
    • 2
    • 2
  • g

    gammax

    09/04/2025, 7:10 PM
    Please report an issue for both folks
  • n

    Nicholas Doglio

    09/04/2025, 7:13 PM
    Is there going to be a migration guide? Parsing the changelog now and I think I'm able to find what I need but as more things changes having a central concise list changes to make would be helpful 🙌
    b
    e
    g
    • 4
    • 5
  • g

    Gama11

    09/05/2025, 8:29 AM
    is the alpha only available as a github release for now? can't seem to find it in maven central / gradle plugin portal
    h
    g
    • 3
    • 4
  • g

    Gama11

    09/05/2025, 9:18 AM
    testing the alpha against our project right now. seems to be extremely slow, taking over 15 minutes for some modules already. is there by chance a way to output time spent per rule or something like that to narrow down the reason?
    g
    b
    • 3
    • 8
  • b

    Brais Gabin

    09/05/2025, 10:12 AM
    I'm working on the migration guide. It is still a Draft-PR but maybe it help someone: https://github.com/detekt/detekt/pull/8610 Right now it's only for rule authors. I hope to have something for the users today too. Any feedback is more than welcome.
    ❤️ 3
  • j

    Joe

    09/05/2025, 10:45 PM
    I may be missing something in the migration (changed version, detekt-cli's maven groupId, and Main class package coordinate) but getting this when I try updating to 2.0.0-alpha.0 using antrun maven plugin (looks like there's a 0-all.jar, but not a 0.jar for both of these?):
    Copy code
    Downloading from central: <https://repo.maven.apache.org/maven2/dev/detekt/detekt-kotlin-analysis-api/2.0.0-alpha.0/detekt-kotlin-analysis-api-2.0.0-alpha.0.jar>
    Downloading from central: <https://repo.maven.apache.org/maven2/dev/detekt/detekt-kotlin-analysis-api-standalone/2.0.0-alpha.0/detekt-kotlin-analysis-api-standalone-2.0.0-alpha.0.jar>
     [.... snip reactor summary ...]
    Execution detekt of goal org.apache.maven.plugins:maven-antrun-plugin:3.1.0:run failed: Plugin org.apache.maven.plugins:maven-antrun-plugin:3.1.0 or one of its dependencies could not be resolved: The following artifacts could not be resolved: dev.detekt:detekt-kotlin-analysis-api:jar:2.0.0-alpha.0 (absent), dev.detekt:detekt-kotlin-analysis-api-standalone:jar:2.0.0-alpha.0 (absent): Could not find artifact dev.detekt:detekt-kotlin-analysis-api:jar:2.0.0-alpha.0 in central (<https://repo.maven.apache.org/maven2>)
    ➕ 1
  • u

    ursus

    09/05/2025, 11:39 PM
    Does the new 2.0 mean that type resolution is always on, i.e. there is distinction between the rules anymore?
    b
    n
    • 3
    • 5
  • r

    Ryan Ulep

    09/08/2025, 1:02 PM
    The new
    detekt-compiler-plugin
    should work with Kotlin 2.+ correct? I'm getting:
    Copy code
    error: there are some plugins incompatible with language version 2.0:
      io.github.detekt.compiler.plugin.DetektCompilerPluginRegistrar
    Please use language version 1.9 or below
    g
    b
    • 3
    • 7
  • u

    ursus

    09/11/2025, 7:41 PM
    How do I create the most light task that has
    type resolution
    enabled? If I run
    detektMain
    I'm getting both debug & release. Is that normal? (and release stuff takes way longer)
    a
    b
    • 3
    • 5
  • u

    ursus

    09/12/2025, 5:13 PM
    I have a sort of DSLy interface with many implementations where
    CognitiveComplexMethod
    blows up. Is there a global way to have implementations of such interface to not be analyzed?
    b
    • 2
    • 2