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

FP S3824 (MapComputeIfAbsentOrPresentCheck) when exception thrown outside map.put(...)

    Details

    • Type: False-Positive
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Rules, Symbolic Execution
    • Labels:
      None

      Description

      Rule S3824 does not propose to replace "Map.get()" by "Map.computeIfAbsent()" in the bellow example, because "getCanonicalFile" throws checked exceptions:

      import java.io.File;
      import java.io.IOException;
      import java.util.Map;
      
        private static void addIfAbsent(Map<String, File> map, String path) throws IOException {
          File canonicalFile = map.get(path); // No issue here
          if (canonicalFile == null) {
            map.put(path, new File(path).getCanonicalFile());
          }
        }
      

      But if the call to "getCanonicalFile" is moved to a previous line, the rule raises a false-positive:

        private static void addIfAbsent(Map<String, File> map, String path) throws IOException {
          File canonicalFile = map.get(path); // false-positive S3824
          if (canonicalFile == null) {
            canonicalFile = new File(path).getCanonicalFile(); // IOException could be thrown here
            map.put(path, canonicalFile);
          }
        }
      

       

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                alban.auzeill Alban Auzeill
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: