|
| 1 | +use clippy_config::msrvs::{self, Msrv}; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::get_parent_expr; |
| 4 | +use clippy_utils::source::snippet; |
| 5 | +use rustc_ast::{LitKind, StrStyle}; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{Expr, ExprKind, QPath, TyKind}; |
| 8 | +use rustc_lint::LateContext; |
| 9 | +use rustc_span::{sym, Span}; |
| 10 | + |
| 11 | +use super::MANUAL_C_STR_LITERALS; |
| 12 | + |
| 13 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args: &[Expr<'_>], msrv: &Msrv) { |
| 14 | + if let ExprKind::Path(QPath::TypeRelative(cstr, fn_name)) = &func.kind |
| 15 | + && let TyKind::Path(QPath::Resolved(_, ty_path)) = &cstr.kind |
| 16 | + && cx.tcx.lang_items().c_str() == ty_path.res.opt_def_id() |
| 17 | + && let [arg] = args |
| 18 | + && msrv.meets(msrvs::C_STR_LITERALS) |
| 19 | + { |
| 20 | + match fn_name.ident.name.as_str() { |
| 21 | + name @ ("from_bytes_with_nul" | "from_bytes_with_nul_unchecked") |
| 22 | + if !arg.span.from_expansion() |
| 23 | + && let ExprKind::Lit(lit) = arg.kind |
| 24 | + && let LitKind::ByteStr(_, StrStyle::Cooked) = lit.node => |
| 25 | + { |
| 26 | + check_from_bytes(cx, expr, arg, name); |
| 27 | + }, |
| 28 | + "from_ptr" => check_from_ptr(cx, expr, arg), |
| 29 | + _ => {}, |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/// Checks `CStr::from_bytes_with_nul(b"foo\0")` |
| 35 | +fn check_from_ptr(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>) { |
| 36 | + if let ExprKind::MethodCall(method, lit, [], _) = peel_ptr_cast(arg).kind |
| 37 | + && method.ident.name == sym::as_ptr |
| 38 | + && !lit.span.from_expansion() |
| 39 | + && let ExprKind::Lit(lit) = lit.kind |
| 40 | + && let LitKind::ByteStr(_, StrStyle::Cooked) = lit.node |
| 41 | + { |
| 42 | + span_lint_and_sugg( |
| 43 | + cx, |
| 44 | + MANUAL_C_STR_LITERALS, |
| 45 | + expr.span, |
| 46 | + "calling `CStr::from_ptr` with a byte string literal", |
| 47 | + r#"use a `c""` literal"#, |
| 48 | + rewrite_as_cstr(cx, lit.span), |
| 49 | + Applicability::MachineApplicable, |
| 50 | + ); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/// Checks `CStr::from_ptr(b"foo\0".as_ptr().cast())` |
| 55 | +fn check_from_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, method: &str) { |
| 56 | + let (span, applicability) = if let Some(parent) = get_parent_expr(cx, expr) |
| 57 | + && let ExprKind::MethodCall(method, ..) = parent.kind |
| 58 | + && [sym::unwrap, sym::expect].contains(&method.ident.name) |
| 59 | + { |
| 60 | + (parent.span, Applicability::MachineApplicable) |
| 61 | + } else if method == "from_bytes_with_nul_unchecked" { |
| 62 | + // `*_unchecked` returns `&CStr` directly, nothing needs to be changed |
| 63 | + (expr.span, Applicability::MachineApplicable) |
| 64 | + } else { |
| 65 | + // User needs to remove error handling, can't be machine applicable |
| 66 | + (expr.span, Applicability::MaybeIncorrect) |
| 67 | + }; |
| 68 | + |
| 69 | + span_lint_and_sugg( |
| 70 | + cx, |
| 71 | + MANUAL_C_STR_LITERALS, |
| 72 | + span, |
| 73 | + "calling `CStr::new` with a byte string literal", |
| 74 | + r#"use a `c""` literal"#, |
| 75 | + rewrite_as_cstr(cx, arg.span), |
| 76 | + applicability, |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +/// Rewrites a byte string literal to a c-str literal. |
| 81 | +/// `b"foo\0"` -> `c"foo"` |
| 82 | +pub fn rewrite_as_cstr(cx: &LateContext<'_>, span: Span) -> String { |
| 83 | + let mut sugg = String::from("c") + snippet(cx, span.source_callsite(), "..").trim_start_matches('b'); |
| 84 | + |
| 85 | + // NUL byte should always be right before the closing quote. |
| 86 | + // (Can rfind ever return `None`?) |
| 87 | + if let Some(quote_pos) = sugg.rfind('"') { |
| 88 | + // Possible values right before the quote: |
| 89 | + // - literal NUL value |
| 90 | + if sugg.as_bytes()[quote_pos - 1] == b'\0' { |
| 91 | + sugg.remove(quote_pos - 1); |
| 92 | + } |
| 93 | + // - \x00 |
| 94 | + else if sugg[..quote_pos].ends_with("\\x00") { |
| 95 | + sugg.replace_range(quote_pos - 4..quote_pos, ""); |
| 96 | + } |
| 97 | + // - \0 |
| 98 | + else if sugg[..quote_pos].ends_with("\\0") { |
| 99 | + sugg.replace_range(quote_pos - 2..quote_pos, ""); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + sugg |
| 104 | +} |
| 105 | + |
| 106 | +/// `x.cast()` -> `x` |
| 107 | +/// `x as *const _` -> `x` |
| 108 | +fn peel_ptr_cast<'tcx>(e: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> { |
| 109 | + match &e.kind { |
| 110 | + ExprKind::MethodCall(method, receiver, [], _) if method.ident.as_str() == "cast" => peel_ptr_cast(receiver), |
| 111 | + ExprKind::Cast(expr, _) => peel_ptr_cast(expr), |
| 112 | + _ => e, |
| 113 | + } |
| 114 | +} |
0 commit comments