On Fri, Mar 18, 2022 at 11:41 AM Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote: > > On Fri, Mar 18, 2022 at 11:29 AM Nick Desaulniers > <ndesaulniers@xxxxxxxxxx> wrote: > > > > Should we add a note diagnostic to clang suggesting the explicit cast > > as one method of silencing the warning? > > On the compiler side, I would love to see warnings about the ambiguity > of the sign of 'char' in the general case. > > That said, I tried to add that to 'sparse' long long ago, and couldn't > make it work sanely. All the approaches I tried all get _way_ too many > false positives. > > I tried to come up with some way of figuring out "this code acts > differently depending on whether 'char' is signed or not" and warning > about it, and never could. > > And I suspect the same is true even for the much moire limited case of > only format warnings. > > Because it's a *bad* idea to use '%d' (or almost any other format > specifier) together with a 'char' argument, but only if you don't know > the range of the char argument. > > But the other side of the argument is that quite often, people *do* > know the range of the 'char' argument. If your 'char' type thing comes > from some array or string that you control, and you used 'char' simply > because you know you have small values (typical example: use it for an > array of booleans etc), then it would be very annoying if the compiler > warned you about using '%d'. > > There is no reason to use '%hhd' when you know your data range is [0,1]. > > So honestly, I don't think you can come up with a sane warning that > doesn't cause *way* too many false positives and just annoys people. > > I'd love to be proven wrong. In fact, I'd _really_ love to be proven > wrong for that generic case. The "sometimes 'char' is signed, > sometimes it is unsigned, and it depends on the architecture and the > compiler flags" can be a real problem. > My first thought is that this might be better suited for a static analyzer, like clang-tidy, that can do deeper analysis on code. It might still be difficult to weed out all of the false positives, but could be useful for specific offenders. -bw