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

FP in S1943: Do not report an issue on any usage of Java 11 FileWriter and FileReader

    Details

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

      Description

      When initially created, S1943 was reporting on any usage of FileWriter and FileReader. It made sense at the time since there was no way to create these classes with a defined charset.

      Since Java 11, it is not true anymore, the new API for FileWriter and FileReader contains constructors including a charset.

      In SONARJAVA-3057, we made sure to report only the constructors without charset.
      However, we still report FP due to this new API:

      java.io.Writer writer2 = fileWriter(); // FP, should be compliant
      
      FileWriter fileWriter() {
        return new FileWriter("fileName", StandardCharsets.UTF_8, true); // Compliant, True Negative
      }
      
      FileReader reader = null; // FP, we have no idea if this reader will be initialized with a charset or not.
      
      FileWriter statusFileWriter = new FileWriter(new File("c:\temp", "status.txt"), // FP raised on the identifier "statusFileWriter"
            StandardCharsets.UTF_8);
      

      We should not report an issue in these cases (whatever the actual java version), and only report when a noncompliant constructor is found.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                alban.auzeill Alban Auzeill
                Reporter:
                quentin.jaquier Quentin Jaquier
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: