Skip to content

Commit 910a5ad

Browse files
committed
Emits suggestions for expressions with parentheses or not separately
1 parent 6034b2f commit 910a5ad

File tree

5 files changed

+65
-26
lines changed

5 files changed

+65
-26
lines changed

compiler/rustc_lint/messages.ftl

+3-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ lint_drop_glue =
197197
types that do not implement `Drop` can still have drop glue, consider instead using `{$needs_drop}` to detect whether a type is trivially dropped
198198
199199
lint_range_endpoint_out_of_range = range endpoint is out of range for `{$ty}`
200-
.suggestion = use an inclusive range instead
200+
201+
lint_range_use_inclusive_range = use an inclusive range instead
202+
201203
202204
lint_overflowing_bin_hex = literal out of range for `{$ty}`
203205
.negative_note = the literal `{$lit}` (decimal `{$dec}`) does not fit into the type `{$ty}`

compiler/rustc_lint/src/lints.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -1210,12 +1210,33 @@ 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 = "=", applicability = "machine-applicable")]
1214-
pub eq_suggestion: Span,
1215-
#[suggestion(code = "{literal}{suffix}", applicability = "machine-applicable")]
1216-
pub lit_suggestion: Span,
1217-
pub literal: u128,
1218-
pub suffix: &'a str,
1213+
#[subdiagnostic]
1214+
pub sub: UseInclusiveRange<'a>,
1215+
}
1216+
1217+
#[derive(Subdiagnostic)]
1218+
pub enum UseInclusiveRange<'a> {
1219+
#[suggestion(
1220+
lint_range_use_inclusive_range,
1221+
code = "{start}..={literal}{suffix}",
1222+
applicability = "machine-applicable"
1223+
)]
1224+
WithoutParen {
1225+
#[primary_span]
1226+
sugg: Span,
1227+
start: String,
1228+
literal: u128,
1229+
suffix: &'a str,
1230+
},
1231+
#[multipart_suggestion(lint_range_use_inclusive_range, applicability = "machine-applicable")]
1232+
WithParen {
1233+
#[suggestion_part(code = "=")]
1234+
eq_sugg: Span,
1235+
#[suggestion_part(code = "{literal}{suffix}")]
1236+
lit_sugg: Span,
1237+
literal: u128,
1238+
suffix: &'a str,
1239+
},
12191240
}
12201241

12211242
#[derive(LintDiagnostic)]

compiler/rustc_lint/src/types.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::{
44
AtomicOrderingFence, AtomicOrderingLoad, AtomicOrderingStore, ImproperCTypes,
55
InvalidAtomicOrderingDiag, OnlyCastu8ToChar, OverflowingBinHex, OverflowingBinHexSign,
66
OverflowingBinHexSub, OverflowingInt, OverflowingIntHelp, OverflowingLiteral,
7-
OverflowingUInt, RangeEndpointOutOfRange, UnusedComparisons, VariantSizeDifferencesDiag,
7+
OverflowingUInt, RangeEndpointOutOfRange, UnusedComparisons, UseInclusiveRange,
8+
VariantSizeDifferencesDiag,
89
},
910
};
1011
use crate::{LateContext, LateLintPass, LintContext};
@@ -172,16 +173,27 @@ fn lint_overflowing_range_endpoint<'tcx>(
172173
_ => bug!(),
173174
};
174175

176+
let sub_sugg = if expr.span.lo() == lit_span.lo() {
177+
let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span) else { return false };
178+
UseInclusiveRange::WithoutParen {
179+
sugg: struct_expr.span.shrink_to_lo().to(lit_span.shrink_to_hi()),
180+
start,
181+
literal: lit_val - 1,
182+
suffix,
183+
}
184+
} else {
185+
UseInclusiveRange::WithParen {
186+
eq_sugg: expr.span.shrink_to_lo(),
187+
lit_sugg: lit_span,
188+
literal: lit_val - 1,
189+
suffix,
190+
}
191+
};
192+
175193
cx.emit_spanned_lint(
176194
OVERFLOWING_LITERALS,
177195
struct_expr.span,
178-
RangeEndpointOutOfRange {
179-
ty,
180-
eq_suggestion: expr.span.shrink_to_lo(),
181-
lit_suggestion: lit_span,
182-
literal: lit_val - 1,
183-
suffix,
184-
},
196+
RangeEndpointOutOfRange { ty, sub: sub_sugg },
185197
);
186198

187199
// We've just emitted a lint, special cased for `(...)..MAX+1` ranges,

tests/ui/lint/issue-109529.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
fn main() {
2-
for i in 0..(256 as u8) { //~ ERROR range endpoint is out of range
3-
println!("{}", i);
4-
}
2+
for _ in 0..256 as u8 {} //~ ERROR range endpoint is out of range
3+
for _ in 0..(256 as u8) {} //~ ERROR range endpoint is out of range
54
}

tests/ui/lint/issue-109529.stderr

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
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-
| ^^^^^^^^^^^^^^
4+
LL | for _ in 0..256 as u8 {}
5+
| ------^^^^^^
6+
| |
7+
| help: use an inclusive range instead: `0..=255`
68
|
79
= note: `#[deny(overflowing_literals)]` on by default
8-
help: use an inclusive range instead
10+
11+
error: range endpoint is out of range for `u8`
12+
--> $DIR/issue-109529.rs:3:14
13+
|
14+
LL | for _ in 0..(256 as u8) {}
15+
| ^^^^^^^^^^^^^^
916
|
10-
LL | for i in 0..=(256 as u8) {
11-
| +
1217
help: use an inclusive range instead
1318
|
14-
LL | for i in 0..(255 as u8) {
15-
| ~~~
19+
LL | for _ in 0..=(255 as u8) {}
20+
| + ~~~
1621

17-
error: aborting due to previous error
22+
error: aborting due to 2 previous errors
1823

0 commit comments

Comments
 (0)