1 use clippy_utils::source::snippet_with_applicability;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use if_chain::if_chain;
5 use rustc_errors::Applicability;
7 use rustc_lint::LateContext;
8 use rustc_middle::ty::{self, Ty};
9 use rustc_span::symbol::sym;
11 pub(super) fn derefs_to_slice<'tcx>(
12 cx: &LateContext<'tcx>,
13 expr: &'tcx hir::Expr<'tcx>,
15 ) -> Option<&'tcx hir::Expr<'tcx>> {
16 fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
19 ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
20 ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type),
21 ty::Array(_, size) => size.try_eval_usize(cx.tcx, cx.param_env).is_some(),
22 ty::Ref(_, inner, _) => may_slice(cx, inner),
27 if let hir::ExprKind::MethodCall(path, _, args, _) = expr.kind {
28 if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
35 ty::Slice(_) => Some(expr),
36 ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
37 ty::Ref(_, inner, _) => {
38 if may_slice(cx, inner) {
49 pub(super) fn get_hint_if_single_char_arg(
52 applicability: &mut Applicability,
55 if let hir::ExprKind::Lit(lit) = &arg.kind;
56 if let ast::LitKind::Str(r, style) = lit.node;
57 let string = r.as_str();
58 if string.chars().count() == 1;
60 let snip = snippet_with_applicability(cx, arg.span, &string, applicability);
61 let ch = if let ast::StrStyle::Raw(nhash) = style {
62 let nhash = nhash as usize;
63 // for raw string: r##"a"##
64 &snip[(nhash + 2)..(snip.len() - 1 - nhash)]
66 // for regular string: "a"
67 &snip[1..(snip.len() - 1)]
69 let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });