Search code examples
androidsonarlintsonarlint-intellij

SonarLint: Suppress specific line warning in Android Studio


There is a special case in our code where we need to call setText with getText of the same TextView. SonarLint warn this with kotlin:S1656, to suppress it we use @SuppressWarnings annotation but it does not work. It started showing an error to the IDE and the warning from SonarLint still there. //NOSONAR is not working as well

enter image description here

enter image description here

No action is available as well for suppressing it. enter image description here


Solution

  • Can you put the SuppressWarnings annotation before your method?

    // Sonarlint plugin version 6.8.0.50884
    
    @SuppressWarnings("kotlin:S1656")
    fun yourMethod() {
        // ...
      withContext(coroutineDispatcherMain) {
            // Set the text again to fix images overlapping text
            htmlTextView.text = htmlTextView.text
      }
    
    }
    

    I also wonder that if htmlTextView.invalidate() would work for you.

    UPDATE:

    SuppressWarnings annotation is also available for LOCAL_VARIABLE but I think the problem is more complicated in kotlin code.

    If you examine the byte code of these lines below, you can see that some.num = ... is some.setNum(...) in byte code.

    Sonar plugin's code analyser may not handle this kind of kotlin specific codes.

    class Sonar {
    
      private val some = Something(0)
      private var member = 0
    
      @Deprecated("...")
      fun myNumber(): Int = 1
    
      fun doThat() {
        // local variable working
        @SuppressWarnings("kotlin:S1874") // Code annotated as deprecated should not be used
        val localNum = myNumber()
    
        // not working
        @SuppressWarnings("kotlin:S1874")
        some.num = myNumber()
    
        // not working
        @SuppressWarnings("kotlin:S1874")
        member = myNumber()
    
        // ...
      }
    
    }