Skip to content
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

Suggest ..= when someone tries to create an overflowing range #109554

Merged
merged 4 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ fn lint_overflowing_range_endpoint<'tcx>(
expr: &'tcx hir::Expr<'tcx>,
ty: &str,
) -> bool {
let (expr, cast_ty) = if let Node::Expr(par_expr) = cx.tcx.hir().get(cx.tcx.hir().parent_id(expr.hir_id))
mu001999 marked this conversation as resolved.
Show resolved Hide resolved
&& let ExprKind::Cast(_, ty) = par_expr.kind {
(par_expr, Some(ty))
} else {
(expr, None)
};

// We only want to handle exclusive (`..`) ranges,
// which are represented as `ExprKind::Struct`.
let par_id = cx.tcx.hir().parent_id(expr.hir_id);
Expand All @@ -157,13 +164,19 @@ fn lint_overflowing_range_endpoint<'tcx>(
};
let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span) else { return false };

use rustc_ast::{LitIntType, LitKind};
let suffix = match lit.node {
LitKind::Int(_, LitIntType::Signed(s)) => s.name_str(),
LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str(),
LitKind::Int(_, LitIntType::Unsuffixed) => "",
_ => bug!(),
let suffix = if let Some(cast_ty) = cast_ty {
let Ok(ty) = cx.sess().source_map().span_to_snippet(cast_ty.span) else { return false };
format!(" as {}", ty)
WaffleLapkin marked this conversation as resolved.
Show resolved Hide resolved
} else {
use rustc_ast::{LitIntType, LitKind};
match lit.node {
LitKind::Int(_, LitIntType::Signed(s)) => s.name_str().to_owned(),
LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str().to_owned(),
LitKind::Int(_, LitIntType::Unsuffixed) => "".to_owned(),
_ => bug!(),
}
};

cx.emit_spanned_lint(
OVERFLOWING_LITERALS,
struct_expr.span,
Expand All @@ -172,7 +185,7 @@ fn lint_overflowing_range_endpoint<'tcx>(
suggestion: struct_expr.span,
start,
literal: lit_val - 1,
suffix,
suffix: &suffix,
},
);

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/lint/issue-109529.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
mu001999 marked this conversation as resolved.
Show resolved Hide resolved
for i in 0..256 as u8 { //~ ERROR range endpoint is out of range
println!("{}", i);
}
}
10 changes: 10 additions & 0 deletions tests/ui/lint/issue-109529.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: range endpoint is out of range for `u8`
--> $DIR/issue-109529.rs:2:14
|
LL | for i in 0..256 as u8 {
| ^^^^^^^^^^^^ help: use an inclusive range instead: `0..=255 as u8`
|
= note: `#[deny(overflowing_literals)]` on by default

error: aborting due to previous error