-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add manual_checked_div lint #16149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add manual_checked_div lint #16149
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,132 @@ | ||||||||||||||||||
| use clippy_utils::diagnostics::span_lint_and_sugg; | ||||||||||||||||||
| use clippy_utils::sugg::Sugg; | ||||||||||||||||||
| use clippy_utils::visitors::{Descend, for_each_expr_without_closures}; | ||||||||||||||||||
| use clippy_utils::{SpanlessEq, is_integer_literal}; | ||||||||||||||||||
| use rustc_errors::Applicability; | ||||||||||||||||||
| use rustc_hir::{BinOpKind, Block, Expr, ExprKind}; | ||||||||||||||||||
| use rustc_lint::{LateContext, LateLintPass}; | ||||||||||||||||||
| use rustc_middle::ty; | ||||||||||||||||||
| use rustc_session::declare_lint_pass; | ||||||||||||||||||
| use std::ops::ControlFlow; | ||||||||||||||||||
|
|
||||||||||||||||||
| declare_clippy_lint! { | ||||||||||||||||||
| /// ### What it does | ||||||||||||||||||
| /// Detects manual zero checks before dividing unsigned integers, such as `if x != 0 { y / x }`. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// ### Why is this bad? | ||||||||||||||||||
| /// `checked_div` already handles the zero case and makes the intent clearer while avoiding a | ||||||||||||||||||
| /// panic from a manual division. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// ### Example | ||||||||||||||||||
| /// ```no_run | ||||||||||||||||||
| /// # let (a, b) = (10u32, 5u32); | ||||||||||||||||||
| /// if b != 0 { | ||||||||||||||||||
| /// let result = a / b; | ||||||||||||||||||
| /// println!("{result}"); | ||||||||||||||||||
| /// } | ||||||||||||||||||
| /// ``` | ||||||||||||||||||
| /// Use instead: | ||||||||||||||||||
| /// ```no_run | ||||||||||||||||||
| /// # let (a, b) = (10u32, 5u32); | ||||||||||||||||||
| /// if let Some(result) = a.checked_div(b) { | ||||||||||||||||||
| /// println!("{result}"); | ||||||||||||||||||
| /// } | ||||||||||||||||||
| /// ``` | ||||||||||||||||||
| #[clippy::version = "1.93.0"] | ||||||||||||||||||
| pub MANUAL_CHECKED_DIV, | ||||||||||||||||||
| nursery, | ||||||||||||||||||
| "manual zero checks before dividing unsigned integers" | ||||||||||||||||||
| } | ||||||||||||||||||
| declare_lint_pass!(ManualCheckedDiv => [MANUAL_CHECKED_DIV]); | ||||||||||||||||||
|
|
||||||||||||||||||
| #[derive(Copy, Clone)] | ||||||||||||||||||
| enum NonZeroBranch { | ||||||||||||||||||
| Then, | ||||||||||||||||||
| Else, | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| impl LateLintPass<'_> for ManualCheckedDiv { | ||||||||||||||||||
| fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||||||||||||||||||
| if expr.span.from_expansion() { | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if let ExprKind::If(cond, then, r#else) = expr.kind | ||||||||||||||||||
| && let Some((divisor, branch)) = divisor_from_condition(cond) | ||||||||||||||||||
| && is_unsigned(cx, divisor) | ||||||||||||||||||
| { | ||||||||||||||||||
| let Some(block) = branch_block(then, r#else, branch) else { | ||||||||||||||||||
| return; | ||||||||||||||||||
| }; | ||||||||||||||||||
|
Comment on lines
+58
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be moved into the let-chain as well |
||||||||||||||||||
| let mut eq = SpanlessEq::new(cx); | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine to inline |
||||||||||||||||||
|
|
||||||||||||||||||
| for_each_expr_without_closures(block, |e| { | ||||||||||||||||||
| if let ExprKind::Binary(binop, lhs, rhs) = e.kind | ||||||||||||||||||
| && binop.node == BinOpKind::Div | ||||||||||||||||||
| && eq.eq_expr(rhs, divisor) | ||||||||||||||||||
| && is_unsigned(cx, lhs) | ||||||||||||||||||
| { | ||||||||||||||||||
| let mut applicability = Applicability::MaybeIncorrect; | ||||||||||||||||||
| let lhs_snip = Sugg::hir_with_applicability(cx, lhs, "..", &mut applicability); | ||||||||||||||||||
| let rhs_snip = Sugg::hir_with_applicability(cx, rhs, "..", &mut applicability); | ||||||||||||||||||
|
Comment on lines
+70
to
+71
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For single expressions, the preferred placeholder is
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| span_lint_and_sugg( | ||||||||||||||||||
| cx, | ||||||||||||||||||
| MANUAL_CHECKED_DIV, | ||||||||||||||||||
| e.span, | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be nice to also highlight the span where the variable (let's call it But with that, if there were multiple instances of |
||||||||||||||||||
| "manual checked division", | ||||||||||||||||||
| "consider using `checked_div`", | ||||||||||||||||||
| format!("{}.checked_div({})", lhs_snip.maybe_paren(), rhs_snip), | ||||||||||||||||||
| applicability, | ||||||||||||||||||
| ); | ||||||||||||||||||
|
|
||||||||||||||||||
| ControlFlow::<(), _>::Continue(Descend::No) | ||||||||||||||||||
| } else { | ||||||||||||||||||
| ControlFlow::<(), _>::Continue(Descend::Yes) | ||||||||||||||||||
| } | ||||||||||||||||||
| }); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| fn divisor_from_condition<'tcx>(cond: &'tcx Expr<'tcx>) -> Option<(&'tcx Expr<'tcx>, NonZeroBranch)> { | ||||||||||||||||||
| let ExprKind::Binary(binop, lhs, rhs) = cond.kind else { | ||||||||||||||||||
| return None; | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| match binop.node { | ||||||||||||||||||
| BinOpKind::Ne | BinOpKind::Lt if is_zero(lhs) => Some((rhs, NonZeroBranch::Then)), | ||||||||||||||||||
| BinOpKind::Ne | BinOpKind::Gt if is_zero(rhs) => Some((lhs, NonZeroBranch::Then)), | ||||||||||||||||||
| BinOpKind::Eq if is_zero(lhs) => Some((rhs, NonZeroBranch::Else)), | ||||||||||||||||||
| BinOpKind::Eq if is_zero(rhs) => Some((lhs, NonZeroBranch::Else)), | ||||||||||||||||||
| _ => None, | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| fn branch_block<'tcx>( | ||||||||||||||||||
| then: &'tcx Expr<'tcx>, | ||||||||||||||||||
| r#else: Option<&'tcx Expr<'tcx>>, | ||||||||||||||||||
| branch: NonZeroBranch, | ||||||||||||||||||
| ) -> Option<&'tcx Block<'tcx>> { | ||||||||||||||||||
| match branch { | ||||||||||||||||||
| NonZeroBranch::Then => { | ||||||||||||||||||
| if let ExprKind::Block(block, _) = then.kind { | ||||||||||||||||||
| Some(block) | ||||||||||||||||||
| } else { | ||||||||||||||||||
| None | ||||||||||||||||||
| } | ||||||||||||||||||
| }, | ||||||||||||||||||
| NonZeroBranch::Else => match r#else.map(|expr| &expr.kind) { | ||||||||||||||||||
| Some(ExprKind::Block(block, _)) => Some(block), | ||||||||||||||||||
| _ => None, | ||||||||||||||||||
| }, | ||||||||||||||||||
|
Comment on lines
+119
to
+122
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| fn is_zero(expr: &Expr<'_>) -> bool { | ||||||||||||||||||
| is_integer_literal(expr, 0) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| fn is_unsigned(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { | ||||||||||||||||||
| matches!(cx.typeck_results().expr_ty(expr).peel_refs().kind(), ty::Uint(_)) | ||||||||||||||||||
| } | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #![warn(clippy::manual_checked_div)] | ||
|
|
||
| fn main() { | ||
| let a = 10u32; | ||
| let b = 5u32; | ||
|
|
||
| // Should trigger lint | ||
| if b != 0 { | ||
| let _result = a.checked_div(b); | ||
| //~^ manual_checked_div | ||
| } | ||
|
|
||
| if b > 0 { | ||
| let _result = a.checked_div(b); | ||
| //~^ manual_checked_div | ||
| } | ||
|
|
||
| if b == 0 { | ||
| println!("zero"); | ||
| } else { | ||
| let _result = a.checked_div(b); | ||
| //~^ manual_checked_div | ||
| } | ||
|
|
||
| // Should NOT trigger (already using checked_div) | ||
| if let Some(result) = b.checked_div(a) { | ||
| println!("{result}"); | ||
| } | ||
|
|
||
| // Should NOT trigger (signed integers) | ||
| let c = -5i32; | ||
| if c != 0 { | ||
| let _result = 10 / c; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #![warn(clippy::manual_checked_div)] | ||
|
|
||
| fn main() { | ||
| let a = 10u32; | ||
| let b = 5u32; | ||
|
|
||
| // Should trigger lint | ||
| if b != 0 { | ||
| let _result = a / b; | ||
| //~^ manual_checked_div | ||
| } | ||
|
|
||
| if b > 0 { | ||
| let _result = a / b; | ||
| //~^ manual_checked_div | ||
| } | ||
|
|
||
| if b == 0 { | ||
| println!("zero"); | ||
| } else { | ||
| let _result = a / b; | ||
| //~^ manual_checked_div | ||
| } | ||
|
|
||
| // Should NOT trigger (already using checked_div) | ||
| if let Some(result) = b.checked_div(a) { | ||
| println!("{result}"); | ||
| } | ||
|
|
||
| // Should NOT trigger (signed integers) | ||
| let c = -5i32; | ||
| if c != 0 { | ||
| let _result = 10 / c; | ||
| } | ||
|
Comment on lines
+30
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, why exactly do we not lint here?... I see that the original issue only talks about unsigned integers, but I think signed ones should be fine as well? |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| error: manual checked division | ||
| --> tests/ui/manual_checked_div.rs:9:23 | ||
| | | ||
| LL | let _result = a / b; | ||
| | ^^^^^ help: consider using `checked_div`: `a.checked_div(b)` | ||
| | | ||
| = note: `-D clippy::manual-checked-div` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(clippy::manual_checked_div)]` | ||
|
|
||
| error: manual checked division | ||
| --> tests/ui/manual_checked_div.rs:14:23 | ||
| | | ||
| LL | let _result = a / b; | ||
| | ^^^^^ help: consider using `checked_div`: `a.checked_div(b)` | ||
|
|
||
| error: manual checked division | ||
| --> tests/ui/manual_checked_div.rs:21:23 | ||
| | | ||
| LL | let _result = a / b; | ||
| | ^^^^^ help: consider using `checked_div`: `a.checked_div(b)` | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be moved into the let-chain as well