detekt / detekt

Static code analysis for Kotlin
https://detekt.dev
Apache License 2.0
6.07k stars 751 forks source link

UnusedPrivateProperty does not detect unused property named "logger" #7222

Open Chris-Goetz opened 3 weeks ago

Chris-Goetz commented 3 weeks ago

Expected Behavior

detekt should find unused properties for which no exception is defined.

Observed Behavior

detekt does not find unused properties named "logger".

Steps to Reproduce

Screenshot from 2024-04-24 13-29-18

Context

The documentation statesthe default allowed values are: allowedNames (default: '_|ignored|expected|serialVersionUID') - "logger" is not listed here. We also have no exceptions in our config for that rule defined:

---
build:
  weights:
    LongParameterList: 0
    TooManyFunctions: 0

complexity:
  excludes: ["**/test/**"]

formatting:
  MaximumLineLength:
    excludes: ["**/test/**"]
  NoWildcardImports:
    active: false
  TrailingCommaOnCallSite:
    active: true
  TrailingCommaOnDeclarationSite:
    active: true

naming:
  FunctionParameterNaming:
    excludes: ["**/test/**"]
  ClassNaming:
    excludes: ["**/test/**"]
  MemberNameEqualsClassName:
    active: false

style:
  MaxLineLength:
    excludes: ["**/test/**"]
  WildcardImport:
    active: false
  SerialVersionUIDInSerializableClass:
    active: false

config:
  validation: true

Your Environment

Kotlin: 1.9.20 Groovy: 3.0.17 Ant: Apache Ant(TM) version 1.10.13 compiled on January 4 2023 JVM: 17.0.10 (Private Build 17.0.10+7-Ubuntu-122.04.1) OS: Linux 6.5.0-28-generic amd64

atulgpt commented 3 weeks ago

Below TC is passing though

@Test
fun `does report private logger property in top level`() {
    val code = """
       private val logger = 2
    """.trimIndent()

    assertThat(subject.lintWithContext(env, code))
        .hasSize(1)
}

is your type resolution set properly?

Chris-Goetz commented 1 week ago

Hi, sorry for the late answer, one of my Outlook filters has eaten up the notification.

I've tried to run 'detektMain' wich gave me a lot of new findings but the unused logger property was still ignored. Then I tried to assign the value '2' to logger, like in your test. That was found by both, the 'detekt' and the 'detektMain' task. After that, I tried to assing 'private val logger = Logger.getLogger("foo")' and that was also found.

It must have something to do with the 'KotlinLogging.logger {}'.