Skip to content

-Wbitfield-constant-conversion does not warn on 1-bit signed integer bitfield edge case #53253

Closed
@Scaless

Description

@Scaless

In C++, signed integers are represented in two's complement. This also applies to signed bitfields.
A signed bitfield composed of one bit can therefore store a value in the range -1 to 0.
Assigning a value of 1 to such a bitfield should produce a warning since it is out of range of representable values.

MSVC and GCC generate the correct warnings for this situation, clang does not.

clang 13.0.0
-std=c++20 -Wall -Wextra -Wpedantic -Wconversion -Wbitfield-constant-conversion

struct test
{
    signed int v : 1;
};

int main()
{
    test t;
    // MSVC:  warning C4463: overflow; assigning 1 to bit-field that can only hold values from -1 to 0
    // GCC:   overflow in conversion from 'int' to 'signed char:1' changes value from '1' to '-1' [-Woverflow]
    // clang: no warning produced
    t.v = 1;
    return t.v; // Returns -1
}

Godbolt

Metadata

Metadata

Assignees

No one assigned

    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