Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 78e572c

Browse files
committed
move useless_asref to its own module
1 parent caaba82 commit 78e572c

File tree

2 files changed

+50
-40
lines changed

2 files changed

+50
-40
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod uninit_assumed_init;
4242
mod unnecessary_filter_map;
4343
mod unnecessary_lazy_eval;
4444
mod unwrap_used;
45+
mod useless_asref;
4546
mod wrong_self_convention;
4647
mod zst_offset;
4748

@@ -65,11 +66,11 @@ use rustc_typeck::hir_ty_to_ty;
6566
use crate::utils::eager_or_lazy::is_lazyness_candidate;
6667
use crate::utils::usage::mutated_variables;
6768
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,
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

7576
declare_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-
27902755
const FN_HEADER: hir::FnHeader = hir::FnHeader {
27912756
unsafety: hir::Unsafety::Normal,
27922757
constness: hir::Constness::NotConst,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use crate::utils::{
2+
get_parent_expr, match_trait_method, paths, snippet_with_applicability, span_lint_and_sugg, walk_ptrs_ty_depth,
3+
};
4+
use if_chain::if_chain;
5+
use rustc_errors::Applicability;
6+
use rustc_hir as hir;
7+
use rustc_lint::LateContext;
8+
9+
use super::USELESS_ASREF;
10+
11+
/// Checks for the `USELESS_ASREF` lint.
12+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_ref_args: &[hir::Expr<'_>]) {
13+
// when we get here, we've already checked that the call name is "as_ref" or "as_mut"
14+
// check if the call is to the actual `AsRef` or `AsMut` trait
15+
if match_trait_method(cx, expr, &paths::ASREF_TRAIT) || match_trait_method(cx, expr, &paths::ASMUT_TRAIT) {
16+
// check if the type after `as_ref` or `as_mut` is the same as before
17+
let recvr = &as_ref_args[0];
18+
let rcv_ty = cx.typeck_results().expr_ty(recvr);
19+
let res_ty = cx.typeck_results().expr_ty(expr);
20+
let (base_res_ty, res_depth) = walk_ptrs_ty_depth(res_ty);
21+
let (base_rcv_ty, rcv_depth) = walk_ptrs_ty_depth(rcv_ty);
22+
if base_rcv_ty == base_res_ty && rcv_depth >= res_depth {
23+
// allow the `as_ref` or `as_mut` if it is followed by another method call
24+
if_chain! {
25+
if let Some(parent) = get_parent_expr(cx, expr);
26+
if let hir::ExprKind::MethodCall(_, ref span, _, _) = parent.kind;
27+
if span != &expr.span;
28+
then {
29+
return;
30+
}
31+
}
32+
33+
let mut applicability = Applicability::MachineApplicable;
34+
span_lint_and_sugg(
35+
cx,
36+
USELESS_ASREF,
37+
expr.span,
38+
&format!("this call to `{}` does nothing", call_name),
39+
"try this",
40+
snippet_with_applicability(cx, recvr.span, "..", &mut applicability).to_string(),
41+
applicability,
42+
);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)