Skip to content

Commit a637d55

Browse files
committed
Auto merge of #3557 - detrumi:allow_asref_adjustments, r=oli-obk
Fix false positive in useless_asref Fixes #3480
2 parents 091fd03 + 24ef8db commit a637d55

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

clippy_lints/src/methods/mod.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ use crate::syntax::symbol::LocalInternedString;
1919
use crate::utils::paths;
2020
use crate::utils::sugg;
2121
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,
2727
};
2828
use if_chain::if_chain;
2929
use 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,

tests/ui/useful_asref.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
#![deny(clippy::useless_asref)]
11+
12+
trait Trait {
13+
fn as_ptr(&self);
14+
}
15+
16+
impl<'a> Trait for &'a [u8] {
17+
fn as_ptr(&self) {
18+
self.as_ref().as_ptr();
19+
}
20+
}
21+
22+
fn main() {}

0 commit comments

Comments
 (0)