Skip to content

Commit a865d0a

Browse files
committed
Auto merge of #4649 - Lythenas:use_match_function_call, r=phansch
Use match_function_call wherever possible Move `match_function_call` to `utils` and use it wherever possible as discussed in #4635. changelog: none
2 parents 4d0e897 + 15b433a commit a865d0a

8 files changed

+45
-62
lines changed

clippy_lints/src/assertions_on_constants.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::consts::{constant, Constant};
22
use crate::utils::paths;
3-
use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, snippet_opt, span_help_and_lint};
3+
use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, snippet_opt, span_help_and_lint};
44
use if_chain::if_chain;
55
use rustc::hir::*;
66
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -145,23 +145,3 @@ fn match_assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx E
145145
}
146146
None
147147
}
148-
149-
/// Matches a function call with the given path and returns the arguments.
150-
///
151-
/// Usage:
152-
///
153-
/// ```rust,ignore
154-
/// if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
155-
/// ```
156-
fn match_function_call<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, path: &[&str]) -> Option<&'a [Expr]> {
157-
if_chain! {
158-
if let ExprKind::Call(ref fun, ref args) = expr.kind;
159-
if let ExprKind::Path(ref qpath) = fun.kind;
160-
if let Some(fun_def_id) = cx.tables.qpath_res(qpath, fun.hir_id).opt_def_id();
161-
if match_def_path(cx, fun_def_id, path);
162-
then {
163-
return Some(&args)
164-
}
165-
};
166-
None
167-
}

