Skip to content

Commit 2b5257e

Browse files
committed
Remove method_call! macro
1 parent 0eff589 commit 2b5257e

File tree

1 file changed

+16
-25
lines changed
  • clippy_lints/src/methods

1 file changed

+16
-25
lines changed

clippy_lints/src/methods/mod.rs

+16-25
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ use rustc_middle::lint::in_external_macro;
8080
use rustc_middle::ty::{self, TraitRef, Ty, TyS};
8181
use rustc_semver::RustcVersion;
8282
use rustc_session::{declare_tool_lint, impl_lint_pass};
83-
use rustc_span::symbol::Symbol;
8483
use rustc_span::{sym, Span};
8584
use rustc_typeck::hir_ty_to_ty;
8685

@@ -1997,24 +1996,16 @@ impl_lint_pass!(Methods => [
19971996
]);
19981997

19991998
/// Extracts a method call name, args, and `Span` of the method name.
2000-
fn method_call<'tcx>(recv: &'tcx hir::Expr<'tcx>) -> Option<(Symbol, &'tcx [hir::Expr<'tcx>], Span)> {
1999+
fn method_call<'tcx>(recv: &'tcx hir::Expr<'tcx>) -> Option<(&'tcx str, &'tcx [hir::Expr<'tcx>], Span)> {
20012000
if let ExprKind::MethodCall(path, span, args, _) = recv.kind {
20022001
if !args.iter().any(|e| e.span.from_expansion()) {
2003-
return Some((path.ident.name, args, span));
2002+
let name = path.ident.name.as_str();
2003+
return Some((name, args, span));
20042004
}
20052005
}
20062006
None
20072007
}
20082008

