| > Deleting code that isn’t dead, it just doesn’t have a universally defined behavior, is the issue. Can I delete "if (x & 3 == 16)" without a warning? There is no 'x' which makes that expression true, so I can safely fold it to false without a warning? Can I delete "if (x + 1 < x)" without a warning? There is no signed 'x' which makes that expression true, so I can safely fold it to false without a warning? How about this: int x = 7;
call_function_outside_this_file();
if (x != 7) { /* dead */ }
Does deleting the code require a warning or no?Or this: void f(int *x, float *y) {
*x = 1;
*y = 2;
if (*x != 1) { /* dead */ }
A float cannot alias an int, so '*x' can not have changed. Warning or no?The problem with UB is that you can use it to set up impossible situations, like create an 'x' where x & 3 == 16 is true or a variable whose address was never taken being modified through a pointer, and so on. If you account for UB then "code that doesn't have a universally defined behaviour" becomes all code. Ideally I think the first two examples should have warnings, though not because we delete the code, and the last two shouldn't? The warning should be because it's a tautology so the human likely didn't mean to write that (for instance if the human wrote it indirectly through macros, then we shouldn't warn on it). |
I’m obviously just a holdover from the 90s, but it does seem we’ve leaned too far into hidden assumptions that the compiler thinks I share, rather than doing what the code says, or a simplification of what the code says.