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;
9 use rustc_middle::ty::Ty;
10 use rustc_span::symbol::sym;
12 pub(super) fn derefs_to_slice<'tcx>(
13 cx: &LateContext<'tcx>,
14 expr: &'tcx hir::Expr<'tcx>,
16 ) -> Option<&'tcx hir::Expr<'tcx>> {
17 fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
20 ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
21 ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type),
22 ty::Array(_, size) => size
23 .try_eval_usize(cx.tcx, cx.param_env)
24 .map_or(false, |size| size < 32),
25 ty::Ref(_, inner, _) => may_slice(cx, inner),
30 if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind {
31 if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
38 ty::Slice(_) => Some(expr),
39 ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
40 ty::Ref(_, inner, _) => {
41 if may_slice(cx, inner) {
52 pub(super) fn get_hint_if_single_char_arg(
55 applicability: &mut Applicability,
58 if let hir::ExprKind::Lit(lit) = &arg.kind;
59 if let ast::LitKind::Str(r, style) = lit.node;
60 let string = r.as_str();
61 if string.chars().count() == 1;
63 let snip = snippet_with_applicability(cx, arg.span, &string, applicability);
64 let ch = if let ast::StrStyle::Raw(nhash) = style {
65 let nhash = nhash as usize;
66 // for raw string: r##"a"##
67 &snip[(nhash + 2)..(snip.len() - 1 - nhash)]
69 // for regular string: "a"
70 &snip[1..(snip.len() - 1)]
72 let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });