diff mbox series

ACPI: thermal: replace ternary operator with min_t()

Message ID 20230328031629.202268-1-yijiangshan@kylinos.cn
State Accepted
Commit 0dc9a715578b041b6159714f23f94cf71a214411
Headers show
Series ACPI: thermal: replace ternary operator with min_t() | expand

Commit Message

Jiangshan Yi March 28, 2023, 3:16 a.m. UTC
Fix the following coccicheck warning:

drivers/acpi/thermal.c:422: WARNING opportunity for min().

min_t() macro is defined in include/linux/minmax.h. It avoids multiple
evaluations of the arguments when non-constant and performs strict
type-checking.

Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
---
 drivers/acpi/thermal.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

Comments

Rafael J. Wysocki March 30, 2023, 5:11 p.m. UTC | #1
On Tue, Mar 28, 2023 at 5:17 AM Jiangshan Yi <yijiangshan@kylinos.cn> wrote:
>
> Fix the following coccicheck warning:

This is not a fix, because the current code is correct AFAICS.

It merely makes the code follow the coccicheck recommendation, which
is a cleanup (although arguably a good one).

But because the new code is way more readable, I've applied this as
6.4 material (with edits in the changelog).

Thanks!

> drivers/acpi/thermal.c:422: WARNING opportunity for min().
>
> min_t() macro is defined in include/linux/minmax.h. It avoids multiple
> evaluations of the arguments when non-constant and performs strict
> type-checking.
>
> Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
> ---
>  drivers/acpi/thermal.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/acpi/thermal.c b/drivers/acpi/thermal.c
> index 0b4b844f9d4c..179f41196a9d 100644
> --- a/drivers/acpi/thermal.c
> +++ b/drivers/acpi/thermal.c
> @@ -419,10 +419,9 @@ static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
>                                          * the next higher trip point
>                                          */
>                                         tz->trips.active[i-1].temperature =
> -                                               (tz->trips.active[i-2].temperature <
> -                                               celsius_to_deci_kelvin(act) ?
> -                                               tz->trips.active[i-2].temperature :
> -                                               celsius_to_deci_kelvin(act));
> +                                               min_t(unsigned long,
> +                                                     tz->trips.active[i-2].temperature,
> +                                                     celsius_to_deci_kelvin(act));
>
>                                 break;
>                         } else {
> --
> 2.25.1
>
David Laight March 31, 2023, 8:58 a.m. UTC | #2
From: Rafael J. Wysocki
> Sent: 30 March 2023 18:12
> 
> On Tue, Mar 28, 2023 at 5:17 AM Jiangshan Yi <yijiangshan@kylinos.cn> wrote:
> >
> > Fix the following coccicheck warning:
> 
> This is not a fix, because the current code is correct AFAICS.
> 
> It merely makes the code follow the coccicheck recommendation, which
> is a cleanup (although arguably a good one).
> 
> But because the new code is way more readable, I've applied this as
> 6.4 material (with edits in the changelog).

Also if you need to use min_t() there is really something wrong
with your types.

> > drivers/acpi/thermal.c:422: WARNING opportunity for min().
> >
> > min_t() macro is defined in include/linux/minmax.h. It avoids multiple
> > evaluations of the arguments when non-constant and performs strict
> > type-checking.

min_t() casts the arguments and performs absolutely no type-checking.
The casts can even mask off high bits that ought to be checked.

	David

> >
> > Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
> > ---
> >  drivers/acpi/thermal.c | 7 +++----
> >  1 file changed, 3 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/acpi/thermal.c b/drivers/acpi/thermal.c
> > index 0b4b844f9d4c..179f41196a9d 100644
> > --- a/drivers/acpi/thermal.c
> > +++ b/drivers/acpi/thermal.c
> > @@ -419,10 +419,9 @@ static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
> >                                          * the next higher trip point
> >                                          */
> >                                         tz->trips.active[i-1].temperature =
> > -                                               (tz->trips.active[i-2].temperature <
> > -                                               celsius_to_deci_kelvin(act) ?
> > -                                               tz->trips.active[i-2].temperature :
> > -                                               celsius_to_deci_kelvin(act));
> > +                                               min_t(unsigned long,
> > +                                                     tz->trips.active[i-2].temperature,
> > +                                                     celsius_to_deci_kelvin(act));
> >
> >                                 break;
> >                         } else {
> > --
> > 2.25.1
> >

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
diff mbox series

Patch

diff --git a/drivers/acpi/thermal.c b/drivers/acpi/thermal.c
index 0b4b844f9d4c..179f41196a9d 100644
--- a/drivers/acpi/thermal.c
+++ b/drivers/acpi/thermal.c
@@ -419,10 +419,9 @@  static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
 					 * the next higher trip point
 					 */
 					tz->trips.active[i-1].temperature =
-						(tz->trips.active[i-2].temperature <
-						celsius_to_deci_kelvin(act) ?
-						tz->trips.active[i-2].temperature :
-						celsius_to_deci_kelvin(act));
+						min_t(unsigned long,
+						      tz->trips.active[i-2].temperature,
+						      celsius_to_deci_kelvin(act));
 
 				break;
 			} else {