Re: [PATCH v2] staging: iio: frequency: ad9832: use guard(mutex)

From: Andy Shevchenko

Date: Mon Oct 19 2026 - 05:34:19 EST


On Sun, Oct 18, 2026 at 02:01:07PM +0300, Melih Emik wrote:
> In ad9832_write(), manual mutex handling via mutex_lock() and
> mutex_unlock() necessitates error-exit trampolines (goto error_ret),
> intermediate variables, and deferred returns. This pattern is prone
> to unlock-omission bugs during refactoring and clutters the control
> flow.
>
> Modernize the locking by using guard(mutex) from <linux/cleanup.h>.
> Scope-based resource management automatically releases the lock upon
> exiting the lexical scope, which enables immediate error propagation
> at call sites, removes the error_ret label, and simplifies the control
> flow across all switch cases.

...

> case AD9832_FREQ_SYM:
> if (val == 1 || val == 0) {
> st->ctrl_fp &= ~AD9832_FREQ;
> st->ctrl_fp |= FIELD_PREP(AD9832_FREQ, val ? 1 : 0);
> } else {
> - ret = -EINVAL;
> - break;
> + return -EINVAL;
> }

'else' is now redundant:

if (val != 1 && val != 0)
return -EINVAL;

> st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_FPSELECT) |
> st->ctrl_fp);
> ret = spi_sync(st->spi, &st->msg);
> + if (ret)
> + return ret;
> +
> break;

--
With Best Regards,
Andy Shevchenko