False-positive cpp:S109 with class-member assignments or std::chrono literals

  • What language is this for? C++
  • Which rule? multiple cpp:S109
  • Why do you believe it’s a false-positive/false-negative? Because the member is a “well-named variable”, and chrono literals often speak for themselves IMHO.
  • SonarLint in VS 2022,, connected to SonarQube
        struct A {
            int member;

        A a;
        a.member = 20;
#include <chrono>
using namespace std::chrono_literals;
void fun(std::chrono::seconds waitBefore) {}

Hi @bers,

Thank you for raching out. In this rule when we refer to the additional context for the constant, we usually to the use of the value. The same constant, even when represented using a strong type, as 24h which clearly describe the unit of time, may still represent different things in different context, for example:

a) it may be time between some cyclic updates, that happens to be a day currently
b) number of hours in a day

In the situation when the we are faced with updating the value corresponding in to case a, it remains unclear which of the instance of 24h needs to be updated. Introducing named constant that is used in all situation when such time between updates is needed, makes the meaning of the code clear.

This is why we belive that the rule should still raise an issues in the situation you have mentioned, and do not consider them as false positives.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.