False positive cpp:S3743 "Remove this "noexcept" clause" for throw present in "if consteval"

I am using SonarQube server.

It reports an issue "Remove this "noexcept" clause on the function declaration as it can throw." in following code.

However in C++23, “throw” has a different meaning in a "if consteval" code block. In that case throw is never exercice at runtime. Either this statement will stop the compilation process immediately (case of consteval) or it is a no-op (runtime case, so noexcept is valid).

I suggest this check cpp:S3743 to not mark the throw statement as an issue in a if consteval code block.

template <size_t N>
class axstring
{
public:
    constexpr explicit axstring(const char* src) noexcept
    {
        const size_t src_len = std::char_traits<char>::length(src);
        if (src_len > N)
        {
            if consteval
            {
                throw std::out_of_range(message);
            }
            logging::error("Truncation occurred when copying {} bytes to axstring<{}>", src_len, N);
        }
        const size_t len = std::min(src_len, N);
        std::char_traits<char>::copy(data_, src, len);
        data_[len] = 0;
    }
private:
    char data_[N + 1]{};  // NOSONAR
};

Hey @bouteillon, I’ve taken the liberty of moving this to the “Report False-positive / False-negative” section. Could you also please let us know which version of SonarQube are you using?

Hi @bouteillon,

Thanks for reporting this problem and the clear example. I reproduced this problem and created an internal ticket to fix it. Unfortunately, I don’t have an ETA for when it will be tackled.

Thanks again for reporting it!

Best,

Lijun