[PATCH] D52137: Added warning for unary minus used with unsigned type

Arthur O'Dwyer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 15 13:09:53 PDT 2018


Quuxplusone added a comment.

In https://reviews.llvm.org/D52137#1236053, @rsmith wrote:

> I think we can and should do better about false positives here. If you move this check to SemaChecking, you can produce the warning in a context where you know what the final type is -- I don't think there's any reason to warn if the final type is signed and no wider than the promoted type of the negation.


I share your general concern about false positives, but in the specific case you mentioned—

  void use(int16_t x)
  uint8_t u8 = 1;
  use(-u8);

—I think it'd be surprising to maybe 50% of average programmers that `x`'s received value wasn't `int16_t(255)` but rather `int16_t(-1)`. The only cases I'd personally consider "false positives" would be cases where `-u32` was being used as a convenient shorthand for `~u32 + 1` a.k.a. `(0u - u32)`... but maybe in those cases it wouldn't be much of a burden for the programmer to just accept a fixit to `(0u - u32)` and be done with it.


https://reviews.llvm.org/D52137





More information about the cfe-commits mailing list