https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66028
Bug ID: 66028 Summary: false positive, unused loop variable Product: gcc Version: 5.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: ncm at cantrip dot org Target Milestone: --- struct range { int start; int stop; struct iter { int i; bool operator!=(iter other) { return other.i != i; }; iter& operator++() { ++i; return *this; }; int operator*() { return i; } }; iter begin() { return iter{start}; } iter end() { return iter{stop}; } }; int main() { int power = 1; for (int i : range{0,10}) power *= 10; } bug.cc: In function ‘int main()’: bug.cc:15:13: warning: unused variable ‘i’ [-Wunused-variable] for (int i : range{0,10}) Manifestly, i is used to count loop iterations. The warning cannot be suppressed by any decoration of the declaration; the best we can do is void(i), power *= 10; in the loop body. The warning is useful in most cases. The exception might be that, here, the iterator has no reference or pointer members, and the loop body changes external state. [This matches clang bug https://llvm.org/bugs/show_bug.cgi?id=23416)