linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: class: usblp: replace conditional statement with min()
@ 2021-08-03  0:28 Salah Triki
  2021-08-03  1:50 ` Pete Zaitcev
  0 siblings, 1 reply; 3+ messages in thread
From: Salah Triki @ 2021-08-03  0:28 UTC (permalink / raw)
  To: Pete Zaitcev, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

Replace conditional statement with min() in order to make code cleaner. Issue
found by coccinelle.

Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
 drivers/usb/class/usblp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c
index f27b4aecff3d..fd779a989526 100644
--- a/drivers/usb/class/usblp.c
+++ b/drivers/usb/class/usblp.c
@@ -264,7 +264,7 @@ static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, i
 	dev_dbg(&usblp->intf->dev,
 		"usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d\n",
 		request, !!dir, recip, value, index, len, retval);
-	return retval < 0 ? retval : 0;
+	return min(retval, 0);
 }
 
 #define usblp_read_status(usblp, status)\
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: class: usblp: replace conditional statement with min()
  2021-08-03  0:28 [PATCH] usb: class: usblp: replace conditional statement with min() Salah Triki
@ 2021-08-03  1:50 ` Pete Zaitcev
  2021-08-03  5:13   ` Greg Kroah-Hartman
  0 siblings, 1 reply; 3+ messages in thread
From: Pete Zaitcev @ 2021-08-03  1:50 UTC (permalink / raw)
  To: Salah Triki; +Cc: Greg Kroah-Hartman, linux-usb, linux-kernel, zaitcev

On Tue, 3 Aug 2021 01:28:06 +0100
Salah Triki <salah.triki@gmail.com> wrote:

> Replace conditional statement with min() in order to make code cleaner. Issue
> found by coccinelle.

> +++ b/drivers/usb/class/usblp.c
>  		request, !!dir, recip, value, index, len, retval);
> -	return retval < 0 ? retval : 0;
> +	return min(retval, 0);
>  }

I'm very much against this change. The function min() is there
for numeric values. But here we have a situation where we
do one thing if there was an error, and another thing if
there was no error.

This sort of abuse is exactly why blindly clicking heels
whenever a tool tells you is not optimal.

If the objective is to shut the tool up, please consider
the following instead:

diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c
index 9596e4279294..bbcbcf199fa9 100644
--- a/drivers/usb/class/usblp.c
+++ b/drivers/usb/class/usblp.c
@@ -264,7 +264,9 @@ static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, i
        dev_dbg(&usblp->intf->dev,
                "usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d\n",
                request, !!dir, recip, value, index, len, retval);
-       return retval < 0 ? retval : 0;
+       if (retval < 0)
+               return retval;
+       return 0;
 }
 
 #define usblp_read_status(usblp, status)\


-- Pete


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: class: usblp: replace conditional statement with min()
  2021-08-03  1:50 ` Pete Zaitcev
@ 2021-08-03  5:13   ` Greg Kroah-Hartman
  0 siblings, 0 replies; 3+ messages in thread
From: Greg Kroah-Hartman @ 2021-08-03  5:13 UTC (permalink / raw)
  To: Pete Zaitcev; +Cc: Salah Triki, linux-usb, linux-kernel

On Mon, Aug 02, 2021 at 08:50:22PM -0500, Pete Zaitcev wrote:
> On Tue, 3 Aug 2021 01:28:06 +0100
> Salah Triki <salah.triki@gmail.com> wrote:
> 
> > Replace conditional statement with min() in order to make code cleaner. Issue
> > found by coccinelle.
> 
> > +++ b/drivers/usb/class/usblp.c
> >  		request, !!dir, recip, value, index, len, retval);
> > -	return retval < 0 ? retval : 0;
> > +	return min(retval, 0);
> >  }
> 
> I'm very much against this change. The function min() is there
> for numeric values. But here we have a situation where we
> do one thing if there was an error, and another thing if
> there was no error.
> 
> This sort of abuse is exactly why blindly clicking heels
> whenever a tool tells you is not optimal.
> 
> If the objective is to shut the tool up, please consider
> the following instead:
> 
> diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c
> index 9596e4279294..bbcbcf199fa9 100644
> --- a/drivers/usb/class/usblp.c
> +++ b/drivers/usb/class/usblp.c
> @@ -264,7 +264,9 @@ static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, i
>         dev_dbg(&usblp->intf->dev,
>                 "usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d\n",
>                 request, !!dir, recip, value, index, len, retval);
> -       return retval < 0 ? retval : 0;
> +       if (retval < 0)
> +               return retval;
> +       return 0;
>  }

I agree with Pete here, this is the "correct" fix for this, using min()
is not ok here.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-08-03  5:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-03  0:28 [PATCH] usb: class: usblp: replace conditional statement with min() Salah Triki
2021-08-03  1:50 ` Pete Zaitcev
2021-08-03  5:13   ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).