Skip to content

Commit 6034b2f

Browse files
committed
Use independent suggestions
1 parent 8e56c2c commit 6034b2f

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,9 +1210,10 @@ impl<'a> DecorateLint<'a, ()> for DropGlue<'_> {
12101210
#[diag(lint_range_endpoint_out_of_range)]
12111211
pub struct RangeEndpointOutOfRange<'a> {
12121212
pub ty: &'a str,
1213-
#[suggestion(code = "{start}..={literal}{suffix}", applicability = "machine-applicable")]
1214-
pub suggestion: Span,
1215-
pub start: String,
1213+
#[suggestion(code = "=", applicability = "machine-applicable")]
1214+
pub eq_suggestion: Span,
1215+
#[suggestion(code = "{literal}{suffix}", applicability = "machine-applicable")]
1216+
pub lit_suggestion: Span,
12161217
pub literal: u128,
12171218
pub suffix: &'a str,
12181219
}

compiler/rustc_lint/src/types.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ fn lint_overflowing_range_endpoint<'tcx>(
136136
expr: &'tcx hir::Expr<'tcx>,
137137
ty: &str,
138138
) -> bool {
139-
let (expr, cast_ty) = if let Node::Expr(par_expr) = cx.tcx.hir().get(cx.tcx.hir().parent_id(expr.hir_id))
140-
&& let ExprKind::Cast(_, ty) = par_expr.kind {
141-
(par_expr, Some(ty))
139+
// Look past casts to support cases like `0..256 as u8`
140+
let (expr, lit_span) = if let Node::Expr(par_expr) = cx.tcx.hir().get(cx.tcx.hir().parent_id(expr.hir_id))
141+
&& let ExprKind::Cast(_, _) = par_expr.kind {
142+
(par_expr, expr.span)
142143
} else {
143-
(expr, None)
144+
(expr, expr.span)
144145
};
145146

146147
// We only want to handle exclusive (`..`) ranges,
@@ -162,30 +163,24 @@ fn lint_overflowing_range_endpoint<'tcx>(
162163
if !(eps[1].expr.hir_id == expr.hir_id && lit_val - 1 == max) {
163164
return false;
164165
};
165-
let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span) else { return false };
166166

167-
let suffix = if let Some(cast_ty) = cast_ty {
168-
let Ok(ty) = cx.sess().source_map().span_to_snippet(cast_ty.span) else { return false };
169-
format!(" as {}", ty)
170-
} else {
171-
use rustc_ast::{LitIntType, LitKind};
172-
match lit.node {
173-
LitKind::Int(_, LitIntType::Signed(s)) => s.name_str().to_owned(),
174-
LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str().to_owned(),
175-
LitKind::Int(_, LitIntType::Unsuffixed) => "".to_owned(),
176-
_ => bug!(),
177-
}
167+
use rustc_ast::{LitIntType, LitKind};
168+
let suffix = match lit.node {
169+
LitKind::Int(_, LitIntType::Signed(s)) => s.name_str(),
170+
LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str(),
171+
LitKind::Int(_, LitIntType::Unsuffixed) => "",
172+
_ => bug!(),
178173
};
179174

180175
cx.emit_spanned_lint(
181176
OVERFLOWING_LITERALS,
182177
struct_expr.span,
183178
RangeEndpointOutOfRange {
184179
ty,
185-
suggestion: struct_expr.span,
186-
start,
180+
eq_suggestion: expr.span.shrink_to_lo(),
181+
lit_suggestion: lit_span,
187182
literal: lit_val - 1,
188-
suffix: &suffix,
183+
suffix,
189184
},
190185
);
191186

tests/ui/lint/issue-109529.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
2-
for i in 0..256 as u8 { //~ ERROR range endpoint is out of range
2+
for i in 0..(256 as u8) { //~ ERROR range endpoint is out of range
33
println!("{}", i);
44
}
55
}

tests/ui/lint/issue-109529.stderr

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
error: range endpoint is out of range for `u8`
22
--> $DIR/issue-109529.rs:2:14
33
|
4-
LL | for i in 0..256 as u8 {
5-
| ^^^^^^^^^^^^ help: use an inclusive range instead: `0..=255 as u8`
4+
LL | for i in 0..(256 as u8) {
5+
| ^^^^^^^^^^^^^^
66
|
77
= note: `#[deny(overflowing_literals)]` on by default
8+
help: use an inclusive range instead
9+
|
10+
LL | for i in 0..=(256 as u8) {
11+
| +
12+
help: use an inclusive range instead
13+
|
14+
LL | for i in 0..(255 as u8) {
15+
| ~~~
816

917
error: aborting due to previous error
1018

0 commit comments

Comments
 (0)