2009-
/// Same as `method_call` but the `Symbol` is dereferenced into a temporary `&str`
2010-
macro_rules! method_call {
2011-
($expr:expr) => {
2012-
method_call($expr)
2013-
.as_ref()
2014-
.map(|&(ref name, args, span)| (name.as_str(), args, span))
2015-
};
2016-
}
2017-
20182009
impl<'tcx> LateLintPass<'tcx> for Methods {
20192010
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
20202011
if expr.span.from_expansion() {
@@ -2217,7 +2208,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
22172208

22182209
#[allow(clippy::too_many_lines)]
22192210
fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Option<&RustcVersion>) {
2220-
if let Some((name, [recv, args @ ..], span)) = method_call!(expr) {
2211+
if let Some((name, [recv, args @ ..], span)) = method_call(expr) {
22212212
match (name, args) {
22222213
("add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub", [_arg]) => {
22232214
zst_offset::check(cx, expr, recv);
@@ -2233,7 +2224,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
22332224
("as_ref", []) => useless_asref::check(cx, expr, "as_ref", recv),
22342225
("assume_init", []) => uninit_assumed_init::check(cx, expr, recv),
22352226
("cloned", []) => cloned_instead_of_copied::check(cx, expr, recv, span, msrv),
2236-
("collect", []) => match method_call!(recv) {
2227+
("collect", []) => match method_call(recv) {
22372228
Some((name @ ("cloned" | "copied"), [recv2], _)) => {
22382229
iter_cloned_collect::check(cx, name, expr, recv2);
22392230
},
@@ -2247,14 +2238,14 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
22472238
},
22482239
_ => {},
22492240
},
2250-
("count", []) => match method_call!(recv) {
2241+
("count", []) => match method_call(recv) {
22512242
Some((name @ ("into_iter" | "iter" | "iter_mut"), [recv2], _)) => {
22522243
iter_count::check(cx, expr, recv2, name);
22532244
},
22542245
Some(("map", [_, arg], _)) => suspicious_map::check(cx, expr, recv, arg),
22552246
_ => {},
22562247
},
2257-
("expect", [_]) => match method_call!(recv) {
2248+
("expect", [_]) => match method_call(recv) {
22582249
Some(("ok", [recv], _)) => ok_expect::check(cx, expr, recv),
22592250
_ => expect_used::check(cx, expr, recv),
22602251
},
@@ -2271,13 +2262,13 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
22712262
flat_map_option::check(cx, expr, arg, span);
22722263
},
22732264
("flatten", []) => {
2274-
if let Some(("map", [recv, map_arg], _)) = method_call!(recv) {
2265+
if let Some(("map", [recv, map_arg], _)) = method_call(recv) {
22752266
map_flatten::check(cx, expr, recv, map_arg);
22762267
}
22772268
},
22782269
("fold", [init, acc]) => unnecessary_fold::check(cx, expr, init, acc, span),
22792270
("for_each", [_]) => {
2280-
if let Some(("inspect", [_, _], span2)) = method_call!(recv) {
2271+
if let Some(("inspect", [_, _], span2)) = method_call(recv) {
22812272
inspect_for_each::check(cx, expr, span2);
22822273
}
22832274
},
@@ -2286,7 +2277,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
22862277
("is_none", []) => check_is_some_is_none(cx, expr, recv, false),
22872278
("is_some", []) => check_is_some_is_none(cx, expr, recv, true),
22882279
("map", [m_arg]) => {
2289-
if let Some((name, [recv2, args @ ..], span2)) = method_call!(recv) {
2280+
if let Some((name, [recv2, args @ ..], span2)) = method_call(recv) {
22902281
match (name, args) {
22912282
("as_mut", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, true, msrv),
22922283
("as_ref", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, false, msrv),
@@ -2301,7 +2292,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
23012292
},
23022293
("map_or", [def, map]) => option_map_or_none::check(cx, expr, recv, def, map),
23032294
("next", []) => {
2304-
if let Some((name, [recv, args @ ..], _)) = method_call!(recv) {
2295+
if let Some((name, [recv, args @ ..], _)) = method_call(recv) {
23052296
match (name, args) {
23062297
("filter", [arg]) => filter_next::check(cx, expr, recv, arg),
23072298
("filter_map", [arg]) => filter_map_next::check(cx, expr, recv, arg, msrv),
@@ -2312,7 +2303,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
23122303
}
23132304
}
23142305
},
2315-
("nth", [n_arg]) => match method_call!(recv) {
2306+
("nth", [n_arg]) => match method_call(recv) {
23162307
Some(("bytes", [recv2], _)) => bytes_nth::check(cx, expr, recv2, n_arg),
23172308
Some(("iter", [recv2], _)) => iter_nth::check(cx, expr, recv2, recv, n_arg, false),
23182309
Some(("iter_mut", [recv2], _)) => iter_nth::check(cx, expr, recv2, recv, n_arg, true),
@@ -2344,12 +2335,12 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
23442335
("to_os_string" | "to_owned" | "to_path_buf" | "to_vec", []) => {
23452336
implicit_clone::check(cx, name, expr, recv, span);
23462337
},
2347-
("unwrap", []) => match method_call!(recv) {
2338+
("unwrap", []) => match method_call(recv) {
23482339
Some(("get", [recv, get_arg], _)) => get_unwrap::check(cx, expr, recv, get_arg, false),
23492340
Some(("get_mut", [recv, get_arg], _)) => get_unwrap::check(cx, expr, recv, get_arg, true),
23502341
_ => unwrap_used::check(cx, expr, recv),
23512342
},
2352-
("unwrap_or", [u_arg]) => match method_call!(recv) {
2343+
("unwrap_or", [u_arg]) => match method_call(recv) {
23532344
Some((arith @ ("checked_add" | "checked_sub" | "checked_mul"), [lhs, rhs], _)) => {
23542345
manual_saturating_arithmetic::check(cx, expr, lhs, rhs, u_arg, &arith["checked_".len()..]);
23552346
},
@@ -2358,7 +2349,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
23582349
},
23592350
_ => {},
23602351
},
2361-
("unwrap_or_else", [u_arg]) => match method_call!(recv) {
2352+
("unwrap_or_else", [u_arg]) => match method_call(recv) {
23622353
Some(("map", [recv, map_arg], _)) if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, msrv) => {},
23632354
_ => {
23642355
unwrap_or_else_default::check(cx, expr, recv, u_arg);
@@ -2371,7 +2362,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
23712362
}
23722363

23732364
fn check_is_some_is_none(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, is_some: bool) {
2374-
if let Some((name @ ("find" | "position" | "rposition"), [f_recv, arg], span)) = method_call!(recv) {
2365+
if let Some((name @ ("find" | "position" | "rposition"), [f_recv, arg], span)) = method_call(recv) {
23752366
search_is_some::check(cx, expr, name, is_some, f_recv, arg, recv, span);
23762367
}
23772368
}

0 commit comments

Comments
 (0)