|
| 1 | +use std::str::Utf8Error; |
| 2 | + |
| 3 | +use rustc_ast::{BorrowKind, LitKind}; |
| 4 | +use rustc_hir::{Expr, ExprKind}; |
| 5 | +use rustc_span::source_map::Spanned; |
| 6 | +use rustc_span::sym; |
| 7 | + |
| 8 | +use crate::lints::InvalidFromUtf8UncheckedDiag; |
| 9 | +use crate::{LateContext, LateLintPass, LintContext}; |
| 10 | + |
| 11 | +declare_lint! { |
| 12 | + /// The `invalid_from_utf8_unchecked` lint checks for calls to |
| 13 | + /// `std::str::from_utf8_unchecked` and `std::str::from_utf8_unchecked_mut` |
| 14 | + /// with an invalid UTF-8 literal. |
| 15 | + /// |
| 16 | + /// ### Example |
| 17 | + /// |
| 18 | + /// ```rust,compile_fail |
| 19 | + /// # #[allow(unused)] |
| 20 | + /// unsafe { |
| 21 | + /// std::str::from_utf8_unchecked(b"Ru\x82st"); |
| 22 | + /// } |
| 23 | + /// ``` |
| 24 | + /// |
| 25 | + /// {{produces}} |
| 26 | + /// |
| 27 | + /// ### Explanation |
| 28 | + /// |
| 29 | + /// Creating such a `str` would result in undefined behavior as per documentation |
| 30 | + /// for `std::str::from_utf8_unchecked` and `std::str::from_utf8_unchecked_mut`. |
| 31 | + pub INVALID_FROM_UTF8_UNCHECKED, |
| 32 | + Deny, |
| 33 | + "using a non UTF-8 literal in `std::str::from_utf8_unchecked`" |
| 34 | +} |
| 35 | + |
| 36 | +declare_lint_pass!(InvalidFromUtf8 => [INVALID_FROM_UTF8_UNCHECKED]); |
| 37 | + |
| 38 | +impl<'tcx> LateLintPass<'tcx> for InvalidFromUtf8 { |
| 39 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 40 | + if let ExprKind::Call(path, [arg]) = expr.kind |
| 41 | + && let ExprKind::Path(ref qpath) = path.kind |
| 42 | + && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() |
| 43 | + && let Some(diag_item) = cx.tcx.get_diagnostic_name(def_id) |
| 44 | + && [sym::str_from_utf8_unchecked, sym::str_from_utf8_unchecked_mut].contains(&diag_item) |
| 45 | + { |
| 46 | + let lint = |utf8_error: Utf8Error| { |
| 47 | + let method = diag_item.as_str().strip_prefix("str_").unwrap(); |
| 48 | + cx.emit_spanned_lint(INVALID_FROM_UTF8_UNCHECKED, expr.span, InvalidFromUtf8UncheckedDiag { |
| 49 | + method: format!("std::str::{method}"), |
| 50 | + valid_up_to: utf8_error.valid_up_to(), |
| 51 | + label: arg.span, |
| 52 | + }) |
| 53 | + }; |
| 54 | + |
| 55 | + match &arg.kind { |
| 56 | + ExprKind::Lit(Spanned { node: lit, .. }) => { |
| 57 | + if let LitKind::ByteStr(bytes, _) = &lit |
| 58 | + && let Err(utf8_error) = std::str::from_utf8(bytes) |
| 59 | + { |
| 60 | + lint(utf8_error); |
| 61 | + } |
| 62 | + }, |
| 63 | + ExprKind::AddrOf(BorrowKind::Ref, _, Expr { kind: ExprKind::Array(args), .. }) => { |
| 64 | + let elements = args.iter().map(|e|{ |
| 65 | + match &e.kind { |
| 66 | + ExprKind::Lit(Spanned { node: lit, .. }) => match lit { |
| 67 | + LitKind::Byte(b) => Some(*b), |
| 68 | + LitKind::Int(b, _) => Some(*b as u8), |
| 69 | + _ => None |
| 70 | + } |
| 71 | + _ => None |
| 72 | + } |
| 73 | + }).collect::<Option<Vec<_>>>(); |
| 74 | + |
| 75 | + if let Some(elements) = elements |
| 76 | + && let Err(utf8_error) = std::str::from_utf8(&elements) |
| 77 | + { |
| 78 | + lint(utf8_error); |
| 79 | + } |
| 80 | + } |
| 81 | + _ => {} |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments