Use operator specific messaging in division by zero diagnostics (#13588)

Requested at
https://github.com/astral-sh/ruff/pull/13576#discussion_r1782530971
This commit is contained in:
Zanie Blue 2024-10-01 08:58:38 -05:00 committed by GitHub
parent 2a36b47f13
commit cfd5d63917
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -504,8 +504,8 @@ impl<'db> TypeInferenceBuilder<'db> {
/// Raise a diagnostic if the given type cannot be divided by zero. /// Raise a diagnostic if the given type cannot be divided by zero.
/// ///
/// Expects the type of the left side of the binary expression. /// Expects the resolved type of the left side of the binary expression.
fn check_division_by_zero(&mut self, node: AnyNodeRef, left: Type<'db>) { fn check_division_by_zero(&mut self, expr: &ast::ExprBinOp, left: Type<'db>) {
match left { match left {
Type::IntLiteral(_) => {} Type::IntLiteral(_) => {}
Type::Instance(cls) Type::Instance(cls)
@ -514,12 +514,19 @@ impl<'db> TypeInferenceBuilder<'db> {
_ => return, _ => return,
}; };
let (op, by_zero) = match expr.op {
ast::Operator::Div => ("divide", "by zero."),
ast::Operator::FloorDiv => ("floor divide", "by zero."),
ast::Operator::Mod => ("reduce", "modulo zero."),
_ => return,
};
self.add_diagnostic( self.add_diagnostic(
node, expr.into(),
"division-by-zero", "division-by-zero",
format_args!( format_args!(
"Cannot divide object of type '{}' by zero.", "Cannot {op} object of type '{}' {by_zero}",
left.display(self.db), left.display(self.db)
), ),
); );
} }
@ -2308,7 +2315,7 @@ impl<'db> TypeInferenceBuilder<'db> {
Type::IntLiteral(0), Type::IntLiteral(0),
) )
) { ) {
self.check_division_by_zero(binary.into(), left_ty); self.check_division_by_zero(binary, left_ty);
} }
match (left_ty, right_ty, op) { match (left_ty, right_ty, op) {
@ -4216,8 +4223,8 @@ mod tests {
"src/a.py", "src/a.py",
&[ &[
"Cannot divide object of type 'Literal[1]' by zero.", "Cannot divide object of type 'Literal[1]' by zero.",
"Cannot divide object of type 'Literal[2]' by zero.", "Cannot floor divide object of type 'Literal[2]' by zero.",
"Cannot divide object of type 'Literal[3]' by zero.", "Cannot reduce object of type 'Literal[3]' modulo zero.",
"Cannot divide object of type 'int' by zero.", "Cannot divide object of type 'int' by zero.",
"Cannot divide object of type 'float' by zero.", "Cannot divide object of type 'float' by zero.",
], ],