Skip to contentSkip to conversation

staging: iio: meter: use min() for comparison and assignment

PATCH In review Deepak R Varma 5 participants 9 messages Nov 7, 2022 – Nov 12, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: iio: meter: use min() for comparison and assignment

Simplify code by using recommended min helper macro for logical evaluation and value assignment. This issue is identified by coccicheck using the minmax.cocci file.

---
 drivers/staging/iio/meter/ade7854-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/meter/ade7854-i2c.c +1 -1
index a9a06e8dda51..a6ce7b24cc8f 100644
@@ -61,7 +61,7 @@ static int ade7854_i2c_write_reg(struct device *dev,
 unlock:
 	mutex_unlock(&st->buf_lock);
 
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);
 }
 
 static int ade7854_i2c_read_reg(struct device *dev,

--
2.34.1

#1 lore raw
Quoted 14 lines · Deepak R Varma

Simplify code by using recommended min helper macro for logical evaluation and value assignment. This issue is identified by coccicheck using the minmax.cocci file.

---
 drivers/staging/iio/meter/ade7854-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/meter/ade7854-i2c.c +0 -0
index a9a06e8dda51..a6ce7b24cc8f 100644

drivers/staging/iio/meter/ade7854-i2c.c static int ade7854_i2c_write_reg(struct device *dev, · line 64

Quoted 5 lines
drivers/staging/iio/meter/ade7854-i2c.c +1 -1
@@ -61,7 +61,7 @@ static int ade7854_i2c_write_reg(struct device *dev,
 unlock:
 	mutex_unlock(&st->buf_lock);
 
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);

The original code is better.

If it's a failure return the error code. If it's not return zero.

You can only compare apples to apples. min() makes sense if you're talking about two lengths. But here if ret is negative that's an error code. If it's positive that's the number of bytes. If the error code is less than the number of bytes then return that? What??? It makes no sense.

In terms of run time, this patch is fine but in terms of reading the code using min() makes it less readable.

regards,
dan carpenter

#2 lore raw
Quoted 23 lines · Dan Carpenter
Quoted 14 lines · Deepak R Varma

Simplify code by using recommended min helper macro for logical evaluation and value assignment. This issue is identified by coccicheck using the minmax.cocci file.

---
 drivers/staging/iio/meter/ade7854-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/meter/ade7854-i2c.c +0 -0
index a9a06e8dda51..a6ce7b24cc8f 100644

drivers/staging/iio/meter/ade7854-i2c.c static int ade7854_i2c_write_reg(struct device *dev, · line 64

Quoted 5 lines
drivers/staging/iio/meter/ade7854-i2c.c +1 -1
@@ -61,7 +61,7 @@ static int ade7854_i2c_write_reg(struct device *dev,
 unlock:
 	mutex_unlock(&st->buf_lock);
 
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);

The original code is better.

Quoted 7 lines

If it's a failure return the error code. If it's not return zero.

You can only compare apples to apples. min() makes sense if you're talking about two lengths. But here if ret is negative that's an error code. If it's positive that's the number of bytes. If the error code is less than the number of bytes then return that? What??? It makes no sense.

Thanks for your view point. I agree.

Quoted 3 lines

In terms of run time, this patch is fine but in terms of reading the code using min() makes it less readable.

Okay, The proposal does not make much difference, so will leave the original line as is.

Thank you,
./drv

Quoted 4 lines

regards,
dan carpenter

#3 lore raw
In reply to Dan Carpenter
Quoted 25 lines · Dan Carpenter
Quoted 14 lines · Deepak R Varma

Simplify code by using recommended min helper macro for logical evaluation and value assignment. This issue is identified by coccicheck using the minmax.cocci file.

---
 drivers/staging/iio/meter/ade7854-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/meter/ade7854-i2c.ck renamed from drivers/staging/iio/meter/ade7854-i2c.c +0 -0
index a9a06e8dda51..a6ce7b24cc8f 100644

drivers/staging/iio/meter/ade7854-i2c.ck static int ade7854_i2c_write_reg(struct device *dev, · line 64

Quoted 5 lines
drivers/staging/iio/meter/ade7854-i2c.ck renamed from drivers/staging/iio/meter/ade7854-i2c.c +1 -1
@@ -61,7 +61,7 @@ static int ade7854_i2c_write_reg(struct device *dev,
 unlock:
 	mutex_unlock(&st->buf_lock);
 
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);

The original code is better.

If it's a failure return the error code. If it's not return zero.

Quoted 8 lines

You can only compare apples to apples. min() makes sense if you're talking about two lengths. But here if ret is negative that's an error code. If it's positive that's the number of bytes. If the error code is less than the number of bytes then return that? What??? It makes no sense.

In terms of run time, this patch is fine but in terms of reading the code using min() makes it less readable.

It's not a runtime question, either should compile to the same object code. It's definitely a readabiity and standardization issue.

In this case, IMO it'd be better to use the much more common

	if (ret < 0)
		return ret;

	return 0;
#4 lore raw
Quoted 6 lines · Joe Perches
Quoted 2 lines

In terms of run time, this patch is fine but in terms of reading the code using min() makes it less readable.

It's not a runtime question, either should compile to the same object code. It's definitely a readabiity and standardization issue.

Quoted 6 lines

In this case, IMO it'd be better to use the much more common

	if (ret < 0)
		return ret;

	return 0;

I also prefer this format.

But at the same time, I can't advise Deepak to go around changing existing code where the author like ternaries.

regards,
dan carpenter

#5 lore raw
Quoted 12 lines · Dan Carpenter
Quoted 11 lines · Joe Perches
Quoted 2 lines

In terms of run time, this patch is fine but in terms of reading the code using min() makes it less readable.

It's not a runtime question, either should compile to the same object code. It's definitely a readabiity and standardization issue.

In this case, IMO it'd be better to use the much more common

	if (ret < 0)
		return ret;
Quoted 6 lines
Quoted 1 line
	return 0;

I also prefer this format.

But at the same time, I can't advise Deepak to go around changing existing code where the author like ternaries.

Thank you Joe, Dan. Just to conclude, I will leave the line untouched as it is no big advantage and the current format is more readable.

./drv

Quoted 4 lines

regards,
dan carpenter

#6 lore raw
In reply to Deepak R Varma
Quoted 14 lines · Deepak R Varma

Simplify code by using recommended min helper macro for logical evaluation and value assignment. This issue is identified by coccicheck using the minmax.cocci file.

---
 drivers/staging/iio/meter/ade7854-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/meter/ade7854-i2c.c +0 -0
index a9a06e8dda51..a6ce7b24cc8f 100644

drivers/staging/iio/meter/ade7854-i2c.c static int ade7854_i2c_write_reg(struct device *dev, · line 64

Quoted 5 lines
drivers/staging/iio/meter/ade7854-i2c.c +1 -1
@@ -61,7 +61,7 @@ static int ade7854_i2c_write_reg(struct device *dev,
 unlock:
 	mutex_unlock(&st->buf_lock);
 
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);

As others have said, this isn't ok, and I hate ? : usage, so if you want, spell that out please.

thanks,

greg k-h

#7 lore raw
Quoted 18 lines · Greg Kroah-Hartman
Quoted 17 lines · Deepak R Varma

Simplify code by using recommended min helper macro for logical evaluation and value assignment. This issue is identified by coccicheck using the minmax.cocci file.

---
 drivers/staging/iio/meter/ade7854-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/meter/ade7854-i2c.c +0 -0
index a9a06e8dda51..a6ce7b24cc8f 100644
@@ -61,7 +61,7 @@ static int ade7854_i2c_write_reg(struct device *dev,
 unlock:
 	mutex_unlock(&st->buf_lock);
 
Quoted 5 lines

drivers/staging/iio/meter/ade7854-i2c.c static int ade7854_i2c_write_reg(struct device *dev, · line 64

Quoted 2 lines
drivers/staging/iio/meter/ade7854-i2c.c +1 -1
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);

As others have said, this isn't ok, and I hate ? : usage, so if you want, spell that out please.

Hello Greg,
Just want to make sure I am getting it right:
Are you suggesting me to resubmit the patch with revised patch description?

Should I consider using the "if" based evaluation rather than using min() macro?

Thank you,
./drv

Quoted 5 lines

thanks,

greg k-h

maintainer
#8 lore raw
Quoted 25 lines · On Tue, 8 Nov 2022 21:06:24 +0530 Deepak R Varma <[email protected]> wrote:
Quoted 18 lines · Greg Kroah-Hartman
Quoted 17 lines · Deepak R Varma

Simplify code by using recommended min helper macro for logical evaluation and value assignment. This issue is identified by coccicheck using the minmax.cocci file.

---
 drivers/staging/iio/meter/ade7854-i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/meter/ade7854-i2c.c +0 -0
index a9a06e8dda51..a6ce7b24cc8f 100644
@@ -61,7 +61,7 @@ static int ade7854_i2c_write_reg(struct device *dev,
 unlock:
 	mutex_unlock(&st->buf_lock);
 
Quoted 5 lines

drivers/staging/iio/meter/ade7854-i2c.c static int ade7854_i2c_write_reg(struct device *dev, · line 64

Quoted 2 lines
drivers/staging/iio/meter/ade7854-i2c.c +1 -1
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);  

As others have said, this isn't ok, and I hate ? : usage, so if you want, spell that out please.

Quoted 5 lines

Hello Greg,
Just want to make sure I am getting it right:
Are you suggesting me to resubmit the patch with revised patch description?

Should I consider using the "if" based evaluation rather than using min() macro?

For IIO staging drivers, I'd take a cleanup that moved to

	if (ret < 0)
		return ret;

	return 0;

As others have suggested though, not a good idea to do this broadly as it would be a lot of noise. We don't mind noise so much for staging drivers :)

Jonathan

Quoted 11 lines

Thank you,
./drv

Quoted 5 lines

thanks,

greg k-h