Change:
<!-- Thank you for filing a bug report! Please provide a short summary of the bug, along with any information you feel relevant to replicating the bug. --I tried this code: I expected to see this happen: printing all numbers from 0 to 255 (inclusive) Instead, this happened: : <!-- TRIAGEBOTSTART --<!-- TRIAGEBOTASSIGNSTART --<!-- TRIAGEBOTASSIGNDATASTARTTRIAGEBOTASSIGNDATAEND --<!-- TRIAGEBOTASSIGNEND --<!-- TRIAGEBOTEND --
The error is correct, 256 is indeed out of range for u8. You need to use .
You can run that code if you want, by allowing the lint: if you do you'll see that it will print nothing because the overflows and becomes , so it's which is empty. Examples like this where allowing the literal overflow would do something you don't want are exactly why this lint is deny-by-default
I've renamed this issue because the lint is correct. If there's anything to be done here, it would be to add a range-specific suggestion for this situation to help guide towards the correct code.
claim

Discussion:
types that do not implement `Drop` can still have drop glue, consider instead using `{$needs_drop}` to detect whether a type is trivially dropped lint_range_endpoint_out_of_range = range endpoint is out of range for `{$ty}` <del> .suggestion = use an inclusive range instead </del> <ins> lint_range_use_inclusive_range = use an inclusive range instead </ins> lint_overflowing_bin_hex = literal out of range for `{$ty}` .negative_note = the literal `{$lit}` (decimal `{$dec}`) does not fit into the type `{$ty}`

#[diag(lint_range_endpoint_out_of_range)] pub struct RangeEndpointOutOfRange<'a> { pub ty: &'a str, <del> #[suggestion(code = "{start}..={literal}{suffix}", applicability = "machine-applicable")] pub suggestion: Span, pub start: String, pub literal: u128, pub suffix: &'a str, </del> <ins> #[subdiagnostic] pub sub: UseInclusiveRange<'a>, } #[derive(Subdiagnostic)] pub enum UseInclusiveRange<'a> { #[suggestion( lint_range_use_inclusive_range, code = "{start}..={literal}{suffix}", applicability = "machine-applicable" )] WithoutParen { #[primary_span] sugg: Span, start: String, literal: u128, suffix: &'a str, }, #[multipart_suggestion(lint_range_use_inclusive_range, applicability = "machine-applicable")] WithParen { #[suggestion_part(code = "=")] eq_sugg: Span, #[suggestion_part(code = "{literal}{suffix}")] lit_sugg: Span, literal: u128, suffix: &'a str, }, </ins> } #[derive(LintDiagnostic)]

AtomicOrderingFence, AtomicOrderingLoad, AtomicOrderingStore, ImproperCTypes, InvalidAtomicOrderingDiag, OnlyCastu8ToChar, OverflowingBinHex, OverflowingBinHexSign, OverflowingBinHexSub, OverflowingInt, OverflowingIntHelp, OverflowingLiteral, <del> OverflowingUInt, RangeEndpointOutOfRange, UnusedComparisons, VariantSizeDifferencesDiag, </del> <ins> OverflowingUInt, RangeEndpointOutOfRange, UnusedComparisons, UseInclusiveRange, VariantSizeDifferencesDiag, </ins> }, }; use crate::{LateContext, LateLintPass, LintContext};

expr: &'tcx hir::Expr<'tcx>, ty: &str, ) -> bool { <ins> // Look past casts to support cases like `0..256 as u8` let (expr, lit_span) = if let Node::Expr(par_expr) = cx.tcx.hir().get(cx.tcx.hir().parent_id(expr.hir_id)) && let ExprKind::Cast(_, _) = par_expr.kind { (par_expr, expr.span) } else { (expr, expr.span) }; </ins> // We only want to handle exclusive (`..`) ranges, // which are represented as `ExprKind::Struct`. let par_id = cx.tcx.hir().parent_id(expr.hir_id);

if !(eps[1].expr.hir_id == expr.hir_id && lit_val - 1 == max) { return false; }; <del> let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span) else { return false }; </del> use rustc_ast::{LitIntType, LitKind}; let suffix = match lit.node {

LitKind::Int(_, LitIntType::Unsuffixed) => "", _ => bug!(), }; <del> cx.emit_spanned_lint( OVERFLOWING_LITERALS, struct_expr.span, RangeEndpointOutOfRange { ty, suggestion: struct_expr.span, </del> <ins> let sub_sugg = if expr.span.lo() == lit_span.lo() { let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span) else { return false }; UseInclusiveRange::WithoutParen { sugg: struct_expr.span.shrink_to_lo().to(lit_span.shrink_to_hi()), </ins> start, literal: lit_val - 1, suffix, <del> }, </del> <ins> } } else { UseInclusiveRange::WithParen { eq_sugg: expr.span.shrink_to_lo(), lit_sugg: lit_span, literal: lit_val - 1, suffix, } }; cx.emit_spanned_lint( OVERFLOWING_LITERALS, struct_expr.span, RangeEndpointOutOfRange { ty, sub: sub_sugg }, </ins> ); // We've just emitted a lint, special cased for `(...)..MAX+1` ranges,

<ins> // run-rustfix fn main() { for _ in 0..=255 as u8 {} //~ ERROR range endpoint is out of range for _ in 0..=(255 as u8) {} //~ ERROR range endpoint is out of range } </ins>

<ins> // run-rustfix fn main() { for _ in 0..256 as u8 {} //~ ERROR range endpoint is out of range for _ in 0..(256 as u8) {} //~ ERROR range endpoint is out of range } </ins>

<ins> error: range endpoint is out of range for `u8` --> $DIR/issue-109529.rs:4:14 | LL |     for _ in 0..256 as u8 {} |              ------^^^^^^ |              | |              help: use an inclusive range instead: `0..=255` | = note: `#[deny(overflowing_literals)]` on by default error: range endpoint is out of range for `u8` --> $DIR/issue-109529.rs:5:14 | LL |     for _ in 0..(256 as u8) {} |              ^^^^^^^^^^^^^^ | help: use an inclusive range instead | LL |     for _ in 0..=(255 as u8) {} |                 + ~~~ error: aborting due to 2 previous errors </ins>