Very small annoyance but I still get a warning `As...
# getting-started
j
Very small annoyance but I still get a warning
Assigned value is never read
on
aNumber += 1
even with the suppressions. I personally consider this warning wrong but I'm sure people could argue about it. Is there anyway to suppress it? Also, why did the `Wrapped into a reference object to be modified when captured in a closure." statement go away?
Copy code
class Foo(val lambda: () -> Unit)

@Suppress("UNUSED_VARIABLE", "UNUSED")
fun test() {
    var aNumber = 0 // "Wrapped into a reference object..." doesn't show anymore

    val newFoo = Foo {
        val test = 1 + aNumber // Do something with current value
        aNumber += 1 // Change value to be used next time Foo.lambda() is called
    }
    // do something with newFoo here to use later like add it to a list, etc.
}
b
but IMO
aNumber
is the primitive int on JVM, so it's not referenced in newFoo initializer
d
It's not a warning but an IDE inspection (you can distinguish them, as it is not reported during the compilation). You can easily add suppression to any warning or inspection by letting the IDE inserting the proper`@Suppress` (
@Suppress("AssignedValueIsNeverRead")
in this case).
Also you can distinguish between warnings and inspection by their names • warnings and errors from compiler are in
SNAKE_CASE
• inspections from the IDE are in
CamelCase
j
Thanks Dmitriy. Part of the issue is that there doesn't seem to be a simple way to find what the correct string is in order to suppress this inspection. How did you find out the proper string arg?
d
You press alt+enter to open the intention popup and then navigate to the suppress option as in the picture above.
plus1 1
r
You're right though that the IDE inspection is incorrect.
Copy code
class Foo(val lambda: () -> Unit)

fun main() {
  var aNumber = 0

  val newFoo = Foo {
    val test = 1 + aNumber // Do something with current value
    aNumber += 1 // Change value to be used next time Foo.lambda() is called
    println(test)
  }
  newFoo.lambda.invoke()
  newFoo.lambda.invoke()
}
The output changes if you accept the IDE's suggestion, which is bad.
d
alt-enter should provide a way to suppress the warning, not just to change the code. Dmitriy included an image for the suggestion.
123 Views