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.
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.
I think this is a reasonable request. Confirmed.
-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.
*** This bug has been marked as a duplicate of 18624 ***