public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/95057] New: missing -Wunused-but-set-variable warning on multiple assignments, not all of them used
@ 2020-05-11 12:50 vincent-gcc at vinc17 dot net
  2020-05-11 12:53 ` [Bug c/95057] " jakub at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2020-05-11 12:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95057

            Bug ID: 95057
           Summary: missing -Wunused-but-set-variable warning on multiple
                    assignments, not all of them used
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent-gcc at vinc17 dot net
  Target Milestone: ---

With -Wunused-but-set-variable, GCC does not warn in the following cases:

int f (int i)
{
  int j;
  j = i + 1;  /* unused */
  j = i + 2;
  return j;
}

int g (int i)
{
  int j, k;
  j = i + 1;
  k = j + 1;
  j = i + 2;  /* unused */
  return k;
}

Note: it is possible that PR44677 may be regarded as a particular case of this
bug.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/95057] missing -Wunused-but-set-variable warning on multiple assignments, not all of them used
  2020-05-11 12:50 [Bug c/95057] New: missing -Wunused-but-set-variable warning on multiple assignments, not all of them used vincent-gcc at vinc17 dot net
@ 2020-05-11 12:53 ` jakub at gcc dot gnu.org
  2020-05-11 13:05 ` vincent-gcc at vinc17 dot net
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-05-11 12:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95057

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
That is something this warning can't warn about.
The warning is a simple FE warning that uses two bits, one for whether certain
decl was used and one whether it was read.  The warning then diagnoses those
that have the former bit set and not the latter (of course, there are some
exceptions etc.).

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/95057] missing -Wunused-but-set-variable warning on multiple assignments, not all of them used
  2020-05-11 12:50 [Bug c/95057] New: missing -Wunused-but-set-variable warning on multiple assignments, not all of them used vincent-gcc at vinc17 dot net
  2020-05-11 12:53 ` [Bug c/95057] " jakub at gcc dot gnu.org
@ 2020-05-11 13:05 ` vincent-gcc at vinc17 dot net
  2020-05-11 13:09 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2020-05-11 13:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95057

--- Comment #2 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> ---
(In reply to Jakub Jelinek from comment #1)
> That is something this warning can't warn about.
> The warning is a simple FE warning that uses two bits, one for whether
> certain decl was used and one whether it was read.  The warning then
> diagnoses those that have the former bit set and not the latter (of course,
> there are some exceptions etc.).

But when the variable is assigned, the "read" bit could be reset.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/95057] missing -Wunused-but-set-variable warning on multiple assignments, not all of them used
  2020-05-11 12:50 [Bug c/95057] New: missing -Wunused-but-set-variable warning on multiple assignments, not all of them used vincent-gcc at vinc17 dot net
  2020-05-11 12:53 ` [Bug c/95057] " jakub at gcc dot gnu.org
  2020-05-11 13:05 ` vincent-gcc at vinc17 dot net
@ 2020-05-11 13:09 ` jakub at gcc dot gnu.org
  2020-05-11 13:39 ` vincent-gcc at vinc17 dot net
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-05-11 13:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95057

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
It can't.  The warning isn't flow sensitive, doesn't know about assignments,
conditionals etc.
What you are thinking of is something that can be only done much later, but at
that point one risks that there is compiler-introduced dead code that wasn't
really the fault of the user.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/95057] missing -Wunused-but-set-variable warning on multiple assignments, not all of them used
  2020-05-11 12:50 [Bug c/95057] New: missing -Wunused-but-set-variable warning on multiple assignments, not all of them used vincent-gcc at vinc17 dot net
                   ` (2 preceding siblings ...)
  2020-05-11 13:09 ` jakub at gcc dot gnu.org
@ 2020-05-11 13:39 ` vincent-gcc at vinc17 dot net
  2023-07-18 22:34 ` vincent-gcc at vinc17 dot net
  2023-07-18 22:46 ` vincent-gcc at vinc17 dot net
  5 siblings, 0 replies; 7+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2020-05-11 13:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95057

--- Comment #4 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> ---
OK, so the work (for this warning or a new one) should be done later, but early
enough not to be affected by optimizations.

One of the goals would be to detect a missing check of a return value of a
function call (typically an error status). For such a typical usage, the same
variable can be reused for each error status.

  int r;

  r = call1 (...);
  check (r);
  /* ... */
  r = call2 (...);
  check (r);
  /* ... */
  r = call3 (...);
  check (r);

If a check is missing, there should be a warning.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/95057] missing -Wunused-but-set-variable warning on multiple assignments, not all of them used
  2020-05-11 12:50 [Bug c/95057] New: missing -Wunused-but-set-variable warning on multiple assignments, not all of them used vincent-gcc at vinc17 dot net
                   ` (3 preceding siblings ...)
  2020-05-11 13:39 ` vincent-gcc at vinc17 dot net
@ 2023-07-18 22:34 ` vincent-gcc at vinc17 dot net
  2023-07-18 22:46 ` vincent-gcc at vinc17 dot net
  5 siblings, 0 replies; 7+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2023-07-18 22:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95057

--- Comment #5 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> ---
FYI, Clang 16 does not warn either on the testcases provided in comment 0 (bug
report).

But contrary to GCC (tested with master r14-1713-g6631fe419c6 - Debian
gcc-snapshot package 20230613-1), Clang 15 and 16 both warn on f and g below:

int h (void);

void f (void)
{
  int i = h ();
  i++;
}

void g (void)
{
  for (int i = 0 ;; i++)
    if (h ())
      break;
}

zira:~> clang-15 -c -Wunused-but-set-variable warn-inc.c
warn-inc.c:5:7: warning: variable 'i' set but not used
[-Wunused-but-set-variable]
  int i = h ();
      ^
warn-inc.c:11:12: warning: variable 'i' set but not used
[-Wunused-but-set-variable]
  for (int i = 0 ;; i++)
           ^
2 warnings generated.

Thanks to this detection, a test with Clang 16 found two issues in GNU MPFR
(one cosmetic about a useless loop variable and one important in the
testsuite). So it is useful to consider such particular cases.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/95057] missing -Wunused-but-set-variable warning on multiple assignments, not all of them used
  2020-05-11 12:50 [Bug c/95057] New: missing -Wunused-but-set-variable warning on multiple assignments, not all of them used vincent-gcc at vinc17 dot net
                   ` (4 preceding siblings ...)
  2023-07-18 22:34 ` vincent-gcc at vinc17 dot net
@ 2023-07-18 22:46 ` vincent-gcc at vinc17 dot net
  5 siblings, 0 replies; 7+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2023-07-18 22:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95057

--- Comment #6 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> ---
Well, for the ++, --, +=, -=, *=, etc. operators, that's PR44677 (though it is
unclear on what it should cover).

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-07-18 22:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-11 12:50 [Bug c/95057] New: missing -Wunused-but-set-variable warning on multiple assignments, not all of them used vincent-gcc at vinc17 dot net
2020-05-11 12:53 ` [Bug c/95057] " jakub at gcc dot gnu.org
2020-05-11 13:05 ` vincent-gcc at vinc17 dot net
2020-05-11 13:09 ` jakub at gcc dot gnu.org
2020-05-11 13:39 ` vincent-gcc at vinc17 dot net
2023-07-18 22:34 ` vincent-gcc at vinc17 dot net
2023-07-18 22:46 ` vincent-gcc at vinc17 dot net

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).