Sonarlint C - Magic Number Rule Limitation

Please provide

  • Operating system: Windows 10 Pro
  • SonarLint plugin version: Sonarlint v4.4.2
  • Programming language you’re coding in: C
  • Is connected mode used: No

I’m currently activating the rule and using it however, I find it questionable why “int someVariable = 32” won’t get flagged as a Magic Number, is there an explanation about this?

And is there a possible way that would allow me to get “int someVariable = 32” flagged as a rule violation?

Hey there.

The goal of this rule is to make sure a number is assigned to a well-named variable, not to avoid the use of numbers altogether.

The rule specifically excludes numbers assigned to a variable declaration.

In short:

Magic Number

if (num < 42) 

Not a magic Number

int meaningOfLife = 42
if (num < meaningOfLife) 
1 Like

Thank you, that explains what I wanted to investigate