You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
classDemo {
val one:Intval two:Stringinit {
val number = get()
if (number !=null) {
one = number.toInt()
two = number
} else {
one =0
two ="0"
}
}
privatefunget(): 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
The text was updated successfully, but these errors were encountered:
@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.
…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
…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
Describe the bug
False positive in class
init
block when assigning to a declaredval
with a branch null check.Expected behavior
Diktat does not rewrite the following code:
Observed behavior
Diktat rewrites the init block to
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
The text was updated successfully, but these errors were encountered: