]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/utils.rs
Remove unnecessary .as_ref().
[rust.git] / src / tools / clippy / clippy_lints / src / methods / utils.rs
1 use clippy_utils::source::snippet_with_applicability;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use if_chain::if_chain;
4 use rustc_ast::ast;
5 use rustc_errors::Applicability;
6 use rustc_hir as hir;
7 use rustc_lint::LateContext;
8 use rustc_middle::ty::{self, Ty};
9 use rustc_span::symbol::sym;
10
11 pub(super) fn derefs_to_slice<'tcx>(
12     cx: &LateContext<'tcx>,
13     expr: &'tcx hir::Expr<'tcx>,
14     ty: Ty<'tcx>,
15 ) -> Option<&'tcx hir::Expr<'tcx>> {
16     fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
17         match ty.kind() {
18             ty::Slice(_) => true,
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),
21             ty::Array(_, size) => size.try_eval_usize(cx.tcx, cx.param_env).is_some(),
22             ty::Ref(_, inner, _) => may_slice(cx, *inner),
23             _ => false,
24         }
25     }
26
27     if let hir::ExprKind::MethodCall(path, [self_arg, ..], _) = &expr.kind {
28         if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(self_arg)) {
29             Some(self_arg)
30         } else {
31             None
32         }
33     } else {
34         match ty.kind() {
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) {
39                     Some(expr)
40                 } else {
41                     None
42                 }
43             },
44             _ => None,
45         }
46     }
47 }
48
49 pub(super) fn get_hint_if_single_char_arg(
50     cx: &LateContext<'_>,
51     arg: &hir::Expr<'_>,
52     applicability: &mut Applicability,
53 ) -> Option<String> {
54     if_chain! {
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;
59         then {
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)]
65             } else {
66                 // for regular string: "a"
67                 &snip[1..(snip.len() - 1)]
68             };
69
70             let hint = format!("'{}'", match ch {
71                 "'" => "\\'" ,
72                 r"\" => "\\\\",
73                 _ => ch,
74             });
75
76             Some(hint)
77         } else {
78             None
79         }
80     }
81 }