kotlin:S1656 Should recognize setter method of the variable and Livedata

versions used

  • SonarScanner 4.2.0.1873-macosx
  • Sonarqube 8.2.0.32929 Docker

I am not sure if this is categorized as False-positive.

There could be additional operation other than just assigning value in the setter method.
In this case this is not an issue.

For e.g.

 private fun updateViewLayoutParameters() {
        rootView.layoutParams.height = myConfig.panelHeight
        // To call parent.onSetLayoutParams() and requestLayout()
        rootView.layoutParams = rootView.layoutParams

This code triggers View#setLayoutParams

Moreover, in Android usecase, there is Livedata.
If it is a LiveData, self-assigning calls Observer:onChanged(T).
In this case this is not an issue.

fun doSomething() {
    // ...
    myViewModel.name.observe(this, myObserver)
    myViewModel.name.value = viewModel.name.value
}

Hi,

Thanks for reporting this false positive (you’ve chosen a good category!). Still I’m afraid we will not fix it any time soon. The only way to ignore such cases is to have the information about setter/getter of the expression on lhs/rhs. We don’t have that info during analysis and I don’t think we will have it any time soon. Also even if this code is not a bug, it’s pretty confusing for the maintainers, so I hope it is not a frequent pattern in your code, so resolving such issues as “false positive” or “won’t fix” is acceptable.

Sorry for the inconvenience and thanks for the feedback anyway!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.