@@ -19,11 +19,11 @@ use crate::syntax::symbol::LocalInternedString;
1919use crate :: utils:: paths;
2020use crate :: utils:: sugg;
2121use crate :: utils:: {
22- get_arg_name, get_trait_def_id, implements_trait, in_macro, is_copy, is_expn_of, is_self, is_self_ty ,
23- iter_input_pats, last_path_segment, match_def_path, match_path, match_qpath, match_trait_method, match_type ,
24- match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys, single_segment_path, snippet ,
25- snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_sugg, span_lint_and_then ,
26- span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq ,
22+ get_arg_name, get_parent_expr , get_trait_def_id, implements_trait, in_macro, is_copy, is_expn_of, is_self,
23+ is_self_ty , iter_input_pats, last_path_segment, match_def_path, match_path, match_qpath, match_trait_method,
24+ match_type , match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys, single_segment_path,
25+ snippet , snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_sugg,
26+ span_lint_and_then , span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq ,
2727} ;
2828use if_chain:: if_chain;
2929use matches:: matches;
@@ -859,8 +859,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
859859 [ "nth" , "iter_mut" ] => lint_iter_nth ( cx, expr, arg_lists[ 1 ] , true ) ,
860860 [ "next" , "skip" ] => lint_iter_skip_next ( cx, expr) ,
861861 [ "collect" , "cloned" ] => lint_iter_cloned_collect ( cx, expr, arg_lists[ 1 ] ) ,
862- [ "as_ref" , .. ] => lint_asref ( cx, expr, "as_ref" , arg_lists[ 0 ] ) ,
863- [ "as_mut" , .. ] => lint_asref ( cx, expr, "as_mut" , arg_lists[ 0 ] ) ,
862+ [ "as_ref" ] => lint_asref ( cx, expr, "as_ref" , arg_lists[ 0 ] ) ,
863+ [ "as_mut" ] => lint_asref ( cx, expr, "as_mut" , arg_lists[ 0 ] ) ,
864864 [ "fold" , ..] => lint_unnecessary_fold ( cx, expr, arg_lists[ 0 ] ) ,
865865 [ "filter_map" , ..] => unnecessary_filter_map:: lint ( cx, expr, arg_lists[ 0 ] ) ,
866866 _ => { } ,
@@ -2181,6 +2181,16 @@ fn lint_asref(cx: &LateContext<'_, '_>, expr: &hir::Expr, call_name: &str, as_re
21812181 let ( base_res_ty, res_depth) = walk_ptrs_ty_depth ( res_ty) ;
21822182 let ( base_rcv_ty, rcv_depth) = walk_ptrs_ty_depth ( rcv_ty) ;
21832183 if base_rcv_ty == base_res_ty && rcv_depth >= res_depth {
2184+ // allow the `as_ref` or `as_mut` if it is followed by another method call
2185+ if_chain ! {
2186+ if let Some ( parent) = get_parent_expr( cx, expr) ;
2187+ if let hir:: ExprKind :: MethodCall ( _, ref span, _) = parent. node;
2188+ if span != & expr. span;
2189+ then {
2190+ return ;
2191+ }
2192+ }
2193+
21842194 let mut applicability = Applicability :: MachineApplicable ;
21852195 span_lint_and_sugg (
21862196 cx,
0 commit comments