https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127272
Bug ID: 127272
Summary: Getting a warning about a code path guaranteed not to
be taken
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: eyalroz1 at gmx dot com
Target Milestone: ---
(Following this StackOverflow Q&A: https://stackoverflow.com/q/80001781/1593077
and the code here: https://godbolt.org/z/dY4rj57aY)
Suppose I have a code within a (non-constexpr) if statement, which, if run, may
be problematic due to some feature of the class. Specifically, a `memmove()`
operation, which requires the class to have trivial copy-assignment.
Suppose also, that the condition can be decided at compile-time, negatively, so
that the compiler can know the condition is never met.
Should I be warned about the problematic execution in this case?
Here's a concrete example:
#include <type_traits>
#include <cstring>
struct A {
int x;
A() {}
A(int x_) : x(x_) {}
A(A const& a) = default;
A(A && a) = default;
A& operator=(A const& other) { x = other.x; return *this; }
};
void foo() {
A arr[2];
A a { 123 };
if (std::is_trivially_copy_assignable<A>::value) {
std::memmove(arr, &a, sizeof(A));
}
}
this yields a compiler warning with GCC 16.2, but - not with clang 23.1; see
the GodBolt link above.
I believe GCC should also notice that the condition is necessarily false and
not issue the warning.