Skip to content

Commit 0040356

Browse files
committed
Cleanup: do not handle methods from several places
Some methods lints were handled in the `methods` module outside the `check_methods()` function.
1 parent 5b23bd4 commit 0040356

File tree

7 files changed

+39
-75
lines changed

7 files changed

+39
-75
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,27 +4,15 @@ 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.
1514
#[allow(clippy::too_many_lines)]
16-
pub(super) fn check(
17-
cx: &LateContext<'_>,
18-
expr: &Expr<'_>,
19-
method_name: Symbol,
20-
receiver: &Expr<'_>,
21-
args: &[Expr<'_>],
22-
) {
23-
let arg = if method_name == sym::clone && args.is_empty() {
24-
receiver
25-
} else {
26-
return;
27-
};
15+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
2816
if cx
2917
.typeck_results()
3018
.type_dependent_def_id(expr.hir_id)
@@ -34,10 +22,10 @@ pub(super) fn check(
3422
{
3523
return;
3624
}
37-
let arg_adjustments = cx.typeck_results().expr_adjustments(arg);
25+
let arg_adjustments = cx.typeck_results().expr_adjustments(receiver);
3826
let arg_ty = arg_adjustments
3927
.last()
40-
.map_or_else(|| cx.typeck_results().expr_ty(arg), |a| a.target);
28+
.map_or_else(|| cx.typeck_results().expr_ty(receiver), |a| a.target);
4129

4230
let ty = cx.typeck_results().expr_ty(expr);
4331
if let ty::Ref(_, inner, _) = arg_ty.kind()
@@ -76,7 +64,7 @@ pub(super) fn check(
7664
};
7765

7866
let mut app = Applicability::MachineApplicable;
79-
let snip = snippet_with_context(cx, arg.span, expr.span.ctxt(), "_", &mut app).0;
67+
let snip = snippet_with_context(cx, receiver.span, expr.span.ctxt(), "_", &mut app).0;
8068

8169
let deref_count = arg_adjustments
8270
.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: 4 additions & 6 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,12 +20,10 @@ 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>,
2524
args: &'tcx [hir::Expr<'tcx>],
2625
) {
27-
if name == sym::expect
28-
&& let [arg] = args
26+
if let [arg] = args
2927
&& let arg_root = get_arg_root(cx, arg)
3028
&& contains_call(cx, arg_root)
3129
&& !contains_return(arg_root)
@@ -54,7 +52,7 @@ pub(super) fn check<'tcx>(
5452
cx,
5553
EXPECT_FUN_CALL,
5654
span_replace_word,
57-
format!("function call inside of `{name}`"),
55+
"function call inside of `expect`",
5856
"try",
5957
format!("unwrap_or_else({closure_args} panic!({sugg}))"),
6058
applicability,
@@ -69,7 +67,7 @@ pub(super) fn check<'tcx>(
6967
cx,
7068
EXPECT_FUN_CALL,
7169
span_replace_word,
72-
format!("function call inside of `{name}`"),
70+
"function call inside of `expect`",
7371
"try",
7472
format!("unwrap_or_else({closure_args} panic!(\"{{}}\", {arg_root_snippet}))"),
7573
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: 22 additions & 20 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 {
@@ -5567,7 +5549,17 @@ impl Methods {
55675549
}
55685550
// Handle method calls whose receiver and arguments may come from expansion
55695551
if let ExprKind::MethodCall(path, recv, args, _call_span) = expr.kind {
5552+
let method_span = path.ident.span;
5553+
5554+
// Those methods do their own method name checking as they deal with multiple methods.
5555+
or_fun_call::check(cx, expr, method_span, path.ident.name, recv, args, self.msrv);
5556+
unnecessary_to_owned::check(cx, expr, path.ident.name, recv, args, self.msrv);
5557+
55705558
match (path.ident.name, args) {
5559+
(sym::clone, []) => {
5560+
clone_on_ref_ptr::check(cx, expr, recv);
5561+
clone_on_copy::check(cx, expr, recv);
5562+
},
55715563
(sym::expect, [_]) => {
55725564
unwrap_expect_used::check(
55735565
cx,
@@ -5578,6 +5570,7 @@ impl Methods {
55785570
self.allow_expect_in_tests,
55795571
unwrap_expect_used::Variant::Expect,
55805572
);
5573+
expect_fun_call::check(cx, &self.format_args, expr, method_span, recv, args);
55815574
},
55825575
(sym::expect_err, [_]) => {
55835576
unwrap_expect_used::check(
@@ -5590,6 +5583,15 @@ impl Methods {
55905583
unwrap_expect_used::Variant::Expect,
55915584
);
55925585
},
5586+
(sym::insert_str | sym::push_str, _) => {
5587+
single_char_add_str::check(cx, expr, recv, args);
5588+
},
5589+
(sym::into_iter, []) => {
5590+
into_iter_on_ref::check(cx, expr, method_span, recv);
5591+
},
5592+
(sym::to_string, []) => {
5593+
inefficient_to_string::check(cx, expr, recv, self.msrv);
5594+
},
55935595
(sym::unwrap, []) => {
55945596
unwrap_expect_used::check(
55955597
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)