clippy_lints/src/explicit_write.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{is_expn_of, match_def_path, paths, resolve_node, span_lint, span_lint_and_sugg};
1+
use crate::utils::{is_expn_of, match_function_call, paths, span_lint, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc::hir::*;
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -41,12 +41,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExplicitWrite {
4141
if write_fun.ident.name == sym!(write_fmt);
4242
// match calls to std::io::stdout() / std::io::stderr ()
4343
if write_args.len() > 0;
44-
if let ExprKind::Call(ref dest_fun, _) = write_args[0].kind;
45-
if let ExprKind::Path(ref qpath) = dest_fun.kind;
46-
if let Some(dest_fun_id) = resolve_node(cx, qpath, dest_fun.hir_id).opt_def_id();
47-
if let Some(dest_name) = if match_def_path(cx, dest_fun_id, &paths::STDOUT) {
44+
if let Some(dest_name) = if match_function_call(cx, &write_args[0], &paths::STDOUT).is_some() {
4845
Some("stdout")
49-
} else if match_def_path(cx, dest_fun_id, &paths::STDERR) {
46+
} else if match_function_call(cx, &write_args[0], &paths::STDERR).is_some() {
5047
Some("stderr")
5148
} else {
5249
None

clippy_lints/src/format.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::utils::paths;
22
use crate::utils::{
3-
is_expn_of, last_path_segment, match_def_path, match_type, resolve_node, snippet, span_lint_and_then, walk_ptrs_ty,
3+
is_expn_of, last_path_segment, match_def_path, match_function_call, match_type, snippet, span_lint_and_then,
4+
walk_ptrs_ty,
45
};
56
use if_chain::if_chain;
67
use rustc::hir::*;
@@ -70,19 +71,16 @@ fn span_useless_format<T: LintContext>(cx: &T, span: Span, help: &str, mut sugg:
7071
});
7172
}
7273

73-
fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arms: &'a [Arm]) -> Option<String> {
74+
fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arms: &'tcx [Arm]) -> Option<String> {
7475
if_chain! {
7576
if let ExprKind::AddrOf(_, ref format_args) = expr.kind;
7677
if let ExprKind::Array(ref elems) = arms[0].body.kind;
7778
if elems.len() == 1;
78-
if let ExprKind::Call(ref fun, ref args) = elems[0].kind;
79-
if let ExprKind::Path(ref qpath) = fun.kind;
80-
if let Some(did) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
81-
if match_def_path(cx, did, &paths::FMT_ARGUMENTV1_NEW);
79+
if let Some(args) = match_function_call(cx, &elems[0], &paths::FMT_ARGUMENTV1_NEW);
8280
// matches `core::fmt::Display::fmt`
8381
if args.len() == 2;
8482
if let ExprKind::Path(ref qpath) = args[1].kind;
85-
if let Some(did) = resolve_node(cx, qpath, args[1].hir_id).opt_def_id();
83+
if let Some(did) = cx.tables.qpath_res(qpath, args[1].hir_id).opt_def_id();
8684
if match_def_path(cx, did, &paths::DISPLAY_FMT_METHOD);
8785
// check `(arg0,)` in match block
8886
if let PatKind::Tuple(ref pats, None) = arms[0].pat.kind;
@@ -114,11 +112,8 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
114112

115113
fn on_new_v1<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<String> {
116114
if_chain! {
117-
if let ExprKind::Call(ref fun, ref args) = expr.kind;
115+
if let Some(args) = match_function_call(cx, expr, &paths::FMT_ARGUMENTS_NEW_V1);
118116
if args.len() == 2;
119-
if let ExprKind::Path(ref qpath) = fun.kind;
120-
if let Some(did) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
121-
if match_def_path(cx, did, &paths::FMT_ARGUMENTS_NEW_V1);
122117
// Argument 1 in `new_v1()`
123118
if let ExprKind::AddrOf(_, ref arr) = args[0].kind;
124119
if let ExprKind::Array(ref pieces) = arr.kind;
@@ -144,11 +139,8 @@ fn on_new_v1<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<S
144139

145140
fn on_new_v1_fmt<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<String> {
146141
if_chain! {
147-
if let ExprKind::Call(ref fun, ref args) = expr.kind;
142+
if let Some(args) = match_function_call(cx, expr, &paths::FMT_ARGUMENTS_NEW_V1_FORMATTED);
148143
if args.len() == 3;
149-
if let ExprKind::Path(ref qpath) = fun.kind;
150-
if let Some(did) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
151-
if match_def_path(cx, did, &paths::FMT_ARGUMENTS_NEW_V1_FORMATTED);
152144
if check_unformatted(&args[2]);
153145
// Argument 1 in `new_v1_formatted()`
154146
if let ExprKind::AddrOf(_, ref arr) = args[0].kind;

clippy_lints/src/identity_conversion.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::utils::{
2-
match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then,
2+
match_def_path, match_trait_method, paths, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then,
33
};
4-
use crate::utils::{paths, resolve_node};
54
use rustc::hir::*;
65
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
76
use rustc::{declare_tool_lint, impl_lint_pass};
@@ -88,7 +87,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
8887

8988
ExprKind::Call(ref path, ref args) => {
9089
if let ExprKind::Path(ref qpath) = path.kind {
91-
if let Some(def_id) = resolve_node(cx, qpath, path.hir_id).opt_def_id() {
90+
if let Some(def_id) = cx.tables.qpath_res(qpath, path.hir_id).opt_def_id() {
9291
if match_def_path(cx, def_id, &paths::FROM_FROM) {
9392
let a = cx.tables.expr_ty(e);
9493
let b = cx.tables.expr_ty(&args[0]);

clippy_lints/src/implicit_return.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::{
22
match_def_path,
33
paths::{BEGIN_PANIC, BEGIN_PANIC_FMT},
4-
resolve_node, snippet_opt, span_lint_and_then,
4+
snippet_opt, span_lint_and_then,
55
};
66
use if_chain::if_chain;
77
use rustc::{
@@ -109,7 +109,7 @@ fn expr_match(cx: &LateContext<'_, '_>, expr: &Expr) {
109109
ExprKind::Call(expr, ..) => {
110110
if_chain! {
111111
if let ExprKind::Path(qpath) = &expr.kind;
112-
if let Some(path_def_id) = resolve_node(cx, qpath, expr.hir_id).opt_def_id();
112+
if let Some(path_def_id) = cx.tables.qpath_res(qpath, expr.hir_id).opt_def_id();
113113
if match_def_path(cx, path_def_id, &BEGIN_PANIC) ||
114114
match_def_path(cx, path_def_id, &BEGIN_PANIC_FMT);
115115
then { }

clippy_lints/src/panic_unimplemented.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, paths, resolve_node, span_lint};
1+
use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, paths, span_lint};
22
use if_chain::if_chain;
3-
use rustc::hir::ptr::P;
43
use rustc::hir::*;
54
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
65
use rustc::{declare_lint_pass, declare_tool_lint};
@@ -49,10 +48,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented {
4948
if_chain! {
5049
if let ExprKind::Block(ref block, _) = expr.kind;
5150
if let Some(ref ex) = block.expr;
52-
if let ExprKind::Call(ref fun, ref params) = ex.kind;
53-
if let ExprKind::Path(ref qpath) = fun.kind;
54-
if let Some(fun_def_id) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
55-
if match_def_path(cx, fun_def_id, &paths::BEGIN_PANIC);
51+
if let Some(params) = match_function_call(cx, ex, &paths::BEGIN_PANIC);
5652
if params.len() == 2;
5753
then {
5854
if is_expn_of(expr.span, "unimplemented").is_some() {
@@ -81,7 +77,7 @@ fn get_outer_span(expr: &Expr) -> Span {
8177
}
8278
}
8379

84-
fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext<'_, '_>) {
80+
fn match_panic(params: &[Expr], expr: &Expr, cx: &LateContext<'_, '_>) {
8581
if_chain! {
8682
if let ExprKind::Lit(ref lit) = params[0].kind;
8783
if is_direct_expn_of(expr.span, "panic").is_some();

clippy_lints/src/utils/higher.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
#![deny(clippy::missing_docs_in_private_items)]
55

6-
use crate::utils::{is_expn_of, match_def_path, match_qpath, paths, resolve_node};
6+
use crate::utils::{is_expn_of, match_def_path, match_qpath, paths};
77
use if_chain::if_chain;
88
use rustc::lint::LateContext;
99
use rustc::{hir, ty};
@@ -250,9 +250,9 @@ pub enum VecArgs<'a> {
250250
pub fn vec_macro<'e>(cx: &LateContext<'_, '_>, expr: &'e hir::Expr) -> Option<VecArgs<'e>> {
251251
if_chain! {
252252
if let hir::ExprKind::Call(ref fun, ref args) = expr.kind;
253-
if let hir::ExprKind::Path(ref path) = fun.kind;
253+
if let hir::ExprKind::Path(ref qpath) = fun.kind;
254254
if is_expn_of(fun.span, "vec").is_some();
255-
if let Some(fun_def_id) = resolve_node(cx, path, fun.hir_id).opt_def_id();
255+
if let Some(fun_def_id) = cx.tables.qpath_res(qpath, fun.hir_id).opt_def_id();
256256
then {
257257
return if match_def_path(cx, fun_def_id, &paths::VEC_FROM_ELEM) && args.len() == 2 {
258258
// `vec![elem; size]` case

clippy_lints/src/utils/mod.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,6 @@ pub fn has_drop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
358358
}
359359
}
360360

361-
/// Resolves the definition of a node from its `HirId`.
362-
pub fn resolve_node(cx: &LateContext<'_, '_>, qpath: &QPath, id: HirId) -> Res {
363-
cx.tables.qpath_res(qpath, id)
364-
}
365-
366361
/// Returns the method names and argument list of nested method call expressions that make up
367362
/// `expr`. method/span lists are sorted with the most recent call first.
368363
pub fn method_calls(expr: &Expr, max_depth: usize) -> (Vec<Symbol>, Vec<&[Expr]>, Vec<Span>) {
@@ -1085,6 +1080,30 @@ pub fn has_iter_method(cx: &LateContext<'_, '_>, probably_ref_ty: Ty<'_>) -> Opt
10851080
None
10861081
}
10871082

1083+
/// Matches a function call with the given path and returns the arguments.
1084+
///
1085+
/// Usage:
1086+
///
1087+
/// ```rust,ignore
1088+
/// if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
1089+
/// ```
1090+
pub fn match_function_call<'a, 'tcx>(
1091+
cx: &LateContext<'a, 'tcx>,
1092+
expr: &'tcx Expr,
1093+
path: &[&str],
1094+
) -> Option<&'tcx [Expr]> {
1095+
if_chain! {
1096+
if let ExprKind::Call(ref fun, ref args) = expr.kind;
1097+
if let ExprKind::Path(ref qpath) = fun.kind;
1098+
if let Some(fun_def_id) = cx.tables.qpath_res(qpath, fun.hir_id).opt_def_id();
1099+
if match_def_path(cx, fun_def_id, path);
1100+
then {
1101+
return Some(&args)
1102+
}
1103+
};
1104+
None
1105+
}
1106+
10881107
#[cfg(test)]
10891108
mod test {
10901109
use super::{trim_multiline, without_block_comments};

0 commit comments

Comments
 (0)