跳到内容

parenthesize-chained-operators (RUF021) (添加括号到链式操作符)

修复总是可用的。

作用

检查链式操作符,添加括号可以提高代码清晰度。

为什么这不好?

andor 两个操作符链在一起时,and 的优先级总是高于 or,但这可能很难记住(有时会令人惊讶)。 在这种情况下添加括号可以大大提高代码可读性,而不会改变语义或性能。

例如

a, b, c = 1, 0, 2
x = a or b and c

d, e, f = 0, 1, 2
y = d and e or f

建议改为

a, b, c = 1, 0, 2
x = a or (b and c)

d, e, f = 0, 1, 2
y = (d and e) or f