Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local variables are treated as properties #2779

Closed
taisuke-fujimoto opened this issue Aug 23, 2024 · 1 comment · Fixed by #2785
Closed

Local variables are treated as properties #2779

taisuke-fujimoto opened this issue Aug 23, 2024 · 1 comment · Fixed by #2785
Milestone

Comments

@taisuke-fujimoto
Copy link

Expected Behavior

This code should not have any lint errors.

package my

import io.kotest.core.spec.style.StringSpec

@Suppress("LocalVariableName")
class SampleTest :
    StringSpec({
        val _test = "test"
        println(_test)
    })

@Suppress("LocalVariableName")
fun test() {
    val _test = "test"
    println(_test)
}

Observed Behavior

This code reports the following lint errors:

Sandbox.kt:8:13 Backing property not allowed when 'private' modifier is missing (cannot be auto-corrected)
Sandbox.kt:8:13 Backing property is only allowed when a matching property or function exists (cannot be auto-corrected)
Sandbox.kt:14:9 Backing property not allowed when 'private' modifier is missing (cannot be auto-corrected)
Sandbox.kt:14:9 Backing property is only allowed when a matching property or function exists (cannot be auto-corrected)

it seems that local variables are treated as properties.

Steps to Reproduce

Your Environment

  • Version of ktlint used: 1.3.1
  • Relevant parts of the .editorconfig settings : no settings
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): Ktlint Gradle plugin 12.1.1
  • Version of Gradle used (if applicable): 8.10
  • Operating System and version: Windows 11
@paul-dingemans
Copy link
Collaborator

it seems that local variables are treated as properties.

From the perspective of the Kotlin Parser, the local variable is indeed a property:
Screenshot 2024-08-28 at 14 37 24

It seems that your expectation is that the @Suppress("LocalVariableName") would also supress the ktlint violation. This is a small fix which makes sense to me.
Screenshot 2024-08-28 at 14 40 00

Until this is implemented, you can suppress the ktlint violation explicitly:

@Suppress("LocalVariableName", "ktlint:standard:backing-property-naming")
fun test() {
    val _test = "test"
    println(_test)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants