All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] net: Replace the ternary conditional operator with min()
@ 2023-05-30  8:45 Lu Hongfei
  2023-05-30  9:03 ` Heiner Kallweit
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Lu Hongfei @ 2023-05-30  8:45 UTC (permalink / raw)
  To: Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	open list:ETHERNET PHY LIBRARY, open list
  Cc: opensource.kernel, luhongfei

It would be better to replace the traditional ternary conditional
operator with min()

Signed-off-by: Lu Hongfei <luhongfei@vivo.com>
---
 drivers/net/phy/phy.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 drivers/net/phy/phy.c

diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index 0c0df38cd1ab..a8beb4ab8451
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -1002,7 +1002,7 @@ static int phy_poll_aneg_done(struct phy_device *phydev)
 	if (!ret)
 		return -ETIMEDOUT;
 
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);
 }
 
 int phy_ethtool_ksettings_set(struct phy_device *phydev,
@@ -1526,7 +1526,7 @@ int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
 		ret = phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
 				       MDIO_PCS_CTRL1_CLKSTOP_EN);
 
-	return ret < 0 ? ret : 0;
+	return min(ret, 0);
 }
 EXPORT_SYMBOL(phy_init_eee);
 
-- 
2.39.0


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

* Re: [PATCH] net: Replace the ternary conditional operator with min()
  2023-05-30  8:45 [PATCH] net: Replace the ternary conditional operator with min() Lu Hongfei
@ 2023-05-30  9:03 ` Heiner Kallweit
  2023-05-30  9:08 ` Russell King (Oracle)
  2023-05-30 13:11 ` Andrew Lunn
  2 siblings, 0 replies; 4+ messages in thread
From: Heiner Kallweit @ 2023-05-30  9:03 UTC (permalink / raw)
  To: Lu Hongfei, Andrew Lunn, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	open list:ETHERNET PHY LIBRARY, open list
  Cc: opensource.kernel

On 30.05.2023 10:45, Lu Hongfei wrote:
> It would be better to replace the traditional ternary conditional
> operator with min()
> 
No. If you say something is better you should explain the benefit.

> Signed-off-by: Lu Hongfei <luhongfei@vivo.com>
> ---
>  drivers/net/phy/phy.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>  mode change 100644 => 100755 drivers/net/phy/phy.c
> 
> diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
> index 0c0df38cd1ab..a8beb4ab8451
> --- a/drivers/net/phy/phy.c
> +++ b/drivers/net/phy/phy.c
> @@ -1002,7 +1002,7 @@ static int phy_poll_aneg_done(struct phy_device *phydev)
>  	if (!ret)
>  		return -ETIMEDOUT;
>  
> -	return ret < 0 ? ret : 0;
> +	return min(ret, 0);

ret < 0 stands for is_err(ret), therefore an arithmetic operator isn't
appropriate here.

>  }
>  
>  int phy_ethtool_ksettings_set(struct phy_device *phydev,
> @@ -1526,7 +1526,7 @@ int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
>  		ret = phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
>  				       MDIO_PCS_CTRL1_CLKSTOP_EN);
>  
> -	return ret < 0 ? ret : 0;
> +	return min(ret, 0);
>  }
>  EXPORT_SYMBOL(phy_init_eee);
>  


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

* Re: [PATCH] net: Replace the ternary conditional operator with min()
  2023-05-30  8:45 [PATCH] net: Replace the ternary conditional operator with min() Lu Hongfei
  2023-05-30  9:03 ` Heiner Kallweit
@ 2023-05-30  9:08 ` Russell King (Oracle)
  2023-05-30 13:11 ` Andrew Lunn
  2 siblings, 0 replies; 4+ messages in thread
From: Russell King (Oracle) @ 2023-05-30  9:08 UTC (permalink / raw)
  To: Lu Hongfei
  Cc: Andrew Lunn, Heiner Kallweit, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, open list:ETHERNET PHY LIBRARY,
	open list, opensource.kernel

On Tue, May 30, 2023 at 04:45:30PM +0800, Lu Hongfei wrote:
> It would be better to replace the traditional ternary conditional
> operator with min()

I don't think this is any "better". It's not really a "let's return the
minimum of two values" even though that is what it ends up functionally
being.

Semantically, it's "Is there an error? Yes, then return the error.
Otherwise return success" where an error in the kernel is defined as a
negative integer and success as generally zero, or sometimes a small
positive integer.

Replacing these with "min()" makes the code _less_ readable.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!

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

* Re: [PATCH] net: Replace the ternary conditional operator with min()
  2023-05-30  8:45 [PATCH] net: Replace the ternary conditional operator with min() Lu Hongfei
  2023-05-30  9:03 ` Heiner Kallweit
  2023-05-30  9:08 ` Russell King (Oracle)
@ 2023-05-30 13:11 ` Andrew Lunn
  2 siblings, 0 replies; 4+ messages in thread
From: Andrew Lunn @ 2023-05-30 13:11 UTC (permalink / raw)
  To: Lu Hongfei
  Cc: Heiner Kallweit, Russell King, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, open list:ETHERNET PHY LIBRARY,
	open list, opensource.kernel

On Tue, May 30, 2023 at 04:45:30PM +0800, Lu Hongfei wrote:
> It would be better to replace the traditional ternary conditional
> operator with min()

Hi Lu

Adding to the comments from Russell and Heiner, if i remember
correctly, the exact same change has been rejected before, for the
same reasons.  When submitting a patch, please do a search first and
see if somebody else has already received a reject.

Did you use a static analyser to find this? Please submit a patch to
the static analyser to stop it reporting code like this. That will
save wasting peoples time having to develop such bad patches, and
reviewers having to reject them.

	Andrew

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

end of thread, other threads:[~2023-05-30 13:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-30  8:45 [PATCH] net: Replace the ternary conditional operator with min() Lu Hongfei
2023-05-30  9:03 ` Heiner Kallweit
2023-05-30  9:08 ` Russell King (Oracle)
2023-05-30 13:11 ` Andrew Lunn

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.