Re: [PATCH] driver base: slience unused warning

From: Maciej W. Rozycki
Date: Thu Oct 05 2023 - 10:34:56 EST


On Wed, 4 Oct 2023, Dan Carpenter wrote:

> > > This variable is been used but never be read, so gcc and W=1 give such
> > > warning.
> > >
> > > drivers/base/module.c:36:6: error:
> > > variable ‘no_warn’ set but not used [-Werror=unused-but-set-variable]
> > >
> > > I wanted to use "__maybe_unused" to avoid  this warning.
> > >
> > > However it seems like a wrong using of "__maybe_unused" as Greg KH said:
> > >
> > > "But no_warn is being used in this file, it's being set but not read
> > > which is ok. That's a real use, so this change really isn't correct,
> > > sorry."
> >
> > The warning itself is a real issue to be sorted though. Is this a use
> > case for `#pragma GCC diagnostic'?
>
> I thought Greg liked using __maybe_unused in this case? This is
> drivers/base. Do the rest of us even get a vote? ;)
>
> If I do have a vote then #pragma is always the worst option. Linus has
> taught me to dislike pragmas a lot.

I'm not a great supporter of this solution, but at least it's guaranteed
to work by definition. Otherwise we could try to outsmart the compiler;
perhaps:

(no_warn = sysfs_create_link(&drv->p->kobj, &mk->kobj,
"module")), no_warn;

would work. At the end of the day it's us who told the compiler to warn
if the result of `sysfs_create_link' is unused with all the consequences.
And while assigning to `no_warn' technically fulfils the requirement, the
variable itself isn't used beyond being assigned to, which the compiler
rightfully complains about because we asked for it. It's up to us really
to tell the compiler what we want it to complain about and what we do not.

Maciej