False positives in rule promoting std::to_underlying

Using SonarQube version 2025.3

I have some false positives to report for this rule.

enum E : int
{
    v1 = 0,
    v2 = 1,
};

void func1()
{
    auto e = E{}; 
    // Suggests to use std::underlying as an intermediary when casting to an integer that is different from the underlying type
}

void func2(const int i)
{
    auto e = E{i}; 
    // Suggests to use std::underlying as an intermediary when casting to an integer that is different from the underlying type
}

Furthermore, I would like to request an exception when converting from std::byte

void func3(const std::byte b)
{
    auto e = int{b}; 
    // Suggests to use std::underlying as an intermediary when casting to an integer that is different from the underlying type
}

Thanks for your report!

I’ve created CPP-6639 and CPP-6640 to track these issues.

While discussion this internally we found an issue in the suggested fixit.

1 Like