public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Compiler message -Wunused confusion
@ 2024-02-14 11:11 A. Mc.
  2024-02-14 12:41 ` Segher Boessenkool
  2024-02-14 12:44 ` David Brown
  0 siblings, 2 replies; 4+ messages in thread
From: A. Mc. @ 2024-02-14 11:11 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 477 bytes --]

Hello,

I am receiving the gcc compiler warning -Wunused-value computed not used. I
can confirm this is true in my embedded application, the statement never
switches. But I am completely confused as to why. It's essentially the
following code:

volatile bool foo;

while (true) {
if (foo == true){
       //do something
       foo = false;}
else{
       //do something else
       foo = true;}
}

Which never switches. What could be the cause of this?

Many thanks in advance!

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

* Re: Compiler message -Wunused confusion
  2024-02-14 11:11 Compiler message -Wunused confusion A. Mc.
@ 2024-02-14 12:41 ` Segher Boessenkool
  2024-02-15 15:31   ` Jędrzej Dudkiewicz
  2024-02-14 12:44 ` David Brown
  1 sibling, 1 reply; 4+ messages in thread
From: Segher Boessenkool @ 2024-02-14 12:41 UTC (permalink / raw)
  To: A. Mc.; +Cc: gcc-help

On Wed, Feb 14, 2024 at 03:11:58AM -0800, A. Mc. via Gcc-help wrote:
> I am receiving the gcc compiler warning -Wunused-value computed not used. I
> can confirm this is true in my embedded application, the statement never
> switches. But I am completely confused as to why. It's essentially the
> following code:
> 
> volatile bool foo;
> 
> while (true) {
> if (foo == true){
>        //do something
>        foo = false;}
> else{
>        //do something else
>        foo = true;}
> }
> 
> Which never switches. What could be the cause of this?

Your code assigns to foo, but never uses foo again.  This is likely a
programming mistake, which is what the warning is for.

It is not that the compiler tells you to not do that.  If you want to
write code like that, you are free to do that, and the compiler will
correctly compile it.  If you have warning messages enabled
(-Wunused-value, enabled by -Wall for example) you get diagnostics like
this, where apparently you made a mistake in your programming.


Segher

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

* Re: Compiler message -Wunused confusion
  2024-02-14 11:11 Compiler message -Wunused confusion A. Mc.
  2024-02-14 12:41 ` Segher Boessenkool
@ 2024-02-14 12:44 ` David Brown
  1 sibling, 0 replies; 4+ messages in thread
From: David Brown @ 2024-02-14 12:44 UTC (permalink / raw)
  To: gcc-help

On 14/02/2024 12:11, A. Mc. via Gcc-help wrote:
> Hello,
> 
> I am receiving the gcc compiler warning -Wunused-value computed not used. I
> can confirm this is true in my embedded application, the statement never
> switches. But I am completely confused as to why. It's essentially the
> following code:
> 
> volatile bool foo;
> 
> while (true) {
> if (foo == true){
>         //do something
>         foo = false;}
> else{
>         //do something else
>         foo = true;}
> }
> 
> Which never switches. What could be the cause of this?
> 
> Many thanks in advance!
> 

My guess is that you have simplified the code a bit too much here. 
Something else is wrong.  (It is a good idea to pare down the code to 
the minimum that demonstrates the problem, as you have tried to do here. 
  But I think you have removed to much!)

David



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

* Re: Compiler message -Wunused confusion
  2024-02-14 12:41 ` Segher Boessenkool
@ 2024-02-15 15:31   ` Jędrzej Dudkiewicz
  0 siblings, 0 replies; 4+ messages in thread
From: Jędrzej Dudkiewicz @ 2024-02-15 15:31 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: gcc-help

[-- Attachment #1: Type: text/plain, Size: 900 bytes --]

On Wed, Feb 14, 2024 at 1:42 PM Segher Boessenkool <
segher@kernel.crashing.org> wrote:

> On Wed, Feb 14, 2024 at 03:11:58AM -0800, A. Mc. via Gcc-help wrote:
> > volatile bool foo;
> >
> > while (true) {
> > if (foo == true){
> >        //do something
> >        foo = false;}
> > else{
> >        //do something else
> >        foo = true;}
> > }
> >
> > Which never switches. What could be the cause of this?
>
> Your code assigns to foo, but never uses foo again.  This is likely a
> programming mistake, which is what the warning is for.
>

What do you mean by "never uses foo again"? In each iteration foo is
compared to true and in each iteration it is flipped, so it should be true,
false, true, false and so on.
-- 
Jędrzej Dudkiewicz

I really hate this damn machine, I wish that they would sell it.
It never does just what I want, but only what I tell it.

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

end of thread, other threads:[~2024-02-15 15:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-14 11:11 Compiler message -Wunused confusion A. Mc.
2024-02-14 12:41 ` Segher Boessenkool
2024-02-15 15:31   ` Jędrzej Dudkiewicz
2024-02-14 12:44 ` David Brown

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).