1use hir::{ExprKind, Node};
2use rustc_abi::{Integer, Size};
3use rustc_apfloat::Float;
4use rustc_apfloat::ieee::{DoubleS, HalfS, IeeeFloat, QuadS, Semantics, SingleS};
5use rustc_ast as ast;
6use rustc_hir as hir;
7use rustc_hir::{HirId, attrs};
8use rustc_middle::ty::Ty;
9use rustc_middle::ty::layout::IntegerExt;
10use rustc_middle::{bug, ty};
11use rustc_span::{Span, Symbol};
12
13use crate::LateContext;
14use crate::context::LintContext;
15use crate::lints::{
16 OnlyCastu8ToChar, OverflowingBinHex, OverflowingBinHexSign, OverflowingBinHexSignBitSub,
17 OverflowingBinHexSub, OverflowingInt, OverflowingIntHelp, OverflowingLiteral, OverflowingUInt,
18 RangeEndpointOutOfRange, SurrogateCharCast, TooLargeCharCast, UseInclusiveRange,
19};
20use crate::types::{OVERFLOWING_LITERALS, TypeLimits};
21
22fn lint_overflowing_range_endpoint<'tcx>(
25 cx: &LateContext<'tcx>,
26 lit: &hir::Lit,
27 lit_val: u128,
28 max: u128,
29 hir_id: HirId,
30 lit_span: Span,
31 ty: &str,
32) -> bool {
33 let (hir_id, span) = if let Node::Expr(par_expr) = cx.tcx.parent_hir_node(hir_id)
35 && let ExprKind::Cast(_, _) = par_expr.kind
36 {
37 (par_expr.hir_id, par_expr.span)
38 } else {
39 (hir_id, lit_span)
40 };
41
42 let Node::ExprField(field) = cx.tcx.parent_hir_node(hir_id) else {
45 return false;
46 };
47 let Node::Expr(struct_expr) = cx.tcx.parent_hir_node(field.hir_id) else {
48 return false;
49 };
50 let Some(range_span) = struct_expr.range_span() else {
51 return false;
52 };
53 let ExprKind::Struct(_, [start, end], _) = &struct_expr.kind else {
54 return false;
55 };
56
57 if !(end.expr.hir_id == hir_id && lit_val - 1 == max) {
61 return false;
62 };
63
64 use rustc_ast::{LitIntType, LitKind};
65 let suffix = match lit.node {
66 LitKind::Int(_, LitIntType::Signed(s)) => s.name_str(),
67 LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str(),
68 LitKind::Int(_, LitIntType::Unsuffixed) => "",
69 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
70 };
71
72 let sub_sugg = if span.lo() == lit_span.lo() {
73 let Ok(start) = cx.sess().source_map().span_to_snippet(start.span) else {
74 return false;
75 };
76 UseInclusiveRange::WithoutParen {
77 sugg: range_span.shrink_to_lo().to(lit_span.shrink_to_hi()),
78 start,
79 literal: lit_val - 1,
80 suffix,
81 }
82 } else {
83 UseInclusiveRange::WithParen {
84 eq_sugg: span.shrink_to_lo(),
85 lit_sugg: lit_span,
86 literal: lit_val - 1,
87 suffix,
88 }
89 };
90
91 cx.emit_span_lint(
92 OVERFLOWING_LITERALS,
93 range_span,
94 RangeEndpointOutOfRange { ty, sub: sub_sugg },
95 );
96
97 true
100}
101
102pub(crate) fn int_ty_range(int_ty: ty::IntTy) -> (i128, i128) {
105 match int_ty {
106 ty::IntTy::Isize => (i64::MIN.into(), i64::MAX.into()),
107 ty::IntTy::I8 => (i8::MIN.into(), i8::MAX.into()),
108 ty::IntTy::I16 => (i16::MIN.into(), i16::MAX.into()),
109 ty::IntTy::I32 => (i32::MIN.into(), i32::MAX.into()),
110 ty::IntTy::I64 => (i64::MIN.into(), i64::MAX.into()),
111 ty::IntTy::I128 => (i128::MIN, i128::MAX),
112 }
113}
114
115pub(crate) fn uint_ty_range(uint_ty: ty::UintTy) -> (u128, u128) {
116 let max = match uint_ty {
117 ty::UintTy::Usize => u64::MAX.into(),
118 ty::UintTy::U8 => u8::MAX.into(),
119 ty::UintTy::U16 => u16::MAX.into(),
120 ty::UintTy::U32 => u32::MAX.into(),
121 ty::UintTy::U64 => u64::MAX.into(),
122 ty::UintTy::U128 => u128::MAX,
123 };
124 (0, max)
125}
126
127fn get_bin_hex_repr(cx: &LateContext<'_>, lit: &hir::Lit) -> Option<String> {
128 let src = cx.sess().source_map().span_to_snippet(lit.span).ok()?;
129 let firstch = src.chars().next()?;
130
131 if firstch == '0' {
132 match src.chars().nth(1) {
133 Some('x' | 'b') => return Some(src),
134 _ => return None,
135 }
136 }
137
138 None
139}
140
141fn report_bin_hex_error(
142 cx: &LateContext<'_>,
143 hir_id: HirId,
144 span: Span,
145 ty: attrs::IntType,
146 size: Size,
147 repr_str: String,
148 val: u128,
149 negative: bool,
150) {
151 let (t, actually) = match ty {
152 attrs::IntType::SignedInt(t) => {
153 let actually = if negative { -(size.sign_extend(val)) } else { size.sign_extend(val) };
154 (t.name_str(), actually.to_string())
155 }
156 attrs::IntType::UnsignedInt(t) => {
157 let actually = size.truncate(val);
158 (t.name_str(), actually.to_string())
159 }
160 };
161 let sign = if negative {
162 OverflowingBinHexSign::Negative {
163 lit: repr_str.clone(),
164 dec: val,
165 actually: actually.clone(),
166 ty: t,
167 }
168 } else {
169 OverflowingBinHexSign::Positive {
170 lit: repr_str.clone(),
171 dec: val,
172 actually: actually.clone(),
173 ty: t,
174 }
175 };
176 let sub = get_type_suggestion(cx.typeck_results().node_type(hir_id), val, negative).map(
177 |suggestion_ty| {
178 if let Some(pos) = repr_str.chars().position(|c| c == 'i' || c == 'u') {
179 let (sans_suffix, _) = repr_str.split_at(pos);
180 OverflowingBinHexSub::Suggestion { span, suggestion_ty, sans_suffix }
181 } else {
182 OverflowingBinHexSub::Help { suggestion_ty }
183 }
184 },
185 );
186 let sign_bit_sub = (!negative)
187 .then(|| {
188 let ty::Int(int_ty) = cx.typeck_results().node_type(hir_id).kind() else {
189 return None;
190 };
191
192 let Some(bit_width) = int_ty.bit_width() else {
193 return None; };
195
196 if (val & (1 << (bit_width - 1))) == 0 {
198 return None;
199 }
200
201 let lit_no_suffix =
202 if let Some(pos) = repr_str.chars().position(|c| c == 'i' || c == 'u') {
203 repr_str.split_at(pos).0
204 } else {
205 &repr_str
206 };
207
208 Some(OverflowingBinHexSignBitSub {
209 span,
210 lit_no_suffix,
211 negative_val: actually,
212 int_ty: int_ty.name_str(),
213 uint_ty: Integer::fit_unsigned(val).uint_ty_str(),
214 })
215 })
216 .flatten();
217
218 cx.emit_span_lint(
219 OVERFLOWING_LITERALS,
220 span,
221 OverflowingBinHex { ty: t, sign, sub, sign_bit_sub },
222 )
223}
224
225fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static str> {
229 match t.kind() {
230 ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize) => None,
231 ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
232 ty::Int(_) => {
233 let signed = literal_to_i128(val, negative).map(Integer::fit_signed);
234 if negative {
235 signed.map(Integer::int_ty_str)
236 } else {
237 let unsigned = Integer::fit_unsigned(val);
238 Some(if let Some(signed) = signed {
239 if unsigned.size() < signed.size() {
240 unsigned.uint_ty_str()
241 } else {
242 signed.int_ty_str()
243 }
244 } else {
245 unsigned.uint_ty_str()
246 })
247 }
248 }
249 _ => None,
250 }
251}
252
253fn literal_to_i128(val: u128, negative: bool) -> Option<i128> {
254 if negative {
255 (val <= i128::MAX as u128 + 1).then(|| val.wrapping_neg() as i128)
256 } else {
257 val.try_into().ok()
258 }
259}
260
261fn lint_int_literal<'tcx>(
262 cx: &LateContext<'tcx>,
263 type_limits: &TypeLimits,
264 hir_id: HirId,
265 span: Span,
266 lit: &hir::Lit,
267 t: ty::IntTy,
268 v: u128,
269) {
270 let int_type = t.normalize(cx.sess().target.pointer_width);
271 let (min, max) = int_ty_range(int_type);
272 let max = max as u128;
273 let negative = type_limits.negated_expr_id == Some(hir_id);
274
275 if (negative && v > max + 1) || (!negative && v > max) {
278 if let Some(repr_str) = get_bin_hex_repr(cx, lit) {
279 report_bin_hex_error(
280 cx,
281 hir_id,
282 span,
283 attrs::IntType::SignedInt(t),
284 Integer::from_int_ty(cx, t).size(),
285 repr_str,
286 v,
287 negative,
288 );
289 return;
290 }
291
292 if lint_overflowing_range_endpoint(cx, lit, v, max, hir_id, span, t.name_str()) {
293 return;
295 }
296
297 let span = if negative { type_limits.negated_expr_span.unwrap() } else { span };
298 let lit = cx
299 .sess()
300 .source_map()
301 .span_to_snippet(span)
302 .unwrap_or_else(|_| if negative { ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("-{0}", v)) })format!("-{v}") } else { v.to_string() });
303 let help = get_type_suggestion(cx.typeck_results().node_type(hir_id), v, negative)
304 .map(|suggestion_ty| OverflowingIntHelp { suggestion_ty });
305
306 cx.emit_span_lint(
307 OVERFLOWING_LITERALS,
308 span,
309 OverflowingInt { ty: t.name_str(), lit, min, max, help },
310 );
311 }
312}
313
314fn lint_uint_literal<'tcx>(
315 cx: &LateContext<'tcx>,
316 hir_id: HirId,
317 span: Span,
318 lit: &hir::Lit,
319 t: ty::UintTy,
320) {
321 let uint_type = t.normalize(cx.sess().target.pointer_width);
322 let (min, max) = uint_ty_range(uint_type);
323 let lit_val: u128 = match lit.node {
324 ast::LitKind::Byte(_v) => return,
326 ast::LitKind::Int(v, _) => v.get(),
327 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
328 };
329
330 if lit_val < min || lit_val > max {
331 if let Node::Expr(par_e) = cx.tcx.parent_hir_node(hir_id) {
332 match par_e.kind {
333 hir::ExprKind::Cast(..) => {
334 if let ty::Char = cx.typeck_results().expr_ty(par_e).kind() {
335 if lit_val > 0x10FFFF {
336 cx.emit_span_lint(
337 OVERFLOWING_LITERALS,
338 par_e.span,
339 TooLargeCharCast { literal: lit_val },
340 );
341 } else if (0xD800..=0xDFFF).contains(&lit_val) {
342 cx.emit_span_lint(
343 OVERFLOWING_LITERALS,
344 par_e.span,
345 SurrogateCharCast { literal: lit_val },
346 );
347 } else {
348 cx.emit_span_lint(
349 OVERFLOWING_LITERALS,
350 par_e.span,
351 OnlyCastu8ToChar { span: par_e.span, literal: lit_val },
352 );
353 }
354 return;
355 }
356 }
357 _ => {}
358 }
359 }
360 if lint_overflowing_range_endpoint(cx, lit, lit_val, max, hir_id, span, t.name_str()) {
361 return;
363 }
364 if let Some(repr_str) = get_bin_hex_repr(cx, lit) {
365 report_bin_hex_error(
366 cx,
367 hir_id,
368 span,
369 attrs::IntType::UnsignedInt(t),
370 Integer::from_uint_ty(cx, t).size(),
371 repr_str,
372 lit_val,
373 false,
374 );
375 return;
376 }
377 cx.emit_span_lint(
378 OVERFLOWING_LITERALS,
379 span,
380 OverflowingUInt {
381 ty: t.name_str(),
382 lit: cx
383 .sess()
384 .source_map()
385 .span_to_snippet(lit.span)
386 .unwrap_or_else(|_| lit_val.to_string()),
387 min,
388 max,
389 },
390 );
391 }
392}
393
394fn float_is_infinite<S: Semantics>(v: Symbol) -> Option<bool> {
397 let x: IeeeFloat<S> = v.as_str().parse().ok()?;
398 Some(x.is_infinite())
399}
400
401pub(crate) fn lint_literal<'tcx>(
402 cx: &LateContext<'tcx>,
403 type_limits: &TypeLimits,
404 hir_id: HirId,
405 span: Span,
406 lit: &hir::Lit,
407 negated: bool,
408) {
409 match *cx.typeck_results().node_type(hir_id).kind() {
410 ty::Int(t) => {
411 match lit.node {
412 ast::LitKind::Int(v, ast::LitIntType::Signed(_) | ast::LitIntType::Unsuffixed) => {
413 lint_int_literal(cx, type_limits, hir_id, span, lit, t, v.get())
414 }
415 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
416 };
417 }
418 ty::Uint(t) => {
419 if !!negated { ::core::panicking::panic("assertion failed: !negated") };assert!(!negated);
420 lint_uint_literal(cx, hir_id, span, lit, t)
421 }
422 ty::Float(t) => {
423 let ast::LitKind::Float(v, _) = lit.node else {
424 ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
425 };
426
427 let is_infinite = match t {
428 ty::FloatTy::F16 => float_is_infinite::<HalfS>(v),
429 ty::FloatTy::F32 => float_is_infinite::<SingleS>(v),
430 ty::FloatTy::F64 => float_is_infinite::<DoubleS>(v),
431 ty::FloatTy::F128 => float_is_infinite::<QuadS>(v),
432 };
433
434 if is_infinite == Some(true) {
435 cx.emit_span_lint(
436 OVERFLOWING_LITERALS,
437 span,
438 OverflowingLiteral {
439 ty: t.name_str(),
440 lit: cx
441 .sess()
442 .source_map()
443 .span_to_snippet(lit.span)
444 .unwrap_or_else(|_| v.to_string()),
445 },
446 );
447 }
448 }
449 _ => {}
450 }
451}