跳到内容

不必要的 int 类型转换 (RUF046)

修复总是可用的。

作用

检查对已经是整数的值进行的 int 转换。

为什么这不好?

这种转换是不必要的。

已知问题

当值重写 __round____ceil____floor____trunc__ 运算符,以致它们不返回整数时,此规则可能会对 roundmath.ceilmath.floormath.trunc 调用产生误报。

示例

int(len([]))
int(round(foo, None))

建议改为

len([])
round(foo)

修复安全性

roundmath.ceilmath.floormath.truncate 的修复是不安全的,因为移除 int 转换会改变重写 __round____ceil____floor____trunc__ 特殊方法的值的语义,使得它们不返回整数。