Kotlin

Kotlin

Made by DeepSource

The result of increment/decrement operation is unused KT-W1036

Bug risk
Major

Postfix expressions increment or decrement a value after it has been evaluated. When the result of the postfix expression is not utilized, it can lead to confusion and potential bugs.

If you assign the output of a postfix operation on a variable back to the variable itself, the result of the post increment/decrement will be ignored. This can mislead readers into thinking that the value will be incremented or decremented, when in fact, the original value remains unchanged.

Bad Practice

fun foo(): Int {
  var i = 0

  i = i--
  print(i) // Here the value of `i` would still be 0, `i--` made no difference

  i = 1 + i++
  print(i) // Here the value of `i` would be 1, `i++` made no difference

  return i++ // Here, the return value will still be 1, `++` makes no difference
}

Recommended

fun foo(): Int {
  var i = 0

  i--
  print(i) // Here the value of `i` would be -1

  i = i + 5
  print(i) // Here the value of `i` would be 4

  i++
  return i // Here, the return value would be 5
}