Skip to content

fix(non_zero_suggestions): don't lint signed integer div/rem as NonZero#17385

Open
Lfan-ke wants to merge 1 commit into
rust-lang:masterfrom
Lfan-ke:fix/non-zero-suggestions-signed-div-rem
Open

fix(non_zero_suggestions): don't lint signed integer div/rem as NonZero#17385
Lfan-ke wants to merge 1 commit into
rust-lang:masterfrom
Lfan-ke:fix/non-zero-suggestions-signed-div-rem

Conversation

@Lfan-ke

@Lfan-ke Lfan-ke commented Jul 9, 2026

Copy link
Copy Markdown

Summary

Fixes #17382.

Div<NonZero<T>> and Rem<NonZero<T>> are only implemented for unsigned integer types in std. The lint was suggesting NonZeroI*::from(x) as the RHS of / or % when the divisor was derived from a signed NonZero type, which produces code that fails to compile:

let s: i64 = 10;
let t = NonZeroI32::new(3).unwrap();

// Before fix: lint fires and auto-fixes to:
let quotient = s / NonZeroI64::from(t); // error: i64 / NonZeroI64 not implemented

Root Cause

In check_expr, the Div | Rem branch called check_non_zero_conversion unconditionally. The get_target_non_zero_type function returned NonZeroI* for signed types too, but the corresponding arithmetic impls do not exist in std.

Fix

Guard the div/rem path: only call check_non_zero_conversion when the RHS type is ty::Uint(_). Signed types still benefit from the lint in non-div/rem contexts (e.g., plain let x: i64 = i64::from(nz.get())NonZeroI64::from(nz)).

Test Plan

  • Added negative test cases for i64 / i64::from(nz.get()) and i64 % i64::from(nz.get()) to tests/ui/non_zero_suggestions.rs - these must NOT trigger the lint.
  • Existing positive test cases for unsigned types remain unchanged.

changelog: [non_zero_suggestions] Fix false positive when suggesting NonZero for signed integer div/rem

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 9, 2026
@rustbot

rustbot commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @llogiq (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, llogiq, samueltardieu

@rustbot

This comment has been minimized.

Div<NonZero<T>> and Rem<NonZero<T>> are only implemented for unsigned
integer types in std. Suggesting NonZeroI* as the RHS of / or % produces
code that fails to compile because those trait impls do not exist for
signed types.

Guard the div/rem path to only fire when the RHS type is ty::Uint.

Signed-off-by: 林晨 (Leo Cheng) <leo-cheng@vip.qq.com>
@Lfan-ke Lfan-ke force-pushed the fix/non-zero-suggestions-signed-div-rem branch from a814a0d to c1fdaa2 Compare July 9, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

non_zero_suggestions suggestion on signed / and % auto-fixes to code that will not compile

3 participants