False Positive for variable can be const in defaulted comparison operator

SonarQube version 9.2.1

class MyClass
    friend bool operator==(MyClass lhs, MyClass rhs) = default;

When this operator was analyzed it emitted Unmodified variable "lhs" of type "class MyClass" should be const-qualified. And ditto for rhs.

Performing that change doesn’t work with clang, see godbolt. Judging by cppreference it seems like the arguments to defaulted comparison operators can only be taken by const ref or non-const value.

This also happens when the parameters are unnamed.

Hello @torgeir.skogen,
The issue looks like a false-positive but I wasn’t able to reproduce the issue from your example.
can you share the reproducer so I can give it a deeper look?
To generate the reproducer file:

  • Add the reproducer option to the scanner configuration:
    sonar.cfamily.reproducer= "Full path to the .cpp file that has or include the file that has the false-positive"
  • Re-run the scanner to generate a file named sonar-cfamily.reproducer in the project folder.
  • Please share this file. if you think this file contains private information you can send it privately.


I was able to trigger it in SonarLint with this sample:

struct Dummy
    friend bool operator==(Dummy, Dummy) = default;
    int a = 0;
    int b = 0;

constexpr Dummy first{2, 1};
constexpr Dummy second{2, 2};
static_assert(!(first == second));

I can reproduce it now. Ticket created, we will fix it soon.
Thanks for your continuous valuable report!

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