0

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

Bitwise DEVS
  • 2,858
  • 4
  • 24
  • 67

2 Answers2

3

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()

    // ...
  }

}
ocos
  • 1,901
  • 1
  • 10
  • 12
  • This works, thanks! By the way can we only suppress it by putting the annotation in the method or class and not by line? – Bitwise DEVS Jul 23 '22 at 15:01
  • One more question, how do you write it with multiple rules? I tried `@SuppressWarnings({"kotlin:S6310","kotlin:S1656"})` but it is mark as an error. – Bitwise DEVS Jul 24 '22 at 04:39
  • 1
    `@SuppressWarnings("kotlin:S6310", "kotlin:S1656")` – ocos Jul 24 '22 at 11:45
0

I found the docs for the issue to confirm its the right number: https://rules.sonarsource.com/kotlin/RSPEC-1656

Also read the source at https://github.com/SonarSource/sonarlint-intellij but not much help.

It seems like "// NOSONAR" is what you want (I know you said you tried it, but did you have the space? And have it at the end of the line?)

htmlTextView.text = htmlTextView.text // NOSONAR

Just a guess here, but you could also try:

@SuppressWarnings("RSPEC:S1656")

lastly to check suppress is working you can ignore everything:

@SuppressWarnings("all")
htmlTextView.text = htmlTextView.text

Big discussion here: Turning Sonar off for certain code

This article also gives you options to choose from: https://docs.codescan.io/hc/en-us/articles/360012109711-Ignoring-violations

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • None of the above work tho I did not try the last one since I just want to suppress that specific rule just for this line. – Bitwise DEVS Jul 23 '22 at 15:04
  • You should try the last one to help you understand the problem more. You could also check your sonar config to see if suppression is enabled. Also check the import you are using for suppresswarnings to check its the correct annotation. – Blundell Jul 23 '22 at 20:17