]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/utils.rs
Auto merge of #6957 - camsteffen:eq-ty-kind, r=flip1995
[rust.git] / 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;
9 use rustc_middle::ty::Ty;
10 use rustc_span::symbol::sym;
11
12 pub(super) fn derefs_to_slice<'tcx>(
13     cx: &LateContext<'tcx>,
14     expr: &'tcx hir::Expr<'tcx>,
15     ty: Ty<'tcx>,
16 ) -> Option<&'tcx hir::Expr<'tcx>> {
17     fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
18         match ty.kind() {
19             ty::Slice(_) => true,
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),
26             _ => false,
27         }
28     }
29
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])) {
32             Some(&args[0])
33         } else {
34             None
35         }
36     } else {
37         match ty.kind() {
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) {
42                     Some(expr)
43                 } else {
44                     None
45                 }
46             },
47             _ => None,
48         }
49     }
50 }
51
52 pub(super) fn get_hint_if_single_char_arg(
53     cx: &LateContext<'_>,
54     arg: &hir::Expr<'_>,
55     applicability: &mut Applicability,
56 ) -> Option<String> {
57     if_chain! {
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;
62         then {
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)]
68             } else {
69                 // for regular string: "a"
70                 &snip[1..(snip.len() - 1)]
71             };
72             let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });
73             Some(hint)
74         } else {
75             None
76         }
77     }
78 }