Skip to content

[clang] False-positive warning for integer promotion [-Wimplicit-int-conversion] #149288

@Mephistophiles

Description

@Mephistophiles
#include <stdint.h>

_Static_assert(__builtin_types_compatible_p(typeof(UINT8_C(1)), int), "");
_Static_assert(__builtin_types_compatible_p(typeof(UINT8_C(1)), uint8_t) == 0, "");

int foo(uint8_t b)
{
    uint8_t a = ((uint8_t)1) << b;
    return __builtin_types_compatible_p(typeof(((uint8_t)1) << b), int);
}

https://godbolt.org/z/s1f64449b

Code snippet gives a warning:

<source>:8:30: warning: implicit conversion loses integer precision: 'int' to 'uint8_t' (aka 'unsigned char') [-Wimplicit-int-conversion]
    8 |     uint8_t a = ((uint8_t)1) << b;
      |             ~   ~~~~~~~~~~~~~^~~~

However, this behavior is not considered an error in gcc (there is no warning):
https://godbolt.org/z/d1KjrbKdd

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerdiverges-from:gccDoes the clang frontend diverge from gcc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions