https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120449
Bug ID: 120449 Summary: g++ -Wtype-limits without -P doesn't warn on unsigned compare to zero Product: gcc Version: 15.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: zmajeed at sbcglobal dot net Target Milestone: --- The is from libstdc++-v3/include/bits/semaphore_base.h struct __atomic_semaphore { explicit __atomic_semaphore(__detail::__platform_wait_t __count) noexcept: _counter(__count) { __glibcxx_assert(__count >= 0 && __count <= _S_max); } }; On Windows Cygwin, __platform_wait_t is unsigned long from libstdc++-v3/include/bits/atomic_wait.h #ifdef _GLIBCXX_HAVE_LINUX_FUTEX #define _GLIBCXX_HAVE_PLATFORM_WAIT 1 using __platform_wait_t = int; inline constexpr size_t __platform_wait_alignment = 4; #else # if ATOMIC_LONG_LOCK_FREE == 2 using __platform_wait_t = unsigned long; # else using __platform_wait_t = unsigned int; # endif inline constexpr size_t __platform_wait_alignment = __alignof__(__platform_wait_t); #endif Here's the single-line typelimits.cpp testfile #include <semaphore> g++ -c --save-temps -Wtype-limits -std=c++20 -P typelimits.cpp typelimits.ii: In constructor βstd::__atomic_semaphore::__atomic_semaphore(std::__detail::__platform_wait_t)β: typelimits.ii:10140:47: warning: comparison of unsigned expression in β>= 0β is always true [-Wtype-limits] 10140 | do { if (__builtin_expect(!bool(__count >= 0 && __count <= _S_max), false)) std::__glibcxx_assert_fail("/usr/lib/gcc/x86_64-pc-cygwin/15/include/c++/bits/semaphore_base.h", 178, __PRETTY_FUNCTION__, "__count >= 0 && __count <= _S_max"); } while (false); | ~~~~~~~~^~~~ But there's no warning from -Wtype-limits when -P is removed g++ -c --save-temps -Wtype-limits -std=c++20 typelimits.cpp grep -En '__count >= 0|using __platform_wait_t' typelimits.ii 6845: using __platform_wait_t = unsigned long; 14948: do { if (__builtin_expect(!bool(__count >= 0 && __count <= _S_max), false)) std::__glibcxx_assert_fail("/usr/lib/gcc/x86_64-pc-cygwin/15/include/c++/bits/semaphore_base.h", 178, __PRETTY_FUNCTION__, "__count >= 0 && __count <= _S_max"); } while (false); On Linux __platform_wait_t is int so -Wtype-limits doesn't apply to the line