Skip to content

Clang emits -Winteger-overflow warnings from discarded if constexpr #59448

Open
@dangelog

Description

Testcase: https://gcc.godbolt.org/z/ds4M6zah4

#include <limits>

int main() {
    using Bounds = std::numeric_limits<long>;
    if constexpr (sizeof(long) < sizeof(long long)) {
        const long long longMaxPlusOne = static_cast<long long>(Bounds::max()) + 1;
        const long long longMinMinusOne = static_cast<long long>(Bounds::min()) - 1;
    }
}

On Unix x86-64, long has the same size of long long, so the if constexpr branch is discarded as the condition is false. However clang warns:

<source>:6:80: warning: overflow in expression; result is -9223372036854775808 with type 'long long' [-Winteger-overflow]
        const long long longMaxPlusOne = static_cast<long long>(Bounds::max()) + 1;
                                                                               ^
<source>:7:81: warning: overflow in expression; result is 9223372036854775807 with type 'long long' [-Winteger-overflow]
        const long long longMinMinusOne = static_cast<long long>(Bounds::min()) - 1;

It's a bit interesting that other kind of warnings are instead suppressed. For instance, if one adds a 123 / 0 outside of the if constexpr, Clang warns about the division by zero, but it doesn't if the division happens inside the if.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions