@@ -42,6 +42,7 @@ mod uninit_assumed_init;
4242mod unnecessary_filter_map;
4343mod unnecessary_lazy_eval;
4444mod unwrap_used;
45+ mod useless_asref;
4546mod wrong_self_convention;
4647mod zst_offset;
4748
@@ -65,11 +66,11 @@ use rustc_typeck::hir_ty_to_ty;
6566use crate :: utils:: eager_or_lazy:: is_lazyness_candidate;
6667use crate :: utils:: usage:: mutated_variables;
6768use crate :: utils:: {
68- contains_return, contains_ty, get_parent_expr , get_trait_def_id, implements_trait, in_macro, is_copy, is_expn_of,
69+ contains_return, contains_ty, get_trait_def_id, implements_trait, in_macro, is_copy, is_expn_of,
6970 is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, match_qpath, match_trait_method,
7071 match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths, remove_blocks, return_ty,
7172 single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
72- span_lint_and_help, span_lint_and_sugg, strip_pat_refs, walk_ptrs_ty_depth , SpanlessEq ,
73+ span_lint_and_help, span_lint_and_sugg, strip_pat_refs, SpanlessEq ,
7374} ;
7475
7576declare_clippy_lint ! {
@@ -1733,8 +1734,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17331734 [ "step_by" , ..] => iterator_step_by_zero:: check ( cx, expr, arg_lists[ 0 ] ) ,
17341735 [ "next" , "skip" ] => iter_skip_next:: check ( cx, expr, arg_lists[ 1 ] ) ,
17351736 [ "collect" , "cloned" ] => iter_cloned_collect:: check ( cx, expr, arg_lists[ 1 ] ) ,
1736- [ "as_ref" ] => lint_asref ( cx, expr, "as_ref" , arg_lists[ 0 ] ) ,
1737- [ "as_mut" ] => lint_asref ( cx, expr, "as_mut" , arg_lists[ 0 ] ) ,
1737+ [ "as_ref" ] => useless_asref :: check ( cx, expr, "as_ref" , arg_lists[ 0 ] ) ,
1738+ [ "as_mut" ] => useless_asref :: check ( cx, expr, "as_mut" , arg_lists[ 0 ] ) ,
17381739 [ "fold" , ..] => lint_unnecessary_fold ( cx, expr, arg_lists[ 0 ] , method_spans[ 0 ] ) ,
17391740 [ "filter_map" , ..] => {
17401741 unnecessary_filter_map:: check ( cx, expr, arg_lists[ 0 ] ) ;
@@ -2751,42 +2752,6 @@ fn get_hint_if_single_char_arg(
27512752 }
27522753}
27532754
2754- /// Checks for the `USELESS_ASREF` lint.
2755- fn lint_asref ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > , call_name : & str , as_ref_args : & [ hir:: Expr < ' _ > ] ) {
2756- // when we get here, we've already checked that the call name is "as_ref" or "as_mut"
2757- // check if the call is to the actual `AsRef` or `AsMut` trait
2758- if match_trait_method ( cx, expr, & paths:: ASREF_TRAIT ) || match_trait_method ( cx, expr, & paths:: ASMUT_TRAIT ) {
2759- // check if the type after `as_ref` or `as_mut` is the same as before
2760- let recvr = & as_ref_args[ 0 ] ;
2761- let rcv_ty = cx. typeck_results ( ) . expr_ty ( recvr) ;
2762- let res_ty = cx. typeck_results ( ) . expr_ty ( expr) ;
2763- let ( base_res_ty, res_depth) = walk_ptrs_ty_depth ( res_ty) ;
2764- let ( base_rcv_ty, rcv_depth) = walk_ptrs_ty_depth ( rcv_ty) ;
2765- if base_rcv_ty == base_res_ty && rcv_depth >= res_depth {
2766- // allow the `as_ref` or `as_mut` if it is followed by another method call
2767- if_chain ! {
2768- if let Some ( parent) = get_parent_expr( cx, expr) ;
2769- if let hir:: ExprKind :: MethodCall ( _, ref span, _, _) = parent. kind;
2770- if span != & expr. span;
2771- then {
2772- return ;
2773- }
2774- }
2775-
2776- let mut applicability = Applicability :: MachineApplicable ;
2777- span_lint_and_sugg (
2778- cx,
2779- USELESS_ASREF ,
2780- expr. span ,
2781- & format ! ( "this call to `{}` does nothing" , call_name) ,
2782- "try this" ,
2783- snippet_with_applicability ( cx, recvr. span , ".." , & mut applicability) . to_string ( ) ,
2784- applicability,
2785- ) ;
2786- }
2787- }
2788- }
2789-
27902755const FN_HEADER : hir:: FnHeader = hir:: FnHeader {
27912756 unsafety : hir:: Unsafety :: Normal ,
27922757 constness : hir:: Constness :: NotConst ,
0 commit comments