Re: [PATCH 0/2] kbuild: remove gcc's -Wtype-limits
Linus Torvalds <[email protected]> Fri, 19 Dec 2025 10:19:15 +1200
| Newsgroups | org.kernel.vger.linux-sparse,dev.linux.lists.llvm,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-btrfs,org.kernel.vger.linux-kbuild,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <CAHk-=wjMVfu-aiQ8aNHcgsh6hYwbZCoX1B4ps2scibokO8EZ+A@mail.gmail.com> |
On Fri, 19 Dec 2025 at 10:06, David Laight <[email protected]> wrote: > > True - especially for code like: > if (x < 0 || x > limit) > return ... Exactly. And yes, sometimes the type of 'x' is obvious, and having the range check for zero can be seen as redundant for unsigned types, but even in that "obviously redundant" case the code is *clearer* with both the lower and upper range checked. And apart from being clearer, it's also then safe when somebody does change the type for whatever reason. And lots of types do *not* have obvious signedness. They might be typedefs, or have other much subtler issues. Something as simple as "char" has subtle sign behavior, and when it comes to things like enums the signedness can also be very non-obvious. So having both sides of a range check is *always* a good idea, even if one side _may_ be redundant for some type-range reasons. And there really is absolutely _no_ sane way to get rid of that broken warning except to just disable the warning itself. All other alternatives are actively broken - adding a Pragma only makes the code worse and illegible, and removing the lower bounds check again only makes the code worse. So this is a compiler warning that actively encourages worse code. It needs to *die*. It doesn't fix anything. And the people who point out that it can show bugs - absolutely *ANY* warning can do that. That doesn't make a warning good. Any code can have bugs in it. The sparse warning I outlined (and that Vincent wrote up and tested and made into a proper patch) was actually showing interesting issues in a much better way. And that sparse warning could certainly be improved on too - I think that one too would be better if it noticed "oh, it's a pure range check, so let's not warn even when the code otherwise looks dodgy". But at least it didn't warn for obviously good code like the horrid broken type-range warning does. Linus