Skip to main content

rustc_lint/types/
literal.rs

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