Fallback to string formatting if source is not available

This commit is contained in:
clubby789 2024-08-01 20:29:39 +00:00
parent c0e32983f5
commit 3cd445a5b6

View File

@ -462,8 +462,11 @@ fn lint_int_literal<'tcx>(
} }
let span = if negative { type_limits.negated_expr_span.unwrap() } else { e.span }; let span = if negative { type_limits.negated_expr_span.unwrap() } else { e.span };
let lit = let lit = cx
cx.sess().source_map().span_to_snippet(span).expect("must get snippet from literal"); .sess()
.source_map()
.span_to_snippet(span)
.unwrap_or_else(|_| if negative { format!("-{v}") } else { v.to_string() });
let help = get_type_suggestion(cx.typeck_results().node_type(e.hir_id), v, negative) let help = get_type_suggestion(cx.typeck_results().node_type(e.hir_id), v, negative)
.map(|suggestion_ty| OverflowingIntHelp { suggestion_ty }); .map(|suggestion_ty| OverflowingIntHelp { suggestion_ty });
@ -489,6 +492,7 @@ fn lint_uint_literal<'tcx>(
ast::LitKind::Int(v, _) => v.get(), ast::LitKind::Int(v, _) => v.get(),
_ => bug!(), _ => bug!(),
}; };
if lit_val < min || lit_val > max { if lit_val < min || lit_val > max {
if let Node::Expr(par_e) = cx.tcx.parent_hir_node(e.hir_id) { if let Node::Expr(par_e) = cx.tcx.parent_hir_node(e.hir_id) {
match par_e.kind { match par_e.kind {
@ -530,7 +534,7 @@ fn lint_uint_literal<'tcx>(
.sess() .sess()
.source_map() .source_map()
.span_to_snippet(lit.span) .span_to_snippet(lit.span)
.expect("must get snippet from literal"), .unwrap_or_else(|_| lit_val.to_string()),
min, min,
max, max,
}, },
@ -555,14 +559,14 @@ fn lint_literal<'tcx>(
} }
ty::Uint(t) => lint_uint_literal(cx, e, lit, t), ty::Uint(t) => lint_uint_literal(cx, e, lit, t),
ty::Float(t) => { ty::Float(t) => {
let is_infinite = match lit.node { let (is_infinite, sym) = match lit.node {
ast::LitKind::Float(v, _) => match t { ast::LitKind::Float(v, _) => match t {
// FIXME(f16_f128): add this check once `is_infinite` is reliable (ABI // FIXME(f16_f128): add this check once `is_infinite` is reliable (ABI
// issues resolved). // issues resolved).
ty::FloatTy::F16 => Ok(false), ty::FloatTy::F16 => (Ok(false), v),
ty::FloatTy::F32 => v.as_str().parse().map(f32::is_infinite), ty::FloatTy::F32 => (v.as_str().parse().map(f32::is_infinite), v),
ty::FloatTy::F64 => v.as_str().parse().map(f64::is_infinite), ty::FloatTy::F64 => (v.as_str().parse().map(f64::is_infinite), v),
ty::FloatTy::F128 => Ok(false), ty::FloatTy::F128 => (Ok(false), v),
}, },
_ => bug!(), _ => bug!(),
}; };
@ -576,7 +580,7 @@ fn lint_literal<'tcx>(
.sess() .sess()
.source_map() .source_map()
.span_to_snippet(lit.span) .span_to_snippet(lit.span)
.expect("must get snippet from literal"), .unwrap_or_else(|_| sym.to_string()),
}, },
); );
} }