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

[Scala] Handle variables in "NewAnonymous"

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 1.9
    • Component/s: Scala
    • Labels:
      None

      Description

      To avoid false positives in S1481 - Unused local variables for variables declared in anonymous classes, ScalaConverter ignored every variable which parent was Template.
      (See https://github.com/SonarSource/slang/pull/250)

      However, this created FN for other rules (eg: S2068) when a variable was not declared in a function but directly in a class or an object

      object Code {
        val password = "azerty" // Was a FN
      }
      

      To correct this FN and don't introduce new FPs for S1481, we made the filtering more precise in ScalaConverter: we ignore every variable which parent is Template and which grand-parent is NewAnonymous.

      But other FNs for S1481 still exist:

      object Code {
        val anonymous = new{val password = "azerty"} // FN
      }
      

      And FPs for S1481 exist too:

      var i = new {{var j = 0}} // Parent of "j" is a BlockTree so a VariableDeclarationTree is created for "j" - S1481 is raised
      

      To correct this, the idea would be to create a VariableDeclarationTree in ScalaConverter for every Defn.Val and Defn.Var and do the filtering in the rule S1481 itself. Then, maybe create a specification for this rule in Scala and ignore variables which have a parent / an ancestor which is NewAnonymous?

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            amelie.renard Amélie Renard
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: