[Bug c/44677] Warn for variables incremented but not used (+=, ++)
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Vincent Lefèvre changed: What|Removed |Added CC||vincent-gcc at vinc17 dot net --- Comment #17 from Vincent Lefèvre --- (In reply to Martin Sebor from comment #10) > $ cat pr95217.c && clang -S -Wall -Wextra --analyze pr95217.c [...] > pr95217.c:8:3: warning: Value stored to 'p' is never read > p += 1; // missing warning > ^~ > pr95217.c:13:3: warning: Value stored to 'p' is never read > p = p + 1; // missing warning > ^ ~ > 2 warnings generated. Clang (15 and above) with -Wunused-but-set-variable now detects the issue on the "p++" and "p += 1" forms (ditto with other combined assignment operators), but not on "p = p + 1". Such forms (p++, etc.) are common, so that detecting an unused variable is very useful. Like Paul did for Emacs (comment 13), I've just fixed two issues in GNU MPFR (one cosmetic about a useless loop variable and one important in the testsuite), found with Clang 16. The references: https://gitlab.inria.fr/mpfr/mpfr/-/commit/4c110cf4773b3c07de2a33acbee591cedb083c80 https://gitlab.inria.fr/mpfr/mpfr/-/commit/b34d867fa41934d12d0d4dbaaa0242d6d3eb32c7 For the second MPFR issue, there was an "err++" for each error found by the function in the testsuite, but err was not tested at the end, so that potential errors were never reported.
[Bug c/44677] Warn for variables incremented but not used (+=, ++)
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Andrew Pinski changed: What|Removed |Added Summary|Warn for variables |Warn for variables |incremented but not used|incremented but not used ||(+=, ++) --- Comment #16 from Andrew Pinski --- "set but not used" (just for future searches)
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Andrew Pinski changed: What|Removed |Added CC||muecker at gwdg dot de --- Comment #15 from Andrew Pinski --- *** Bug 108156 has been marked as a duplicate of this bug. ***
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 David Manuelda changed: What|Removed |Added CC||stormbyte at gmail dot com --- Comment #14 from David Manuelda --- It is worth to notice that this bug propagates to C++ whenever an object uses an int that increments but is never used, like the following example: class Test{ public: Test() { i = 0; i++; } private: int i; }; int main(int, char**) { Test unused; return 0; }
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 eggert at cs dot ucla.edu changed: What|Removed |Added CC||eggert at cs dot ucla.edu --- Comment #13 from eggert at cs dot ucla.edu --- I ran into this issue today on Emacs master, with both += and |=. Clang correctly diagnosed unused local variables, but GCC did not. It would be nice if GCC could do at least as well as Clang does. Here are references to patches I installed into Emacs to fix these issues that Clang found but GCC did not: https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=d9bffa1f3b121085fd8f954eb9446a4a5241c062 https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=68bc1446855c86b96d5bc22f819e63358ab250ac
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Andrew Pinski changed: What|Removed |Added CC||mytbk920423 at gmail dot com --- Comment #12 from Andrew Pinski --- *** Bug 102909 has been marked as a duplicate of this bug. ***
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Joseph S. Myers changed: What|Removed |Added CC||eyalroz1 at gmx dot com --- Comment #11 from Joseph S. Myers --- *** Bug 100184 has been marked as a duplicate of this bug. ***
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Martin Sebor changed: What|Removed |Added Last reconfirmed|2010-06-28 00:34:58 |2020-5-19 --- Comment #10 from Martin Sebor --- See pr95217 for other cases to handle (-Wunused-but-set-parameter). For the test case there, Clang's static analyzer diagnoses two out of the four cases: $ cat pr95217.c && clang -S -Wall -Wextra --analyze pr95217.c void f0 (int *p) { p = 0; // -Wunused-but-set-parameter (expected) } void f1 (int *p) { p += 1; // missing warning } void f2 (int *p) { p = p + 1; // missing warning } void f3 (int *p) { ++p; // missing warning } pr95217.c:8:3: warning: Value stored to 'p' is never read p += 1; // missing warning ^~ pr95217.c:13:3: warning: Value stored to 'p' is never read p = p + 1; // missing warning ^ ~ 2 warnings generated.
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 --- Comment #9 from Jakub Jelinek --- (In reply to Martin Sebor from comment #8) > I would expect handling -Wunused-but-set-variable during Gimplification to > make detecting these sorts of things possible at least in the basic cases. That is way too late.
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Martin Sebor changed: What|Removed |Added Blocks||89180 --- Comment #8 from Martin Sebor --- I would expect handling -Wunused-but-set-variable during Gimplification to make detecting these sorts of things possible at least in the basic cases. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89180 [Bug 89180] [meta-bug] bogus/missing -Wunused warnings
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 --- Comment #7 from Jakub Jelinek --- (In reply to Martin Sebor from comment #6) > I haven't thought through the implementation challenges but defining the > extended -Wunused-but-set-variabl rule that's being suggested here seems > straightforward: every write to an object must be followed by another access > to it (either read or write). If not, it's diagnosed. That is not straightforward at all. The FE doesn't have IL on which it can analyze write accesses being followed by something (no cfg, no SSA form), and in the middle end it is way too late for such a warning (because as soon as you optimize away something, you could optimize away those reads and warning just because the optimizers managed to optimize away some use are not really helpful).
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Martin Sebor changed: What|Removed |Added CC||msebor at gcc dot gnu.org --- Comment #6 from Martin Sebor --- I haven't thought through the implementation challenges but defining the extended -Wunused-but-set-variabl rule that's being suggested here seems straightforward: every write to an object must be followed by another access to it (either read or write). If not, it's diagnosed. This seems analogous to the -Wuninitialized checker/rule that might be stated as: the first read of an object must be preceded by a write to it.
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Jakub Jelinek changed: What|Removed |Added CC||jakub at gcc dot gnu.org --- Comment #5 from Jakub Jelinek --- As soon as consider more complicated testcases than just pre/post increment, this is going to be very hard to define and especially implement. The way the warning works right now is that all rvalue uses of a variable are marked as "read" and the warning then warns about variables that don't have the "read" bit set, but have the "used" bit set (i.e. they aren't fully unused). This is especially because the FEs used to fold expressions aggressively very early (and still do, especially the C FE), so not all expressions (even use in sizeof etc. counts) survive long enough where we actually would know what the lhs is corresponding to rhs. Also, we do not warn if there are any side-effects in between the rvalue use of the var and the store to it. So, if we do want to warn about m = m + 1; we still shouldn't warn for m = foo (m) + 1; or m = (n = m) + 1; etc. Handling the pre/post increment by 1 might be easiest, just remember whether the var is not "read" before processing it and reset the "read" bit if so afterwards, but as soon as you run into more complex expressions that is going to be harder and harder.
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Martin Sebor changed: What|Removed |Added CC||dcb314 at hotmail dot com --- Comment #4 from Martin Sebor --- *** Bug 78964 has been marked as a duplicate of this bug. ***
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Manuel López-Ibáñez changed: What|Removed |Added CC||hadrien-gcc at psydk dot org --- Comment #3 from Manuel López-Ibáñez --- *** Bug 70092 has been marked as a duplicate of this bug. ***
[Bug c/44677] Warn for variables incremented but not used
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677 Manuel López-Ibáñez changed: What|Removed |Added CC||developm...@faf-ltd.com --- Comment #2 from Manuel López-Ibáñez --- *** Bug 69723 has been marked as a duplicate of this bug. ***
[Bug c/44677] Warn for variables incremented but not used
--- Comment #1 from pinskia at gcc dot gnu dot org 2010-06-28 00:34 --- Confirmed. -- pinskia at gcc dot gnu dot org changed: What|Removed |Added Severity|normal |enhancement Status|UNCONFIRMED |NEW Ever Confirmed|0 |1 Keywords||diagnostic Last reconfirmed|-00-00 00:00:00 |2010-06-28 00:34:58 date|| http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44677