Message ID | 0f8ec6bad2e2a182126b8c3c6f1ec2b560ec3e6a.camel@redhat.com |
---|---|
State | New |
Headers | show |
Series | [RFA,c/88660] Fix bogus set-but-unused warning | expand |
On Wed, 29 Jan 2020, Jeff Law wrote: > In the last major change in this code was ~5 years ago and twiddled the > handling of the switch expression to call convert_lvalue_to_rvalue. > > The last argument to that function indicates whether or not we should > mark the switch expression as a use of the object. We're currently > passing in "false" so the object doesn't get marked and we get the > bogus warning. > > The obvious fix is to pass in "true", which is what the proposed patch > does. If there's a reason we can't/shouldn't do that in this case we > could call mark_exp_read on the switch expression at some point other > point. > > Bootstrapped and regression tested on x86_64. OK for the trunk? I think the code passes false simply because it was a straight conversion from previous code that (probably erroneously) didn't call mark_exp_read. The patch is OK.
diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index 6164017de02..1e8f2f7108d 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -6607,7 +6607,7 @@ c_parser_switch_statement (c_parser *parser, bool *if_p) && c_token_starts_typename (c_parser_peek_2nd_token (parser))) explicit_cast_p = true; ce = c_parser_expression (parser); - ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false); + ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, true); expr = ce.value; /* ??? expr has no valid location? */ parens.skip_until_found_close (parser); diff --git a/gcc/testsuite/gcc.dg/pr88660.c b/gcc/testsuite/gcc.dg/pr88660.c new file mode 100644 index 00000000000..09a2d3270bd --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr88660.c @@ -0,0 +1,13 @@ +/* { dg-do-compile } */ +/* { dg-options "-O -Wunused-but-set-variable" } */ + +int main(void) +{ + const int i = 0; + switch(i) + { + default: break; + } +} + +