https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91335
Bug ID: 91335 Summary: False positive "unused variable" warning with variable initialized in 'if' condition Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: zeratul976 at hotmail dot com Target Milestone: --- In the following code: int* foo(); int main() { if (int* f = foo()) { return 1; } return 0; } GCC (tested version 9.1.0) issues the following warning with -Wall: test.cpp: In function ‘int main()’: test.cpp:4:12: warning: unused variable ‘f’ [-Wunused-variable] 4 | if (int* f = foo()) { | ^ I consider this a false positive because testing the variable's value in the if-condition constitutes a use. The language does not syntactically permit omitting the variable name here: int main() { if (int* = foo()) { // error: expected unqualified-id before '=' token return 1; } return 0; } As a result, the only way to fix the warning is to lift the variable into the outer scope, which can be undesirable from a code style point of view: int* foo(); int main() { int* f = foo(); if (f) { return 1; } // variable 'f' still in scope here, when I don't want it to be return 0; }