Suspicious shadowing of variable pattern

IntelliJ warns me about something which looks correct to me.
I have a local variable declaration (left) inside a pattern match which has the same name as another variable
which is declared below the partial function in a different scope.
IntelliJ warns me that if I want the value of the variable left I can use the backquote syntax.
BUT I DON’T want that value. Is there a syntax I should use to say it should be a new variable declaration? I see that If I replace left with left:LS (i.e., include a redundant type declaration) the warning goes away.

def mergeSort(unsorted:LS):LS = {
    bisect(unsorted) match {
      case (Nil,Nil) => Nil
      case (Nil,right) => right
      case (left,Nil) => left  // InteliJ warns about the (suspicious) variable left
      case (left,right) => merge2Lists(mergeSort(left),mergeSort(right))
    }
    val len = unsorted.length / 2
    val left = unsorted.drop(len)
    unsorted match {
      case Nil => Nil
      case h::Nil => unsorted
      case _ =>  merge2Lists(mergeSort(left),mergeSort(unsorted.dropRight(left.length)))
    }
  }

I’m no Intellij expert, but I think the only way to make the warning go away is renaming one of the variables. You are shadowing variable left, even though using that variable in that position would result in an error forward reference extends over definition of value left. If left were a lazy val instead you would indeed be shadowing a usable variable.

2 Likes