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

S109: fix false-positives by avoiding numbers comming from template instantiation

    Details

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

      Description

      Add exception when the number is instantiated. Example:

      template <int N>
      class CheckMagicNumberFalsePositive {
        public:
        static int staticMethod() {
          return N;
          //             ~
          // Assign this magic number N to a well-named variable, and use the variable instead.
        }
      
        int method();
      };
      
      template <int N>
      int CheckMagicNumberFalsePositive<N>::method()
      //                                ~
      // Assign this magic number N to a well-named variable, and use the variable instead.
      
      {
        return N;
        //         ~
        // Assign this magic number N to a well-named variable, and use the variable instead.
      }
      
      template <int N>
      int falsePositiveFunction() {
        return N;
        //         ~
        // Assign this magic number N to a well-named variable, and use the variable instead.
      }
      
      static auto value_from_function = falsePositiveFunction<2>();
      static auto value_from_static_method = CheckMagicNumberFalsePositive<3>::staticMethod();
      static auto object = CheckMagicNumberFalsePositive<4>();
      static auto value_from_object_method = object.method();
      
      

      Also, False-negative, when variables are assigned to a magic number, should be fixed:

      int size=2; // ok
      size=3; // false-negative to be fixed
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                abbas.sabra Abbas Sabra
                Reporter:
                abbas.sabra Abbas Sabra
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: