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

false positive when assigning to a declared val with a branch null check #1844

Closed
Skeletonxf opened this issue Dec 1, 2023 · 2 comments · Fixed by #1851
Closed

false positive when assigning to a declared val with a branch null check #1844

Skeletonxf opened this issue Dec 1, 2023 · 2 comments · Fixed by #1851
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Skeletonxf
Copy link

Describe the bug

False positive in class init block when assigning to a declared val with a branch null check.

Expected behavior

Diktat does not rewrite the following code:

class Demo {
  val one: Int
  val two: String

  init {
    val number = get()
    if (number != null) {
      one = number.toInt()
      two = number
    } else {
      one = 0
      two = "0"
    }
  }

  private fun get(): String? = if (Math.random() > 0.5) { "1" } else { null }
}

Observed behavior

Diktat rewrites the init block to

val number = get()
number?.let {
  one = number.toInt()
  two = number
} ?: run {
  one = 0
  two = "0"
}

which does not compile as the kotlin compiler complains that "val cannot be reassigned" (even though we can see that it wouldn't be in this case)

Steps to Reproduce

Assign to a declared val in both branches of a null check

Environment information

  • diktat version: 1.2.5
  • build tool (maven/gradle): gradle
  • how is diktat run (CLI, plugin, etc.): spotless (spotlessApply task)
  • kotlin version: 1.9.20
  • operating system: macOS
  • link to a project (if your project is public): NA
@Skeletonxf Skeletonxf added the bug Something isn't working label Dec 1, 2023
@orchestr7
Copy link
Member

@Skeletonxf thank you for your report! Your particular case is definitely a bug, because we did not expect that run will be called as extension function inside of init block, but actually it does not work even with kotlin.run function. And that is a bug in a design of Kotlin, as I think.

We will workaround it, by removing this false positive in particular scopes. But I have anyway reported it to Kotlin:
https://youtrack.jetbrains.com/issue/KT-64174/kotlin.run-inconsistent-behaviour-inside-of-scope-structures-like-init

DrAlexD added a commit that referenced this issue Dec 8, 2023
…nches

### What's done:
- Fixed bug related to case when `if-else` block inside `init` or 'run', 'with', 'apply' blocks and there is more than one statement inside 'else' block. This case leads to kotlin compilation error after adding 'run' block instead of 'else' block. Read https://youtrack.jetbrains.com/issue/KT-64174 for more information.
- Added new warning tests.

Closes #1844
@nulls nulls added this to the 2.0.0 milestone Dec 11, 2023
DrAlexD added a commit that referenced this issue Dec 11, 2023
…nches (#1851)

### What's done:
- Fixed bug related to case when `if-else` block inside `init` or `run`, `with`, `apply` blocks and there is more than one statement inside `else` block. This case leads to kotlin compilation error after adding `run` block instead of `else` block. Read https://youtrack.jetbrains.com/issue/KT-64174 for more information.
- Added new warning tests.

Closes #1844
@orchestr7
Copy link
Member

@Skeletonxf indeed, Kotlin team has reviewed it. And it appeared that the bug in Kotlin we are talking about was reported 5 years ago: https://youtrack.jetbrains.com/issue/KT-28511

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants