@@ -42,6 +42,7 @@ mod uninit_assumed_init;
42
42
mod unnecessary_filter_map;
43
43
mod unnecessary_lazy_eval;
44
44
mod unwrap_used;
45
+ mod useless_asref;
45
46
mod wrong_self_convention;
46
47
mod zst_offset;
47
48
@@ -65,11 +66,11 @@ use rustc_typeck::hir_ty_to_ty;
65
66
use crate :: utils:: eager_or_lazy:: is_lazyness_candidate;
66
67
use crate :: utils:: usage:: mutated_variables;
67
68
use 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,
69
70
is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, match_qpath, match_trait_method,
70
71
match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths, remove_blocks, return_ty,
71
72
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 ,
73
74
} ;
74
75
75
76
declare_clippy_lint ! {
@@ -1733,8 +1734,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
1733
1734
[ "step_by" , ..] => iterator_step_by_zero:: check ( cx, expr, arg_lists[ 0 ] ) ,
1734
1735
[ "next" , "skip" ] => iter_skip_next:: check ( cx, expr, arg_lists[ 1 ] ) ,
1735
1736
[ "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 ] ) ,
1738
1739
[ "fold" , ..] => lint_unnecessary_fold ( cx, expr, arg_lists[ 0 ] , method_spans[ 0 ] ) ,
1739
1740
[ "filter_map" , ..] => {
1740
1741
unnecessary_filter_map:: check ( cx, expr, arg_lists[ 0 ] ) ;
@@ -2751,42 +2752,6 @@ fn get_hint_if_single_char_arg(
2751
2752
}
2752
2753
}
2753
2754
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
-
2790
2755
const FN_HEADER : hir:: FnHeader = hir:: FnHeader {
2791
2756
unsafety : hir:: Unsafety :: Normal ,
2792
2757
constness : hir:: Constness :: NotConst ,
0 commit comments