Skip to content

Conversation

@amerikrainian
Copy link

@amerikrainian amerikrainian commented Nov 28, 2025

changelog: [manual_checked_div]: new lint suggesting checked_div instead of manual zero checks before unsigned division

Implements the manual checked div from #12894. I'm relatively new to Rust and a complete newbie at Clippy, so my apologies if I forgot anything. I looked through the linked issue and could not find anything talking about the above lint.

@rustbot rustbot added needs-fcp S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Nov 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 28, 2025

r? @samueltardieu

rustbot has assigned @samueltardieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@github-actions
Copy link

Lintcheck changes for bf07325

Lint Added Removed Changed
clippy::manual_checked_div 3 0 0

This comment will be updated if you push new changes

Copy link
Contributor

@ada4a ada4a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely impressive for a "complete newbie at Clippy"! Left a couple starting comments

View changes since this review

Comment on lines +119 to +122
NonZeroBranch::Else => match r#else.map(|expr| &expr.kind) {
Some(ExprKind::Block(block, _)) => Some(block),
_ => None,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NonZeroBranch::Else => match r#else.map(|expr| &expr.kind) {
Some(ExprKind::Block(block, _)) => Some(block),
_ => None,
},
NonZeroBranch::Else => match r#else?.kind {
ExprKind::Block(block, _) => Some(block),
_ => None,
},

Comment on lines +58 to +60
let Some(block) = branch_block(then, r#else, branch) else {
return;
};
Copy link
Contributor

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

Comment on lines +50 to +52
if expr.span.from_expansion() {
return;
}
Copy link
Contributor

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

Comment on lines +70 to +71
let lhs_snip = Sugg::hir_with_applicability(cx, lhs, "..", &mut applicability);
let rhs_snip = Sugg::hir_with_applicability(cx, rhs, "..", &mut applicability);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For single expressions, the preferred placeholder is "_"

Suggested change
let lhs_snip = Sugg::hir_with_applicability(cx, lhs, "..", &mut applicability);
let rhs_snip = Sugg::hir_with_applicability(cx, rhs, "..", &mut applicability);
let lhs_snip = Sugg::hir_with_applicability(cx, lhs, "_", &mut applicability);
let rhs_snip = Sugg::hir_with_applicability(cx, rhs, "_", &mut applicability);

let Some(block) = branch_block(then, r#else, branch) else {
return;
};
let mut eq = SpanlessEq::new(cx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine to inline

span_lint_and_sugg(
cx,
MANUAL_CHECKED_DIV,
e.span,
Copy link
Contributor

Choose a reason for hiding this comment

The 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 b) is checked for being non-zero, which you could do by passing MultiSpan::new(vec![cond.span, e.span]).

But with that, if there were multiple instances of a / b (however unlikely that is), each firing of the lint would show the said checking place, which would be suboptimal. Because of this, a better solution could be to first collect all the instances of a / b (where each one might have a different a!), then lint on all of them at once (if there are any, of course). Now that I've written all this, it does sound like a bit too much work -- if you want to give it a try, feel free, but otherwise just highlighting both the condition and the division would be a good start.

Comment on lines +30 to +34
// Should NOT trigger (signed integers)
let c = -5i32;
if c != 0 {
let _result = 10 / c;
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

@rustbot
Copy link
Collaborator

rustbot commented Dec 2, 2025

☔ The latest upstream changes (possibly a10cafe) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants