Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3759

Rule S6212: Local-Variable Type Inference (var) should be used

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.15
    • Component/s: Rules
    • Labels:

      Description

      The idea is to start simple: report only when the type on the LHS is exactly the same as the one on the RHS.

      MyClass myClass = new MyClass(); // Noncompliant
      var myClass = new MyClass(); // Compliant
      List<String> list = new ArrayList<String>(); // Compliant, not the same type.
      var list = new ArrayList<String>(); // Compliant
      

      Be careful to only report an issue if the code is supporting Java 10 and if the variable is local.

      Eventually, in a second step, we could improve the rule when the type is not the same (subtype).

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              margarita.nedzelska Margarita Nedzelska
              Reporter:
              quentin.jaquier Quentin Jaquier
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: