https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123127

            Bug ID: 123127
           Summary: -Wsign-conversion should consider control-flow sentive
                    range and only warn if problematic numbers are
                    possible
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redbeard0531 at gmail dot com
  Target Milestone: ---

The following code is perfectly fine, but currently generates a bogus warning:


int f(unsigned);
int test(int i) {
    if (i < 0)
        return 0;
    return f(i);
}

<source>:7:14: warning: conversion to 'unsigned int' from 'int' may change the
sign of the result [-Wsign-conversion]
    7 |     return f(i);
      |              ^


`i` can never be negative at that point (and I'm pretty sure other warnings
know that), so there shouldn't be a warning when it gets converted to unsigned.

https://godbolt.org/z/6KzM1K36f

I have also filed a matching bug with clang:
https://github.com/llvm/llvm-project/issues/172279

Reply via email to