Skip to content

Fix invalid_upcast_comparisons treating non-widening casts as upcasts - #17460

Open
shulaoda wants to merge 1 commit into
rust-lang:masterfrom
shulaoda:07-26-fix_invalid_upcast_comparisons_treating_non-widening_casts_as_upcasts
Open

Fix invalid_upcast_comparisons treating non-widening casts as upcasts#17460
shulaoda wants to merge 1 commit into
rust-lang:masterfrom
shulaoda:07-26-fix_invalid_upcast_comparisons_treating_non-widening_casts_as_upcasts

Conversation

@shulaoda

@shulaoda shulaoda commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #7648

invalid_upcast_comparisons decides a comparison is always true or false by applying the bounds of the pre-cast type to the cast expression, which only holds while the cast preserves the value. numeric_cast_precast_bounds bailed out only when both types had the same size, so a narrowing cast such as u64 as i32, or a sign-losing widening one such as i8 as u64, kept the source bounds. For b: u64, (b as i32) < 0 was reported as always false, but u64::MAX as i32 is -1.

Also println!("{}", 128u16 as i8); -> -128, so (u16 as i8) < 0 is not always false.

The bail-out now keeps only casts to a strictly wider integer type whose signedness can represent the whole source range.

changelog: [invalid_upcast_comparisons]: don't draw conclusions from a cast that is not a widening cast

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Jul 26, 2026
@rustbot

rustbot commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request. A reviewer will take a look after it receives 2 community reviews.

In the meantime, we would highly appreciate if you could try to review any of PRs waiting on community reviews.

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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. 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.

invalid_upcast_comparisons: false positive on implicit wrapping

2 participants