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

S1142: Fix false-positives by changing the way returns are counted

    Details

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

      Description

      Currently, we count each return, so this sample code contains 5 returns:

          switch (option) {
          case rtUnknown:
          case rtSingleThreaded:
          case rtSingleThreadedDebug:
              break;
          case rtMultiThreaded:
              return "MultiThreaded";
          case rtMultiThreadedDLL:
              return "MultiThreadedDLL";
          case rtMultiThreadedDebug:
              return "MultiThreadedDebug";
          case rtMultiThreadedDebugDLL:
              return "MultiThreadedDebugDLL";
          }
          return QString();
      

      All the return at the same level in the case do not really make the code more complex to read or understand... So maybe we should change the way we count return so that whatever the number of return at the top level of a switch, we only count them as 1.

      With such a change, the previous function would be counted as having 2 returns,

      On the other hand, cases like the following also:

      inline uint64_t hash_short(const char *s, size_t length, uint64_t seed) {
        if (length >= 4 && length <= 8)
          return hash_4to8_bytes(s, length, seed);
        if (length > 8 && length <= 16)
          return hash_9to16_bytes(s, length, seed);
        if (length > 16 && length <= 32)
          return hash_17to32_bytes(s, length, seed);
        if (length > 32)
          return hash_33to64_bytes(s, length, seed);
        if (length != 0)
          return hash_1to3_bytes(s, length, seed);
        return k2 ^ seed;
      }

      should count as as many returns that are written. It is consistent with the way we compute cognitive complexity (+1 for a switch and all its cases, +1 for each if statement).

      These changes need changing the 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: