Rollup merge of #102602 - WaffleLapkin:linty_action, r=estebank
Slightly tweak comments wrt `lint_overflowing_range_endpoint` From the review: https://github.com/rust-lang/rust/pull/101986#discussion_r975610611 It _seemed_ that the lint was not emitted when the `if` check failed, but _actually_ this happens already in a special case and the lint is emitted outside of this function, if this function doesn't. I've cleared up the code/comments a bit, so it's more obvious :) r? ```@estebank```
This commit is contained in:
commit
16c3b64794
1 changed files with 38 additions and 36 deletions
|
@ -116,8 +116,8 @@ impl TypeLimits {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempts to special-case the overflowing literal lint when it occurs as a range endpoint.
|
/// Attempts to special-case the overflowing literal lint when it occurs as a range endpoint (`expr..MAX+1`).
|
||||||
/// Returns `true` iff the lint was overridden.
|
/// Returns `true` iff the lint was emitted.
|
||||||
fn lint_overflowing_range_endpoint<'tcx>(
|
fn lint_overflowing_range_endpoint<'tcx>(
|
||||||
cx: &LateContext<'tcx>,
|
cx: &LateContext<'tcx>,
|
||||||
lit: &hir::Lit,
|
lit: &hir::Lit,
|
||||||
|
@ -140,44 +140,46 @@ fn lint_overflowing_range_endpoint<'tcx>(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut overwritten = false;
|
|
||||||
// We can suggest using an inclusive range
|
// We can suggest using an inclusive range
|
||||||
// (`..=`) instead only if it is the `end` that is
|
// (`..=`) instead only if it is the `end` that is
|
||||||
// overflowing and only by 1.
|
// overflowing and only by 1.
|
||||||
if eps[1].expr.hir_id == expr.hir_id && lit_val - 1 == max
|
if !(eps[1].expr.hir_id == expr.hir_id && lit_val - 1 == max) {
|
||||||
&& let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span)
|
return false;
|
||||||
{
|
};
|
||||||
cx.struct_span_lint(
|
let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span) else { return false };
|
||||||
OVERFLOWING_LITERALS,
|
|
||||||
struct_expr.span,
|
|
||||||
fluent::lint::range_endpoint_out_of_range,
|
|
||||||
|lint| {
|
|
||||||
use ast::{LitIntType, LitKind};
|
|
||||||
|
|
||||||
lint.set_arg("ty", ty);
|
cx.struct_span_lint(
|
||||||
|
OVERFLOWING_LITERALS,
|
||||||
|
struct_expr.span,
|
||||||
|
fluent::lint::range_endpoint_out_of_range,
|
||||||
|
|lint| {
|
||||||
|
use ast::{LitIntType, LitKind};
|
||||||
|
|
||||||
// We need to preserve the literal's suffix,
|
lint.set_arg("ty", ty);
|
||||||
// as it may determine typing information.
|
|
||||||
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 suggestion = format!("{}..={}{}", start, lit_val - 1, suffix);
|
|
||||||
lint.span_suggestion(
|
|
||||||
struct_expr.span,
|
|
||||||
fluent::lint::suggestion,
|
|
||||||
suggestion,
|
|
||||||
Applicability::MachineApplicable,
|
|
||||||
);
|
|
||||||
overwritten = true;
|
|
||||||
|
|
||||||
lint
|
// We need to preserve the literal's suffix,
|
||||||
},
|
// as it may determine typing information.
|
||||||
);
|
let suffix = match lit.node {
|
||||||
}
|
LitKind::Int(_, LitIntType::Signed(s)) => s.name_str(),
|
||||||
overwritten
|
LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str(),
|
||||||
|
LitKind::Int(_, LitIntType::Unsuffixed) => "",
|
||||||
|
_ => bug!(),
|
||||||
|
};
|
||||||
|
let suggestion = format!("{}..={}{}", start, lit_val - 1, suffix);
|
||||||
|
lint.span_suggestion(
|
||||||
|
struct_expr.span,
|
||||||
|
fluent::lint::suggestion,
|
||||||
|
suggestion,
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
|
||||||
|
lint
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// We've just emitted a lint, special cased for `(...)..MAX+1` ranges,
|
||||||
|
// return `true` so the callers don't also emit a lint
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
// For `isize` & `usize`, be conservative with the warnings, so that the
|
// For `isize` & `usize`, be conservative with the warnings, so that the
|
||||||
|
@ -358,7 +360,7 @@ fn lint_int_literal<'tcx>(
|
||||||
}
|
}
|
||||||
|
|
||||||
if lint_overflowing_range_endpoint(cx, lit, v, max, e, t.name_str()) {
|
if lint_overflowing_range_endpoint(cx, lit, v, max, e, t.name_str()) {
|
||||||
// The overflowing literal lint was overridden.
|
// The overflowing literal lint was emited by `lint_overflowing_range_endpoint`.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -427,7 +429,7 @@ fn lint_uint_literal<'tcx>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if lint_overflowing_range_endpoint(cx, lit, lit_val, max, e, t.name_str()) {
|
if lint_overflowing_range_endpoint(cx, lit, lit_val, max, e, t.name_str()) {
|
||||||
// The overflowing literal lint was overridden.
|
// The overflowing literal lint was emited by `lint_overflowing_range_endpoint`.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let Some(repr_str) = get_bin_hex_repr(cx, lit) {
|
if let Some(repr_str) = get_bin_hex_repr(cx, lit) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue