Skip to content

Commit 99b8106

Browse files
authored
Cleanup: do not handle methods from several places (#15751)
Some methods lints were handled in the `methods` module outside the `check_methods()` function. changelog: none
2 parents e70b206 + c425389 commit 99b8106

File tree

7 files changed

+42
-81
lines changed

7 files changed

+42
-81
lines changed

clippy_lints/src/methods/clone_on_copy.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,14 @@ use clippy_utils::ty::is_copy;
44
use rustc_errors::Applicability;
55
use rustc_hir::{BindingMode, ByRef, Expr, ExprKind, MatchSource, Node, PatKind, QPath};
66
use rustc_lint::LateContext;
7+
use rustc_middle::ty;
78
use rustc_middle::ty::adjustment::Adjust;
89
use rustc_middle::ty::print::with_forced_trimmed_paths;
9-
use rustc_middle::ty::{self};
10-
use rustc_span::symbol::{Symbol, sym};
1110

1211
use super::CLONE_ON_COPY;
1312

1413
/// Checks for the `CLONE_ON_COPY` lint.
15-
pub(super) fn check(
16-
cx: &LateContext<'_>,
17-
expr: &Expr<'_>,
18-
method_name: Symbol,
19-
receiver: &Expr<'_>,
20-
args: &[Expr<'_>],
21-
) {
22-
let arg = if method_name == sym::clone && args.is_empty() {
23-
receiver
24-
} else {
25-
return;
26-
};
14+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
2715
if cx
2816
.typeck_results()
2917
.type_dependent_def_id(expr.hir_id)
@@ -33,10 +21,10 @@ pub(super) fn check(
3321
{
3422
return;
3523
}
36-
let arg_adjustments = cx.typeck_results().expr_adjustments(arg);
24+
let arg_adjustments = cx.typeck_results().expr_adjustments(receiver);
3725
let arg_ty = arg_adjustments
3826
.last()
39-
.map_or_else(|| cx.typeck_results().expr_ty(arg), |a| a.target);
27+
.map_or_else(|| cx.typeck_results().expr_ty(receiver), |a| a.target);
4028

4129
let ty = cx.typeck_results().expr_ty(expr);
4230
if let ty::Ref(_, inner, _) = arg_ty.kind()
@@ -75,7 +63,7 @@ pub(super) fn check(
7563
};
7664

7765
let mut app = Applicability::MachineApplicable;
78-
let snip = snippet_with_context(cx, arg.span, expr.span.ctxt(), "_", &mut app).0;
66+
let snip = snippet_with_context(cx, receiver.span, expr.span.ctxt(), "_", &mut app).0;
7967

8068
let deref_count = arg_adjustments
8169
.iter()

clippy_lints/src/methods/clone_on_ref_ptr.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,11 @@ use rustc_errors::Applicability;
44
use rustc_hir as hir;
55
use rustc_lint::LateContext;
66
use rustc_middle::ty;
7-
use rustc_span::symbol::{Symbol, sym};
7+
use rustc_span::symbol::sym;
88

99
use super::CLONE_ON_REF_PTR;
1010

11-
pub(super) fn check(
12-
cx: &LateContext<'_>,
13-
expr: &hir::Expr<'_>,
14-
method_name: Symbol,
15-
receiver: &hir::Expr<'_>,
16-
args: &[hir::Expr<'_>],
17-
) {
18-
if !(args.is_empty() && method_name == sym::clone) {
19-
return;
20-
}
11+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, receiver: &hir::Expr<'_>) {
2112
let obj_ty = cx.typeck_results().expr_ty(receiver).peel_refs();
2213

2314
if let ty::Adt(adt, subst) = obj_ty.kind()

clippy_lints/src/methods/expect_fun_call.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use clippy_utils::{contains_return, is_inside_always_const_context, peel_blocks}
77
use rustc_errors::Applicability;
88
use rustc_hir as hir;
99
use rustc_lint::LateContext;
10+
use rustc_span::Span;
1011
use rustc_span::symbol::sym;
11-
use rustc_span::{Span, Symbol};
1212
use std::borrow::Cow;
1313
use std::ops::ControlFlow;
1414

@@ -20,16 +20,11 @@ pub(super) fn check<'tcx>(
2020
format_args_storage: &FormatArgsStorage,
2121
expr: &hir::Expr<'_>,
2222
method_span: Span,
23-
name: Symbol,
2423
receiver: &'tcx hir::Expr<'tcx>,
25-
args: &'tcx [hir::Expr<'tcx>],
24+
arg: &'tcx hir::Expr<'tcx>,
2625
) {
27-
if name == sym::expect
28-
&& let [arg] = args
29-
&& let arg_root = get_arg_root(cx, arg)
30-
&& contains_call(cx, arg_root)
31-
&& !contains_return(arg_root)
32-
{
26+
let arg_root = get_arg_root(cx, arg);
27+
if contains_call(cx, arg_root) && !contains_return(arg_root) {
3328
let receiver_type = cx.typeck_results().expr_ty_adjusted(receiver);
3429
let closure_args = if is_type_diagnostic_item(cx, receiver_type, sym::Option) {
3530
"||"
@@ -54,7 +49,7 @@ pub(super) fn check<'tcx>(
5449
cx,
5550
EXPECT_FUN_CALL,
5651
span_replace_word,
57-
format!("function call inside of `{name}`"),
52+
"function call inside of `expect`",
5853
"try",
5954
format!("unwrap_or_else({closure_args} panic!({sugg}))"),
6055
applicability,
@@ -69,7 +64,7 @@ pub(super) fn check<'tcx>(
6964
cx,
7065
EXPECT_FUN_CALL,
7166
span_replace_word,
72-
format!("function call inside of `{name}`"),
67+
"function call inside of `expect`",
7368
"try",
7469
format!("unwrap_or_else({closure_args} panic!(\"{{}}\", {arg_root_snippet}))"),
7570
applicability,

clippy_lints/src/methods/inefficient_to_string.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@ use rustc_errors::Applicability;
66
use rustc_hir as hir;
77
use rustc_lint::LateContext;
88
use rustc_middle::ty::{self, Ty};
9-
use rustc_span::symbol::{Symbol, sym};
9+
use rustc_span::symbol::sym;
1010

1111
use super::INEFFICIENT_TO_STRING;
1212

13-
/// Checks for the `INEFFICIENT_TO_STRING` lint
14-
pub fn check(
15-
cx: &LateContext<'_>,
16-
expr: &hir::Expr<'_>,
17-
method_name: Symbol,
18-
receiver: &hir::Expr<'_>,
19-
args: &[hir::Expr<'_>],
20-
msrv: Msrv,
21-
) {
22-
if args.is_empty()
23-
&& method_name == sym::to_string
24-
&& let Some(to_string_meth_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
13+
pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, receiver: &hir::Expr<'_>, msrv: Msrv) {
14+
if let Some(to_string_meth_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
2515
&& cx.tcx.is_diagnostic_item(sym::to_string_method, to_string_meth_did)
2616
&& let Some(args) = cx.typeck_results().node_args_opt(expr.hir_id)
2717
&& let arg_ty = cx.typeck_results().expr_ty_adjusted(receiver)

clippy_lints/src/methods/into_iter_on_ref.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,9 @@ use rustc_span::symbol::{Symbol, sym};
1010

1111
use super::INTO_ITER_ON_REF;
1212

13-
pub(super) fn check(
14-
cx: &LateContext<'_>,
15-
expr: &hir::Expr<'_>,
16-
method_span: Span,
17-
method_name: Symbol,
18-
receiver: &hir::Expr<'_>,
19-
) {
13+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, method_span: Span, receiver: &hir::Expr<'_>) {
2014
let self_ty = cx.typeck_results().expr_ty_adjusted(receiver);
2115
if let ty::Ref(..) = self_ty.kind()
22-
&& method_name == sym::into_iter
2316
&& is_trait_method(cx, expr, sym::IntoIterator)
2417
&& let Some((kind, method_name)) = ty_has_iter_method(cx, self_ty)
2518
{

clippy_lints/src/methods/mod.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4842,8 +4842,6 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
48424842
return;
48434843
}
48444844

4845-
self.check_methods(cx, expr);
4846-
48474845
match expr.kind {
48484846
ExprKind::Call(func, args) => {
48494847
from_iter_instead_of_collect::check(cx, expr, args, func);
@@ -4854,24 +4852,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
48544852
swap_with_temporary::check(cx, expr, func, args);
48554853
ip_constant::check(cx, expr, func, args);
48564854
},
4857-
ExprKind::MethodCall(method_call, receiver, args, _) => {
4858-
let method_span = method_call.ident.span;
4859-
or_fun_call::check(cx, expr, method_span, method_call.ident.name, receiver, args, self.msrv);
4860-
expect_fun_call::check(
4861-
cx,
4862-
&self.format_args,
4863-
expr,
4864-
method_span,
4865-
method_call.ident.name,
4866-
receiver,
4867-
args,
4868-
);
4869-
clone_on_copy::check(cx, expr, method_call.ident.name, receiver, args);
4870-
clone_on_ref_ptr::check(cx, expr, method_call.ident.name, receiver, args);
4871-
inefficient_to_string::check(cx, expr, method_call.ident.name, receiver, args, self.msrv);
4872-
single_char_add_str::check(cx, expr, receiver, args);
4873-
into_iter_on_ref::check(cx, expr, method_span, method_call.ident.name, receiver);
4874-
unnecessary_to_owned::check(cx, expr, method_call.ident.name, receiver, args, self.msrv);
4855+
ExprKind::MethodCall(..) => {
4856+
self.check_methods(cx, expr);
48754857
},
48764858
ExprKind::Binary(op, lhs, rhs) if op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne => {
48774859
let mut info = BinaryExprInfo {
@@ -5571,8 +5553,18 @@ impl Methods {
55715553
}
55725554
// Handle method calls whose receiver and arguments may come from expansion
55735555
if let ExprKind::MethodCall(path, recv, args, _call_span) = expr.kind {
5556+
let method_span = path.ident.span;
5557+
5558+
// Those methods do their own method name checking as they deal with multiple methods.
5559+
or_fun_call::check(cx, expr, method_span, path.ident.name, recv, args, self.msrv);
5560+
unnecessary_to_owned::check(cx, expr, path.ident.name, recv, args, self.msrv);
5561+
55745562
match (path.ident.name, args) {
5575-
(sym::expect, [_]) => {
5563+
(sym::clone, []) => {
5564+
clone_on_ref_ptr::check(cx, expr, recv);
5565+
clone_on_copy::check(cx, expr, recv);
5566+
},
5567+
(sym::expect, [arg]) => {
55765568
unwrap_expect_used::check(
55775569
cx,
55785570
expr,
@@ -5582,6 +5574,7 @@ impl Methods {
55825574
self.allow_expect_in_tests,
55835575
unwrap_expect_used::Variant::Expect,
55845576
);
5577+
expect_fun_call::check(cx, &self.format_args, expr, method_span, recv, arg);
55855578
},
55865579
(sym::expect_err, [_]) => {
55875580
unwrap_expect_used::check(
@@ -5594,6 +5587,15 @@ impl Methods {
55945587
unwrap_expect_used::Variant::Expect,
55955588
);
55965589
},
5590+
(sym::insert_str | sym::push_str, _) => {
5591+
single_char_add_str::check(cx, expr, recv, args);
5592+
},
5593+
(sym::into_iter, []) => {
5594+
into_iter_on_ref::check(cx, expr, method_span, recv);
5595+
},
5596+
(sym::to_string, []) => {
5597+
inefficient_to_string::check(cx, expr, recv, self.msrv);
5598+
},
55975599
(sym::unwrap, []) => {
55985600
unwrap_expect_used::check(
55995601
cx,

clippy_utils/src/sym.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ generate! {
176176
hidden_glob_reexports,
177177
hygiene,
178178
insert,
179+
insert_str,
179180
inspect,
180181
int_roundings,
181182
into,
@@ -259,6 +260,7 @@ generate! {
259260
powi,
260261
product,
261262
push,
263+
push_str,
262264
read,
263265
read_exact,
264266
read_line,

0 commit comments

Comments
 (0)