@@ -19,11 +19,11 @@ use crate::syntax::symbol::LocalInternedString;
19
19
use crate :: utils:: paths;
20
20
use crate :: utils:: sugg;
21
21
use 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 ,
27
27
} ;
28
28
use if_chain:: if_chain;
29
29
use matches:: matches;
@@ -859,8 +859,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
859
859
[ "nth" , "iter_mut" ] => lint_iter_nth ( cx, expr, arg_lists[ 1 ] , true ) ,
860
860
[ "next" , "skip" ] => lint_iter_skip_next ( cx, expr) ,
861
861
[ "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 ] ) ,
864
864
[ "fold" , ..] => lint_unnecessary_fold ( cx, expr, arg_lists[ 0 ] ) ,
865
865
[ "filter_map" , ..] => unnecessary_filter_map:: lint ( cx, expr, arg_lists[ 0 ] ) ,
866
866
_ => { } ,
@@ -2181,6 +2181,16 @@ fn lint_asref(cx: &LateContext<'_, '_>, expr: &hir::Expr, call_name: &str, as_re
2181
2181
let ( base_res_ty, res_depth) = walk_ptrs_ty_depth ( res_ty) ;
2182
2182
let ( base_rcv_ty, rcv_depth) = walk_ptrs_ty_depth ( rcv_ty) ;
2183
2183
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
+
2184
2194
let mut applicability = Applicability :: MachineApplicable ;
2185
2195
span_lint_and_sugg (
2186
2196
cx,
0 commit comments