Skip to content

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

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

Merged
merged 4 commits into from
Mar 30, 2023
Merged
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
Prev Previous commit
Next Next commit
Emits suggestions for expressions with parentheses or not separately
mu001999 committed Mar 24, 2023
commit 910a5ad2dfbff6c29092b73e696349eb45eb1aa5
4 changes: 3 additions & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
@@ -197,7 +197,9 @@ lint_drop_glue =
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}`
.suggestion = use an inclusive range instead

lint_range_use_inclusive_range = use an inclusive range instead


lint_overflowing_bin_hex = literal out of range for `{$ty}`
.negative_note = the literal `{$lit}` (decimal `{$dec}`) does not fit into the type `{$ty}`
33 changes: 27 additions & 6 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
@@ -1210,12 +1210,33 @@ impl<'a> DecorateLint<'a, ()> for DropGlue<'_> {
#[diag(lint_range_endpoint_out_of_range)]
pub struct RangeEndpointOutOfRange<'a> {
pub ty: &'a str,
#[suggestion(code = "=", applicability = "machine-applicable")]
pub eq_suggestion: Span,
#[suggestion(code = "{literal}{suffix}", applicability = "machine-applicable")]
pub lit_suggestion: Span,
pub literal: u128,
pub suffix: &'a str,
#[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,
},
}

#[derive(LintDiagnostic)]
28 changes: 20 additions & 8 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@ use crate::{
AtomicOrderingFence, AtomicOrderingLoad, AtomicOrderingStore, ImproperCTypes,
InvalidAtomicOrderingDiag, OnlyCastu8ToChar, OverflowingBinHex, OverflowingBinHexSign,
OverflowingBinHexSub, OverflowingInt, OverflowingIntHelp, OverflowingLiteral,
OverflowingUInt, RangeEndpointOutOfRange, UnusedComparisons, VariantSizeDifferencesDiag,
OverflowingUInt, RangeEndpointOutOfRange, UnusedComparisons, UseInclusiveRange,
VariantSizeDifferencesDiag,
},
};
use crate::{LateContext, LateLintPass, LintContext};
@@ -172,16 +173,27 @@ fn lint_overflowing_range_endpoint<'tcx>(
_ => bug!(),
};

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()),
start,
literal: lit_val - 1,
suffix,
}
} 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,
eq_suggestion: expr.span.shrink_to_lo(),
lit_suggestion: lit_span,
literal: lit_val - 1,
suffix,
},
RangeEndpointOutOfRange { ty, sub: sub_sugg },
);

// We've just emitted a lint, special cased for `(...)..MAX+1` ranges,
5 changes: 2 additions & 3 deletions tests/ui/lint/issue-109529.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
fn main() {
for i in 0..(256 as u8) { //~ ERROR range endpoint is out of range
println!("{}", i);
}
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
}
21 changes: 13 additions & 8 deletions tests/ui/lint/issue-109529.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
error: range endpoint is out of range for `u8`
--> $DIR/issue-109529.rs:2:14
|
LL | for i in 0..(256 as u8) {
| ^^^^^^^^^^^^^^
LL | for _ in 0..256 as u8 {}
| ------^^^^^^
| |
| help: use an inclusive range instead: `0..=255`
|
= note: `#[deny(overflowing_literals)]` on by default
help: use an inclusive range instead

error: range endpoint is out of range for `u8`
--> $DIR/issue-109529.rs:3:14
|
LL | for _ in 0..(256 as u8) {}
| ^^^^^^^^^^^^^^
|
LL | for i in 0..=(256 as u8) {
| +
help: use an inclusive range instead
|
LL | for i in 0..(255 as u8) {
| ~~~
LL | for _ in 0..=(255 as u8) {}
| + ~~~

error: aborting due to previous error
error: aborting due to 2 previous errors