Re: [PATCH] staging: pi433: replace simple switch statements

From: Joe Perches
Date: Sun Jun 24 2018 - 11:26:43 EST


On Sun, 2018-06-24 at 11:42 +0200, Valentin Vidic wrote:
> Use const array to map switch cases to resulting values.

I suggest you make the const arrays the same type
as the argument in the function being called.

> diff --git a/drivers/staging/pi433/rf69.c b/drivers/staging/pi433/rf69.c
[]
> @@ -111,27 +111,22 @@ static inline int rf69_read_mod_write(struct spi_device *spi, u8 reg,
>
> int rf69_set_mode(struct spi_device *spi, enum mode mode)
> {
> - switch (mode) {
> - case transmit:
> - return rf69_read_mod_write(spi, REG_OPMODE, MASK_OPMODE_MODE,
> - OPMODE_MODE_TRANSMIT);
[]
> + static const int mode_map[] = {
> + [transmit] = OPMODE_MODE_TRANSMIT,
> + [receive] = OPMODE_MODE_RECEIVE,
> + [synthesizer] = OPMODE_MODE_SYNTHESIZER,
> + [standby] = OPMODE_MODE_STANDBY,
> + [mode_sleep] = OPMODE_MODE_SLEEP,
> + };
[]
> + return rf69_read_mod_write(spi, REG_OPMODE, MASK_OPMODE_MODE,
> + mode_map[mode]);

drivers/staging/pi433/rf69.c:static inline int rf69_read_mod_write(struct spi_device *spi, u8 reg,
drivers/staging/pi433/rf69.c- u8 mask, u8 value)

so u8 here.

etc...