Emits suggestions for expressions with parentheses or not separately
This commit is contained in:
parent
6034b2fcb8
commit
910a5ad2df
5 changed files with 66 additions and 27 deletions
|
@ -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)]
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue