Skip to content

Commit 586a993

Browse files
committed
Auto merge of #7049 - Jarcho:remove_match_path, r=camsteffen
Remove `match_path` and `match_qpath` The only remaining usage is the `author` lint, so the functions are left in for now. The test result for `repl_uninit` changed, the lint was broken before. The `collapsible_span_lint_calls` and `match_type_on_diag_item` tests have been changed. Both lints were broken when utils was extracted into it's own crate. `match_type_on_diag_item` isn't quite fixed, but it's at least less broken. changelog: None
2 parents 52c8c9c + f6c5d8d commit 586a993

34 files changed

+269
-335
lines changed

clippy_lints/src/assertions_on_constants.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ fn match_assert_with_message<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>)
127127
_ => &block.expr,
128128
};
129129
// function call
130-
if let Some(args) = match_panic_call(cx, begin_panic_call);
131-
if args.len() == 1;
130+
if let Some(arg) = match_panic_call(cx, begin_panic_call);
132131
// bind the second argument of the `assert!` macro if it exists
133-
if let panic_message = snippet_opt(cx, args[0].span);
132+
if let panic_message = snippet_opt(cx, arg.span);
134133
// second argument of begin_panic is irrelevant
135134
// as is the second match arm
136135
then {

clippy_lints/src/excessive_bools.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::{in_macro, match_path_ast};
2+
use clippy_utils::in_macro;
33
use rustc_ast::ast::{AssocItemKind, Extern, FnKind, FnSig, ImplKind, Item, ItemKind, TraitKind, Ty, TyKind};
44
use rustc_lint::{EarlyContext, EarlyLintPass};
55
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -126,7 +126,9 @@ impl_lint_pass!(ExcessiveBools => [STRUCT_EXCESSIVE_BOOLS, FN_PARAMS_EXCESSIVE_B
126126

127127
fn is_bool_ty(ty: &Ty) -> bool {
128128
if let TyKind::Path(None, path) = &ty.kind {
129-
return match_path_ast(path, &["bool"]);
129+
if let [name] = path.segments.as_slice() {
130+
return name.ident.name == sym::bool;
131+
}
130132
}
131133
false
132134
}

clippy_lints/src/implicit_hasher.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
2222
use clippy_utils::paths;
2323
use clippy_utils::source::{snippet, snippet_opt};
2424
use clippy_utils::ty::is_type_diagnostic_item;
25-
use clippy_utils::{differing_macro_contexts, match_path};
25+
use clippy_utils::{differing_macro_contexts, match_def_path};
2626

2727
declare_clippy_lint! {
2828
/// **What it does:** Checks for public `impl` or `fn` missing generalization
@@ -333,12 +333,13 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
333333
if let ExprKind::Call(fun, args) = e.kind;
334334
if let ExprKind::Path(QPath::TypeRelative(ty, method)) = fun.kind;
335335
if let TyKind::Path(QPath::Resolved(None, ty_path)) = ty.kind;
336+
if let Some(ty_did) = ty_path.res.opt_def_id();
336337
then {
337338
if !TyS::same_type(self.target.ty(), self.maybe_typeck_results.unwrap().expr_ty(e)) {
338339
return;
339340
}
340341

341-
if match_path(ty_path, &paths::HASHMAP) {
342+
if match_def_path(self.cx, ty_did, &paths::HASHMAP) {
342343
if method.ident.name == sym::new {
343344
self.suggestions
344345
.insert(e.span, "HashMap::default()".to_string());
@@ -351,7 +352,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
351352
),
352353
);
353354
}
354-
} else if match_path(ty_path, &paths::HASHSET) {
355+
} else if match_def_path(self.cx, ty_did, &paths::HASHSET) {
355356
if method.ident.name == sym::new {
356357
self.suggestions
357358
.insert(e.span, "HashSet::default()".to_string());

clippy_lints/src/implicit_saturating_sub.rs

+31-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::{in_macro, match_qpath, SpanlessEq};
2+
use clippy_utils::{in_macro, SpanlessEq};
33
use if_chain::if_chain;
44
use rustc_ast::ast::LitKind;
55
use rustc_errors::Applicability;
6-
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, StmtKind};
6+
use rustc_hir::{lang_items::LangItem, BinOpKind, Expr, ExprKind, QPath, StmtKind};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99

@@ -87,7 +87,13 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
8787

8888
// Get the variable name
8989
let var_name = ares_path.segments[0].ident.name.as_str();
90-
const INT_TYPES: [&str; 5] = ["i8", "i16", "i32", "i64", "i128"];
90+
const INT_TYPES: [LangItem; 5] = [
91+
LangItem::I8,
92+
LangItem::I16,
93+
LangItem::I32,
94+
LangItem::I64,
95+
LangItem::Isize
96+
];
9197

9298
match cond_num_val.kind {
9399
ExprKind::Lit(ref cond_lit) => {
@@ -99,17 +105,30 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
99105
};
100106
}
101107
},
102-
ExprKind::Path(ref cond_num_path) => {
103-
if INT_TYPES.iter().any(|int_type| match_qpath(cond_num_path, &[int_type, "MIN"])) {
104-
print_lint_and_sugg(cx, &var_name, expr);
105-
};
108+
ExprKind::Path(QPath::TypeRelative(_, name)) => {
109+
if_chain! {
110+
if name.ident.as_str() == "MIN";
111+
if let Some(const_id) = cx.typeck_results().type_dependent_def_id(cond_num_val.hir_id);
112+
if let Some(impl_id) = cx.tcx.impl_of_method(const_id);
113+
let mut int_ids = INT_TYPES.iter().filter_map(|&ty| cx.tcx.lang_items().require(ty).ok());
114+
if int_ids.any(|int_id| int_id == impl_id);
115+
then {
116+
print_lint_and_sugg(cx, &var_name, expr)
117+
}
118+
}
106119
},
107-
ExprKind::Call(func, _) => {
108-
if let ExprKind::Path(ref cond_num_path) = func.kind {
109-
if INT_TYPES.iter().any(|int_type| match_qpath(cond_num_path, &[int_type, "min_value"])) {
110-
print_lint_and_sugg(cx, &var_name, expr);
120+
ExprKind::Call(func, []) => {
121+
if_chain! {
122+
if let ExprKind::Path(QPath::TypeRelative(_, name)) = func.kind;
123+
if name.ident.as_str() == "min_value";
124+
if let Some(func_id) = cx.typeck_results().type_dependent_def_id(func.hir_id);
125+
if let Some(impl_id) = cx.tcx.impl_of_method(func_id);
126+
let mut int_ids = INT_TYPES.iter().filter_map(|&ty| cx.tcx.lang_items().require(ty).ok());
127+
if int_ids.any(|int_id| int_id == impl_id);
128+
then {
129+
print_lint_and_sugg(cx, &var_name, expr)
111130
}
112-
};
131+
}
113132
},
114133
_ => (),
115134
}

clippy_lints/src/infinite_iter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::ty::{implements_trait, match_type};
3-
use clippy_utils::{get_trait_def_id, higher, match_qpath, paths};
3+
use clippy_utils::{get_trait_def_id, higher, is_qpath_def_path, paths};
44
use rustc_hir::{BorrowKind, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -163,7 +163,7 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
163163
ExprKind::Box(e) | ExprKind::AddrOf(BorrowKind::Ref, _, e) => is_infinite(cx, e),
164164
ExprKind::Call(path, _) => {
165165
if let ExprKind::Path(ref qpath) = path.kind {
166-
match_qpath(qpath, &paths::REPEAT).into()
166+
is_qpath_def_path(cx, qpath, path.hir_id, &paths::ITER_REPEAT).into()
167167
} else {
168168
Finite
169169
}

clippy_lints/src/map_identity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::ty::is_type_diagnostic_item;
3-
use clippy_utils::{is_adjusted, is_trait_method, match_path, match_var, paths, remove_blocks};
3+
use clippy_utils::{is_adjusted, is_qpath_def_path, is_trait_method, match_var, paths, remove_blocks};
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Body, Expr, ExprKind, Pat, PatKind, QPath, StmtKind};
@@ -80,7 +80,7 @@ fn get_map_argument<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a
8080
fn is_expr_identity_function(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
8181
match expr.kind {
8282
ExprKind::Closure(_, _, body_id, _, _) => is_body_identity_function(cx, cx.tcx.hir().body(body_id)),
83-
ExprKind::Path(QPath::Resolved(_, path)) => match_path(path, &paths::STD_CONVERT_IDENTITY),
83+
ExprKind::Path(ref path) => is_qpath_def_path(cx, path, expr.hir_id, &paths::CONVERT_IDENTITY),
8484
_ => false,
8585
}
8686
}

clippy_lints/src/matches.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ mod redundant_pattern_match {
17011701
use super::REDUNDANT_PATTERN_MATCHING;
17021702
use clippy_utils::diagnostics::span_lint_and_then;
17031703
use clippy_utils::source::snippet;
1704-
use clippy_utils::{is_lang_ctor, is_trait_method, match_qpath, paths};
1704+
use clippy_utils::{is_lang_ctor, is_qpath_def_path, is_trait_method, paths};
17051705
use if_chain::if_chain;
17061706
use rustc_ast::ast::LitKind;
17071707
use rustc_errors::Applicability;
@@ -1735,8 +1735,8 @@ mod redundant_pattern_match {
17351735
kind = &inner.kind;
17361736
}
17371737
let good_method = match kind {
1738-
PatKind::TupleStruct(ref path, patterns, _) if patterns.len() == 1 => {
1739-
if let PatKind::Wild = patterns[0].kind {
1738+
PatKind::TupleStruct(ref path, [sub_pat], _) => {
1739+
if let PatKind::Wild = sub_pat.kind {
17401740
if is_lang_ctor(cx, path, ResultOk) {
17411741
"is_ok()"
17421742
} else if is_lang_ctor(cx, path, ResultErr) {
@@ -1745,9 +1745,9 @@ mod redundant_pattern_match {
17451745
"is_some()"
17461746
} else if is_lang_ctor(cx, path, PollReady) {
17471747
"is_ready()"
1748-
} else if match_qpath(path, &paths::IPADDR_V4) {
1748+
} else if is_qpath_def_path(cx, path, sub_pat.hir_id, &paths::IPADDR_V4) {
17491749
"is_ipv4()"
1750-
} else if match_qpath(path, &paths::IPADDR_V6) {
1750+
} else if is_qpath_def_path(cx, path, sub_pat.hir_id, &paths::IPADDR_V6) {
17511751
"is_ipv6()"
17521752
} else {
17531753
return;
@@ -1821,6 +1821,7 @@ mod redundant_pattern_match {
18211821
) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
18221822
if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].kind, &patterns_right[0].kind) {
18231823
find_good_method_for_match(
1824+
cx,
18241825
arms,
18251826
path_left,
18261827
path_right,
@@ -1831,6 +1832,7 @@ mod redundant_pattern_match {
18311832
)
18321833
.or_else(|| {
18331834
find_good_method_for_match(
1835+
cx,
18341836
arms,
18351837
path_left,
18361838
path_right,
@@ -1850,6 +1852,7 @@ mod redundant_pattern_match {
18501852
{
18511853
if let PatKind::Wild = patterns[0].kind {
18521854
find_good_method_for_match(
1855+
cx,
18531856
arms,
18541857
path_left,
18551858
path_right,
@@ -1860,6 +1863,7 @@ mod redundant_pattern_match {
18601863
)
18611864
.or_else(|| {
18621865
find_good_method_for_match(
1866+
cx,
18631867
arms,
18641868
path_left,
18651869
path_right,
@@ -1900,7 +1904,9 @@ mod redundant_pattern_match {
19001904
}
19011905
}
19021906

1907+
#[allow(clippy::too_many_arguments)]
19031908
fn find_good_method_for_match<'a>(
1909+
cx: &LateContext<'_>,
19041910
arms: &[Arm<'_>],
19051911
path_left: &QPath<'_>,
19061912
path_right: &QPath<'_>,
@@ -1909,9 +1915,13 @@ mod redundant_pattern_match {
19091915
should_be_left: &'a str,
19101916
should_be_right: &'a str,
19111917
) -> Option<&'a str> {
1912-
let body_node_pair = if match_qpath(path_left, expected_left) && match_qpath(path_right, expected_right) {
1918+
let body_node_pair = if is_qpath_def_path(cx, path_left, arms[0].pat.hir_id, expected_left)
1919+
&& is_qpath_def_path(cx, path_right, arms[1].pat.hir_id, expected_right)
1920+
{
19131921
(&(*arms[0].body).kind, &(*arms[1].body).kind)
1914-
} else if match_qpath(path_right, expected_left) && match_qpath(path_left, expected_right) {
1922+
} else if is_qpath_def_path(cx, path_right, arms[1].pat.hir_id, expected_left)
1923+
&& is_qpath_def_path(cx, path_left, arms[0].pat.hir_id, expected_right)
1924+
{
19151925
(&(*arms[1].body).kind, &(*arms[0].body).kind)
19161926
} else {
19171927
return None;

clippy_lints/src/methods/filter_map_identity.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::{is_trait_method, match_qpath, path_to_local_id, paths};
2+
use clippy_utils::{is_expr_path_def_path, is_trait_method, path_to_local_id, paths};
33
use if_chain::if_chain;
44
use rustc_errors::Applicability;
55
use rustc_hir as hir;
@@ -33,14 +33,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, filter_map_arg:
3333
}
3434
}
3535

36-
if_chain! {
37-
if let hir::ExprKind::Path(ref qpath) = filter_map_arg.kind;
38-
39-
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
40-
41-
then {
42-
apply_lint("called `filter_map(std::convert::identity)` on an `Iterator`");
43-
}
36+
if is_expr_path_def_path(cx, filter_map_arg, &paths::CONVERT_IDENTITY) {
37+
apply_lint("called `filter_map(std::convert::identity)` on an `Iterator`");
4438
}
4539
}
4640
}

clippy_lints/src/methods/flat_map_identity.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::{is_trait_method, match_qpath, paths};
2+
use clippy_utils::{is_expr_path_def_path, is_trait_method, paths};
33
use if_chain::if_chain;
44
use rustc_errors::Applicability;
55
use rustc_hir as hir;
@@ -16,8 +16,6 @@ pub(super) fn check<'tcx>(
1616
flat_map_span: Span,
1717
) {
1818
if is_trait_method(cx, expr, sym::Iterator) {
19-
let arg_node = &flat_map_arg.kind;
20-
2119
let apply_lint = |message: &str| {
2220
span_lint_and_sugg(
2321
cx,
@@ -31,8 +29,8 @@ pub(super) fn check<'tcx>(
3129
};
3230

3331
if_chain! {
34-
if let hir::ExprKind::Closure(_, _, body_id, _, _) = arg_node;
35-
let body = cx.tcx.hir().body(*body_id);
32+
if let hir::ExprKind::Closure(_, _, body_id, _, _) = flat_map_arg.kind;
33+
let body = cx.tcx.hir().body(body_id);
3634

3735
if let hir::PatKind::Binding(_, _, binding_ident, _) = body.params[0].pat.kind;
3836
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = body.value.kind;
@@ -45,14 +43,8 @@ pub(super) fn check<'tcx>(
4543
}
4644
}
4745

48-
if_chain! {
49-
if let hir::ExprKind::Path(ref qpath) = arg_node;
50-
51-
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
52-
53-
then {
54-
apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`");
55-
}
46+
if is_expr_path_def_path(cx, flat_map_arg, &paths::CONVERT_IDENTITY) {
47+
apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`");
5648
}
5749
}
5850
}

clippy_lints/src/methods/from_iter_instead_of_collect.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::ty::implements_trait;
3-
use clippy_utils::{get_trait_def_id, match_qpath, paths, sugg};
3+
use clippy_utils::{is_expr_path_def_path, paths, sugg};
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
use rustc_hir as hir;
7-
use rustc_hir::ExprKind;
87
use rustc_lint::{LateContext, LintContext};
98
use rustc_middle::ty::Ty;
109
use rustc_span::sym;
1110

1211
use super::FROM_ITER_INSTEAD_OF_COLLECT;
1312

14-
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>], func_kind: &ExprKind<'_>) {
13+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>], func: &hir::Expr<'_>) {
1514
if_chain! {
16-
if let hir::ExprKind::Path(path) = func_kind;
17-
if match_qpath(path, &["from_iter"]);
15+
if is_expr_path_def_path(cx, func, &paths::FROM_ITERATOR_METHOD);
1816
let ty = cx.typeck_results().expr_ty(expr);
1917
let arg_ty = cx.typeck_results().expr_ty(&args[0]);
20-
if let Some(from_iter_id) = get_trait_def_id(cx, &paths::FROM_ITERATOR);
2118
if let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator);
2219

23-
if implements_trait(cx, ty, from_iter_id, &[]) && implements_trait(cx, arg_ty, iter_id, &[]);
20+
if implements_trait(cx, arg_ty, iter_id, &[]);
2421
then {
2522
// `expr` implements `FromIterator` trait
2623
let iter_expr = sugg::Sugg::hir(cx, &args[0], "..").maybe_par();

clippy_lints/src/methods/manual_saturating_arithmetic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::match_qpath;
2+
use clippy_utils::is_qpath_def_path;
33
use clippy_utils::source::snippet_with_applicability;
44
use if_chain::if_chain;
55
use rustc_ast::ast;
@@ -94,11 +94,11 @@ fn is_min_or_max<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) -> Option<M
9494

9595
// `std::T::MAX` `std::T::MIN` constants
9696
if let hir::ExprKind::Path(path) = &expr.kind {
97-
if match_qpath(path, &["core", &ty_str, "MAX"][..]) {
97+
if is_qpath_def_path(cx, path, expr.hir_id, &["core", &ty_str, "MAX"][..]) {
9898
return Some(MinMax::Max);
9999
}
100100

101-
if match_qpath(path, &["core", &ty_str, "MIN"][..]) {
101+
if is_qpath_def_path(cx, path, expr.hir_id, &["core", &ty_str, "MIN"][..]) {
102102
return Some(MinMax::Min);
103103
}
104104
}

clippy_lints/src/methods/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17081708

17091709
match expr.kind {
17101710
hir::ExprKind::Call(func, args) => {
1711-
from_iter_instead_of_collect::check(cx, expr, args, &func.kind);
1711+
from_iter_instead_of_collect::check(cx, expr, args, func);
17121712
},
17131713
hir::ExprKind::MethodCall(method_call, ref method_span, args, _) => {
17141714
or_fun_call::check(cx, expr, *method_span, &method_call.ident.as_str(), args);

0 commit comments

Comments
 (0)