Unused local class object variable not being flagged

Hi,

Currently using SonarQube Developer Edition 8.9.9 (build 56886) on C++ code.

I’ve noticed that SonarQube does not flag unused local class object variables. It seems like it should according to rule https://rules.sonarsource.com/cpp/RSPEC-1481. The rule only flags unused local primitive data type variables. For example, the variable x below will be flagged by the rule but someObject will not. As a test, I made the class MyClass empty (i.e. no constructors, destructor, member variables, etc.) and only then it would be flagged. Am I missing something or is this a bug?

void someMethod()
{
  const MyClass someObject;
  const int x = 0;
}

Thanks

Hi @menglim,

Yes, this is the intended behavior. If you have an untrivial destructor, it is no longer an unused variable.
You might be creating this object for its destructor to be called. For example, in the case of an RAII object.

Thanks,

I see, that makes sense. Thank you for the explanation.

Thanks

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