Uploaded image for project: 'SonarSlang'
  1. SonarSlang
  2. SONARSLANG-351

[Scala] S3923 & S1871 should not raise an issue on variable shadowed in pattern matching.

    XMLWordPrintable

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.8
    • Component/s: Scala
    • Labels:
      None

      Description

      In Scala, it is possible to shadow a variable in the pattern of a pattern matching statement.

      val cond: Option[Int] = Some(3)
      val value = 1
      
      cond match {
        case Some(value) => value
        case _ => value
      }   
      

      value don't refers to the same variable in the two cases.

      Examples on SonarCloud:
      https://peach.sonarsource.com/project/issues?id=scala%3Ascala&issues=AWZX6nrym1pxnxKPUGVt&open=AWZX6nrym1pxnxKPUGVt

      https://peach.sonarsource.com/project/issues?id=scala%3Ascala&issues=AWZX6oODm1pxnxKPUGX6&open=AWZX6oODm1pxnxKPUGX6

      S1871 is also affected by this problem (see peach).

      To efficiently solve this problem, it requires to be able to compare two identifier with the same name (semantics/type of variable).

       

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              andrei.epure Andrei Epure
              Reporter:
              quentin.jaquier Quentin Jaquier
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: