Skip to contentSkip to conversation

hwmon: Use min() instead of doing it manually

PATCH Superseded Jiapeng Chong 3 participants 4 messages Dec 27, 2021 – Dec 28, 2021 Hardware monitoring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: Use min() instead of doing it manually

Eliminate following coccicheck warning:

./drivers/hwmon/nzxt-smart2.c:461:12-13: WARNING opportunity for min().

---
 drivers/hwmon/nzxt-smart2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/nzxt-smart2.c +1 -1
index 534d39b8908e..b30de7441fbb 100644
@@ -458,7 +458,7 @@ static int send_output_report(struct drvdata *drvdata, const void *data,
 
 	ret = hid_hw_output_report(drvdata->hid, drvdata->output_buffer,
 				   sizeof(drvdata->output_buffer));
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);
 }
 
 static int set_pwm(struct drvdata *drvdata, int channel, long val)
Signature · 1 line
2.20.1.7.g153144c
maintainer NAK
#1 lore raw
Quoted 15 lines · Jiapeng Chong

Eliminate following coccicheck warning:

./drivers/hwmon/nzxt-smart2.c:461:12-13: WARNING opportunity for min().

---
  drivers/hwmon/nzxt-smart2.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/nzxt-smart2.c +0 -0
index 534d39b8908e..b30de7441fbb 100644

drivers/hwmon/nzxt-smart2.c static int send_output_report(struct drvdata *drvdata, const void *data, · line 461

Quoted 5 lines
drivers/hwmon/nzxt-smart2.c +1 -1
@@ -458,7 +458,7 @@ static int send_output_report(struct drvdata *drvdata, const void *data,
 
  	ret = hid_hw_output_report(drvdata->hid, drvdata->output_buffer,
  				   sizeof(drvdata->output_buffer));
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);

Nack, that is just confusing. ret is an error if < 0, and min obfuscates that we want to return an error or 0.

Guenter

#2 lore raw
Quoted 26 lines · Guenter Roeck
Quoted 15 lines · Jiapeng Chong

Eliminate following coccicheck warning:

./drivers/hwmon/nzxt-smart2.c:461:12-13: WARNING opportunity for min().

---
  drivers/hwmon/nzxt-smart2.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/nzxt-smart2.c +0 -0
index 534d39b8908e..b30de7441fbb 100644

drivers/hwmon/nzxt-smart2.c static int send_output_report(struct drvdata *drvdata, const void *data, · line 461

Quoted 5 lines
drivers/hwmon/nzxt-smart2.c +1 -1
@@ -458,7 +458,7 @@ static int send_output_report(struct drvdata *drvdata, const void *data,
 
      ret = hid_hw_output_report(drvdata->hid, drvdata->output_buffer,
                                 sizeof(drvdata->output_buffer));
-     return ret < 0 ? ret : 0;
+     return min(ret, 0);

Nack, that is just confusing. ret is an error if < 0, and min obfuscates that we want to return an error or 0.

Quoted 1 line

Guenter

Should I change that ternary operator to a full "if" maybe? Apparently, both some people and some tools read it as "min()".

maintainer
#3 lore raw
Quoted 27 lines · Aleksandr Mezin
Quoted 26 lines · Guenter Roeck
Quoted 15 lines · Jiapeng Chong

Eliminate following coccicheck warning:

./drivers/hwmon/nzxt-smart2.c:461:12-13: WARNING opportunity for min().

---
   drivers/hwmon/nzxt-smart2.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/nzxt-smart2.c +0 -0
index 534d39b8908e..b30de7441fbb 100644

drivers/hwmon/nzxt-smart2.c static int send_output_report(struct drvdata *drvdata, const void *data, · line 461

Quoted 5 lines
drivers/hwmon/nzxt-smart2.c +1 -1
@@ -458,7 +458,7 @@ static int send_output_report(struct drvdata *drvdata, const void *data,
 
       ret = hid_hw_output_report(drvdata->hid, drvdata->output_buffer,
                                  sizeof(drvdata->output_buffer));
-     return ret < 0 ? ret : 0;
+     return min(ret, 0);

Nack, that is just confusing. ret is an error if < 0, and min obfuscates that we want to return an error or 0.

Quoted 5 lines
Quoted 1 line

Guenter

Should I change that ternary operator to a full "if" maybe? Apparently, both some people and some tools read it as "min()".

No, the code is good as is, using if() doesn't really make a difference, and I _really_ don't want to encourage people to start submitting patches to change the other 100+ instances of the same code in the kernel.

Guenter