This patch fixes an issue with -Wunreachable-code diagnostic that happens with the following code sample:
struct s { void *p; }; void fn(struct s *s) { if (1 || !s->p) return; fn(s); }
Currently, clang will emit two warnings (at fn(s) and at !s->p). As well as that, the fixits for the first warning (for fn(s)) have an incorrect source range: it suggests to wrap !s->p in parentheses when it should suggest to wrap 1 instead.
The attached patch ensures that the fixits for the fn(s) warning suggest to wrap the correct expression (1). It also avoids the second warning that was previously shown for !s->p because that warning is caused by the same expression (1).
Thanks for taking a look.
Thanks, that fixed the incorrect fixit. The patch looks good to me, but there are still cases in which the suggestions clang makes are not accurate. Perhaps you can leave a FIXME somewhere so that we don't forget to fix it later?
For example, if we change the condition in the test case to this,
, clang suggests enclosing the entire expression with a parenthesis (caret points to "!"), but the warning will not go away unless the parenthesis is around "0".
The second example is in function unaryOpFixit in warn-unreachable.c. clang suggests enclosing "-1" with a parenthesis, but it still warns after putting the parenthesis around "-1" or "1".