Bug 36390 - do not mark as used variables used only as lvalue
Summary: do not mark as used variables used only as lvalue
Status: RESOLVED DUPLICATE of bug 18624
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2008-05-30 13:16 UTC by José Manuel Ferrer Ortiz
Modified: 2009-11-24 11:36 UTC (History)
11 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-02-12 14:33:07


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description José Manuel Ferrer Ortiz 2008-05-30 13:16:37 UTC
gcc and g++ (all the versions I've ever used) don't give unused variable warnings in a lot of cases where there are unused variables. Simple sample code:

int test1 ()
{
  int x = 3;
  return (2);
}

int test2 ()
{
  int x;
  x = 3;
  return (2);
}

They only give an unused variable warning in test1 function. They should warn in both cases.
Comment 1 Eugene Zelenko 2008-08-14 23:32:06 UTC
I think unused variable warning should be expanded for all cases where variable used only as lvalue. As result some useless computations could be avoided.

Coverity Prevent warn about such situations with one of its checkers. But Coverity is too huge and slow tool, so GCC could definitely be better for such diagnostics.
Comment 2 Manuel López-Ibáñez 2009-02-12 14:33:07 UTC
I think this is a reasonable request. Confirmed.
Comment 3 Jakub Jelinek 2009-11-24 08:10:09 UTC
-Wunused-variable is checking the TREE_USED flag, which is set quite early during the parsing (in C FE build_external_ref, in C++ FE mark_used called from finish_id_expression).  At that point the parser doesn't track whether it is LHS of an assignment or RHS in a way that could be easily checked in those routines unfortunately (and just setting a some parser flag e.g. around
lhs = c_parser_conditional_expression (parser, after);
in c_parser_expr_no_commas is insufficient, we want that only for the actual LHS, but not other variables referenced in that, say in x[i] = 5; i must be marked as used, etc.).  So implementing this isn't just a one-liner.

On the other side, I agree the warning is very useful in some cases, not so much when var is only used on LHS where RHS has no side-effects, that is just optimized out, but if an otherwise unused variable is initialized using some expensive function call the user might find out that the call is just a waste of time and remove it, even when it is not marked pure/const and thus the compiler can't optimize it itself.
Comment 4 Jakub Jelinek 2009-11-24 11:36:44 UTC

*** This bug has been marked as a duplicate of 18624 ***