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

S3415: better support of constant used as actual value

    Details

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

      Description

      In addition to hard-coded value (as described in the RSPEC), the current implementation also reports when the actual value is constant. It makes sense when you extracted a duplicated string for example:

      static final String CONSTANT = "extractedString";
      assertEquals(actualObject(), CONSTANT); // Noncompliant, swap...
      

      or when you use common constant:

      assertEquals(actualObject(), Boolean.FALSE); // Noncompliant, true positive
      assertEquals(actualObject(), MyCustomConstantUtils.MyConstant); // Noncompliant, true positive
      

      However, it leads to arguable issues when the constant is used in another context:

      class MyClass {
       public static final String MY_CLASS_CONSTANT = "cst";
      }
      assertEquals("cst", MyClass.MY_CLASS_CONSTANT); // Noncompliant, FP
      
      static final MyClass MY_CLASS = new MyClass();
      void test() {
        assertEquals("cst", MY_CLASS.MY_CLASS_CONSTANT); // Noncompliant, really?
        assertEquals("field", MY_CLASS.field); // Compliant, actual is not a constant
      }
      
      static final MyClass MY_CLASS = new MyClass();
      void test() {
        assertEquals(MY_CLASS, MY_CLASS); // Noncompliant, we should not report an issue
      }
      

       

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                quentin.jaquier Quentin Jaquier
                Reporter:
                quentin.jaquier Quentin Jaquier
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: