# Re: \[peterz-queue:sched/core 13/19\] kernel/sched/fair.c:892:34: warning: variable 'stats' set but not used

- List: linux-toolchains
- 3 messages, 2 authors
- Page: https://ratatoskr.run/linux-toolchains/2021/09/1504075/t

## Re: \[peterz-queue:sched/core 13/19\] kernel/sched/fair.c:892:34: warning: variable 'stats' set but not used

- From: Peter Zijlstra \<peterz@infradead.org\>
- Date: 2021-09-11 12:21:38 
- Message: https://ratatoskr.run/linux-toolchains/2021/09/1504075.md

```text
On Sat, Sep 11, 2021 at 02:21:26AM +0800, kernel test robot wrote:

> compiler: m68k-linux-gcc (GCC) 11.2.0

>    kernel/sched/fair.c: In function 'update_curr':
>    kernel/sched/fair.c:860:42: warning: unused variable 'stats' [-Wunused-variable]
>      860 |                 struct sched_statistics *stats = __schedstats_from_se(curr);
>          |                                          ^~~~~

OK, compiler guys, this code reads like:

#define schedstats_enabled()	(0)
#define __schedstat_set(x, y)	do { } while (0)


	if (schedstats_enabled()) {
		struct sched_statistics *stats = __schedstats_from_se(curr);

		__schedstat_set(stats->exec_max,
				max(delta_exec, stats->exec_max));
	}

So yes, we initialize a variable that then isn't used, but the whole
bloody thing is inside if (0) which will not ever get ran *anyway*.

This is a crap warning if ever I saw one...
```

## Re: \[peterz-queue:sched/core 13/19\] kernel/sched/fair.c:892:34: warning: variable 'stats' set but not used

- From: Segher Boessenkool \<hidden\>
- Date: 2021-09-11 21:35:02 
- Message: https://ratatoskr.run/linux-toolchains/2021/09/1504361.md

```text
Hi!

On Sat, Sep 11, 2021 at 02:20:49PM +0200, Peter Zijlstra wrote:
> On Sat, Sep 11, 2021 at 02:21:26AM +0800, kernel test robot wrote:
> > compiler: m68k-linux-gcc (GCC) 11.2.0
> 
> >    kernel/sched/fair.c: In function 'update_curr':
> >    kernel/sched/fair.c:860:42: warning: unused variable 'stats' [-Wunused-variable]
> >      860 |                 struct sched_statistics *stats = __schedstats_from_se(curr);
> >          |                                          ^~~~~
> 
> OK, compiler guys, this code reads like:
> 
> #define schedstats_enabled()	(0)
> #define __schedstat_set(x, y)	do { } while (0)
> 
> 
> 	if (schedstats_enabled()) {
> 		struct sched_statistics *stats = __schedstats_from_se(curr);
> 
> 		__schedstat_set(stats->exec_max,
> 				max(delta_exec, stats->exec_max));
> 	}
> 
> So yes, we initialize a variable that then isn't used, but the whole
> bloody thing is inside if (0) which will not ever get ran *anyway*.
> 
> This is a crap warning if ever I saw one...

Yes, we really should warn "do not use a preprocessor macro if what you
want is a function"?  The variable really *is* unused, with this macro.

If we would remove dead code before warning about unused variables
there would be many *more* false positives, fwiw.


Segher
```

## Re: \[peterz-queue:sched/core 13/19\] kernel/sched/fair.c:892:34: warning: variable 'stats' set but not used

- From: Peter Zijlstra \<peterz@infradead.org\>
- Date: 2021-09-11 22:34:00 
- Message: https://ratatoskr.run/linux-toolchains/2021/09/1504366.md

```text
On Sat, Sep 11, 2021 at 04:30:41PM -0500, Segher Boessenkool wrote:
> Hi!
> 
> On Sat, Sep 11, 2021 at 02:20:49PM +0200, Peter Zijlstra wrote:
> > On Sat, Sep 11, 2021 at 02:21:26AM +0800, kernel test robot wrote:
> > > compiler: m68k-linux-gcc (GCC) 11.2.0
> > 
> > >    kernel/sched/fair.c: In function 'update_curr':
> > >    kernel/sched/fair.c:860:42: warning: unused variable 'stats' [-Wunused-variable]
> > >      860 |                 struct sched_statistics *stats = __schedstats_from_se(curr);
> > >          |                                          ^~~~~
> > 
> > OK, compiler guys, this code reads like:
> > 
> > #define schedstats_enabled()	(0)
> > #define __schedstat_set(x, y)	do { } while (0)
> > 
> > 
> > 	if (schedstats_enabled()) {
> > 		struct sched_statistics *stats = __schedstats_from_se(curr);
> > 
> > 		__schedstat_set(stats->exec_max,
> > 				max(delta_exec, stats->exec_max));
> > 	}
> > 
> > So yes, we initialize a variable that then isn't used, but the whole
> > bloody thing is inside if (0) which will not ever get ran *anyway*.
> > 
> > This is a crap warning if ever I saw one...
> 
> Yes, we really should warn "do not use a preprocessor macro if what you
> want is a function"?  The variable really *is* unused, with this macro.

Why would I want to write a bunch of one-off functions and preprocessor
guard them? That's going to be a mess.

> If we would remove dead code before warning about unused variables
> there would be many *more* false positives, fwiw.

Not if you also remove any variables declared in dead code and all
variables only used in the dead code.

```
