1 use super::utils
::derefs_to_slice
;
2 use clippy_utils
::diagnostics
::span_lint_and_sugg
;
3 use clippy_utils
::source
::snippet_with_applicability
;
4 use clippy_utils
::ty
::{is_type_diagnostic_item, match_type}
;
5 use clippy_utils
::{get_parent_expr, paths}
;
6 use if_chain
::if_chain
;
7 use rustc_errors
::Applicability
;
9 use rustc_lint
::LateContext
;
12 use super::GET_UNWRAP
;
14 pub(super) fn check
<'tcx
>(
15 cx
: &LateContext
<'tcx
>,
17 recv
: &'tcx hir
::Expr
<'tcx
>,
18 get_arg
: &'tcx hir
::Expr
<'_
>,
21 // Note: we don't want to lint `get_mut().unwrap` for `HashMap` or `BTreeMap`,
22 // because they do not implement `IndexMut`
23 let mut applicability
= Applicability
::MachineApplicable
;
24 let expr_ty
= cx
.typeck_results().expr_ty(recv
);
25 let get_args_str
= snippet_with_applicability(cx
, get_arg
.span
, "..", &mut applicability
);
27 let caller_type
= if derefs_to_slice(cx
, recv
, expr_ty
).is_some() {
28 needs_ref
= get_args_str
.parse
::<usize>().is_ok();
30 } else if is_type_diagnostic_item(cx
, expr_ty
, sym
::vec_type
) {
31 needs_ref
= get_args_str
.parse
::<usize>().is_ok();
33 } else if is_type_diagnostic_item(cx
, expr_ty
, sym
::vecdeque_type
) {
34 needs_ref
= get_args_str
.parse
::<usize>().is_ok();
36 } else if !is_mut
&& is_type_diagnostic_item(cx
, expr_ty
, sym
::hashmap_type
) {
39 } else if !is_mut
&& match_type(cx
, expr_ty
, &paths
::BTREEMAP
) {
43 return; // caller is not a type that we want to lint
46 let mut span
= expr
.span
;
48 // Handle the case where the result is immediately dereferenced
49 // by not requiring ref and pulling the dereference into the
53 if let Some(parent
) = get_parent_expr(cx
, expr
);
54 if let hir
::ExprKind
::Unary(hir
::UnOp
::Deref
, _
) = parent
.kind
;
61 let mut_str
= if is_mut { "_mut" }
else { "" }
;
62 let borrow_str
= if !needs_ref
{
75 "called `.get{0}().unwrap()` on a {1}. Using `[]` is more clear and more concise",
82 snippet_with_applicability(cx
, recv
.span
, "..", &mut applicability
),