HairyFotr / linter

Static Analysis Compiler Plugin for Scala
Apache License 2.0
268 stars 34 forks source link

UnnecessaryStringNonEmpty false positive? #58

Open nfsantos opened 7 years ago

nfsantos commented 7 years ago

With version 0.1.17, I get a false positive with the following code:

def test1(s: StringBuilder): Boolean = {
    if (s != null) {
      val trimmed = s.toString.trim
      trimmed.nonEmpty
    } else {
      false
    }
  }
Warning:(123, 15) [UnnecessaryStringNonEmpty] This string will never be empty.
      trimmed.nonEmpty

If I remove the intermediate variable trimmed, the false positive disappears:

  def test2(s: StringBuilder): Boolean = {
    if (s != null) {
      s.toString.trim.nonEmpty
    } else {
      false
    }
  }

It is also not reported if we replace StringBuilder by String as the argument:

  def test3(s: String): Boolean = {
    if (s != null) {
      val trimmed = s.trim
      trimmed.nonEmpty
    } else {
      false
    }
  }