Uploaded image for project: 'Product Roadmaps'
  1. Product Roadmaps
  2. MMF-1511

Reduce False-Negatives rate in Slang-based analyzers

    Details

    • Type: MMF
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Fix Version/s: None

      Description

      WHY

      When we were converting original ASTs to Slang AST for 3 languages (Kotlin, Ruby, Scala), we did it in isolation: everything not easily matching Slang AST was converted to NativeTree. Because existing rules ignore NativeTree, there are false negatives, i.e. for Scala for rule RSPEC-117:

      def f(bad_parameter_name: String = "Default") = ... // FN, because we don't match params with default value
      

      WHAT

      This MMF sums up places where NativeTree is created and it might result in false negative issues. The goal is to perform some minor improvements to Slang AST and/or language-specific converters to be able to improve existing rules.

      NativeTree is created in Scala for..

      1. Function with many parameter clauses
      Affected rules: RSPEC-3776, RSPEC-138, RSPEC-107
      scala.meta.Defn$Def$DefnDefImpl -> FunctionDeclarationTreeImpl 90%, NativeTreeImpl 10%

      def add(i1: Int)(i2: Int): Int = {
        i1 + i2
      }
      

      2. Match statement with at least one conditional case, as well as all its cases
      Affected rules: RSPEC-131, RSPEC-134, RSPEC-1479, but be cautious RSPEC-1862, RSPEC-3923, RSPEC-1871
      scala.meta.Term$Match$TermMatchImpl -> MatchTreeImpl 70%, BlockTreeImpl 21%, NativeTreeImpl 9%

      "something" match {
         case "a" if(variable) => println("a")
         case "a" => println("a")
         case "b" if(variable) => println("b")
      }
      

      3. Variable and constant definition if there is some templating
      Affected rules: RSPEC-117, RSPEC-1481
      scala.meta.Defn$Val$DefnValImpl -> VariableDeclarationTreeImpl 64%, NativeTreeImpl 36%
      scala.meta.Defn$Var$DefnVarImpl -> NativeTreeImpl 65%, VariableDeclarationTreeImpl 35%

      val  (x,y) = (1, 2) // x = 1, y = 2
      var z :: zs = List(1, 2, 3) // z = 1, zs = List(2,3)
      

      4. Function parameters with default value or modifier.
      Affected rules: RSPEC-117
      scala.meta.Term$Param$TermParamImpl -> NativeTreeImpl 61%, ParameterTreeImpl 39%

      def f1(i: String = "Default") = ...
      def f2(implicit j: Int) = ...
      

      5. Constructors
      Affected rules: RSPEC-1481, RSPEC-117, RSPEC-3776, RSPEC-138, RSPEC-107. Be cautious some rules using FunctionDeclarationTree (e.g. RSPEC-100) may not apply to constructors.
      scala.meta.Ctor -> NativeTreeImpl 100%
      As most rules applying to functions can also be applied to constructors, all constructors will have FN.

      public class Greeter{
          public readonly string message;
          public Greeter(string msg) { // message param never used
              this.message = "hello";
          }
      }
      

      NativeTree is created in Kotlin for..

      1. Function parameters with default value.
      Affected rules: RSPEC-117
      KtParameter -> ParameterTreeImpl 91%, NativeTreeImpl 9%

      fun foo(i: String = "Default") ...
      

      2. when statement without expression RSPEC-131, RSPEC-134, RSPEC-1479, RSPEC-1862, RSPEC-3923, RSPEC-1871
      KtWhenExpression -> MatchTreeImpl 75%, NativeTreeImpl 25%

      when {
         cond1 -> println("1")
         cond2 -> println("2")
         else -> println("3")
      }
      

      3. Constructors
      Affected rules: RSPEC-1481, RSPEC-117, RSPEC-3776, RSPEC-138, RSPEC-107. Be cautious some rules using FunctionDeclarationTree (e.g. RSPEC-100) may not apply to constructors.
      KtNamedFunction -> FunctionDeclarationTreeImpl 88%, NativeTreeImpl 12%
      As most rules applying to functions can also be applied to constructors, all constructors will have FN.

      class A(val message: String) { } // message param never used
      

      NativeTree is created in Ruby for..

      1. Multiple variable declaration
      Affected rules: RSPEC-1481, RSPEC-117
      masgn -> AssignmentExpressionTreeImpl 100% (they are all considered assignments, when some should be considered declarations)

      def method()
        a, b = 0, 1
        puts a
      end
      

      2. unless statements and ternary operators
      Affected rules: RSPEC-1145, RSPEC-134, RSPEC-1125, RSPEC-3923
      unless -> IfTreeImpl 77%, NativeTreeImpl 23%
      The unless statement, which behaves as 'if not', is mapped to native trees. This impacts rules regarding conditions, nested depth and complexity.

      unless false // Condition always false
        doSomething()
      end
      

      NativeTree is created in Apex for..

      1. Constructors
      Affected rules: RSPEC-1481, RSPEC-117, RSPEC-3776, RSPEC-138, RSPEC-107. Be cautious some rules using FunctionDeclarationTree (e.g. RSPEC-100) may not apply to constructors.
      MethodDecl -> FunctionDeclarationTreeImpl 92%, NativeTreeImpl 8%
      As most rules applying to functions can also be applied to constructors, all constructors will have FN.

      class A() {
        A(String message) { } // message param never used
      }
      

      HOW

      In order to reduce the rate of FN, we should reduce amount of NativeTree in converted Slang AST for the constructions mentioned in WHAT section. In order to do that we expect to extend/improve Slang AST API in order to support constructions above and not produce new FP after their full conversion.
      E.g. both in Kotlin and Scala parameters with default values are converted to NativeTree. In order to have less FN for rule RSPEC-117, we might add default value in API of ParameterTree (see code sample in WHY section).

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                elena.vilchik Elena Vilchik
                Reporter:
                elena.vilchik Elena Vilchik
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: