Uploaded image for project: 'SonarCFamily'
  1. SonarCFamily
  2. CPP-2437

S1301: Fix false-positives by reducing the number of catch to 2

    Details

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

      Description

      Even with 2 cases (not default, real cases), a switch can already be more powerful than an if:

      void f(int i)
      {
        switch(i) {
          case 42:
            // Do something
          case 0:
            // Do something else
        }
        // Do something for the other values
      }
      

      We would need 2 ifs to write the equivalent without a switch.

      If one of the case is collapsed with default, it should not count:

      void f(int i)
      {
        switch(i) {
          case 42:
            // Do something
          case 0: // Violation, we effectively only have one case...
          default:
            // Do something else
        }
      }
      

      This change will require updating the rule title and rspec.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                geoffray.adde Geoffray Adde
                Reporter:
                loic.joly Loïc Joly
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: