Skip to content

Commit 7d611d9

Browse files
committed
Auto merge of #5769 - robojumper:match_like_matches_macro, r=phansch
new lint: match_like_matches_macro Suggests using the `matches!` macro from `std` where appropriate. `redundant_pattern_matching` has been moved into the `matches` pass to allow suppressing the suggestion where `is_some` and friends are a better replacement. changelog: new lint: `match_like_matches_macro`
2 parents be88122 + 37d75da commit 7d611d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+659
-496
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,7 @@ Released 2018-09-13
15131513
[`map_unwrap_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap_or
15141514
[`match_as_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_as_ref
15151515
[`match_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_bool
1516+
[`match_like_matches_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro
15161517
[`match_on_vec_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_on_vec_items
15171518
[`match_overlapping_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_overlapping_arm
15181519
[`match_ref_pats`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_ref_pats

clippy_lints/src/comparison_chain.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,5 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
122122
}
123123

124124
fn kind_is_cmp(kind: BinOpKind) -> bool {
125-
match kind {
126-
BinOpKind::Lt | BinOpKind::Gt | BinOpKind::Eq => true,
127-
_ => false,
128-
}
125+
matches!(kind, BinOpKind::Lt | BinOpKind::Gt | BinOpKind::Eq)
129126
}

clippy_lints/src/eq_op.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,20 @@ impl<'tcx> LateLintPass<'tcx> for EqOp {
214214
}
215215

216216
fn is_valid_operator(op: BinOp) -> bool {
217-
match op.node {
217+
matches!(
218+
op.node,
218219
BinOpKind::Sub
219-
| BinOpKind::Div
220-
| BinOpKind::Eq
221-
| BinOpKind::Lt
222-
| BinOpKind::Le
223-
| BinOpKind::Gt
224-
| BinOpKind::Ge
225-
| BinOpKind::Ne
226-
| BinOpKind::And
227-
| BinOpKind::Or
228-
| BinOpKind::BitXor
229-
| BinOpKind::BitAnd
230-
| BinOpKind::BitOr => true,
231-
_ => false,
232-
}
220+
| BinOpKind::Div
221+
| BinOpKind::Eq
222+
| BinOpKind::Lt
223+
| BinOpKind::Le
224+
| BinOpKind::Gt
225+
| BinOpKind::Ge
226+
| BinOpKind::Ne
227+
| BinOpKind::And
228+
| BinOpKind::Or
229+
| BinOpKind::BitXor
230+
| BinOpKind::BitAnd
231+
| BinOpKind::BitOr
232+
)
233233
}

clippy_lints/src/escape.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
105105
_ => return false,
106106
}
107107

108-
match map.find(map.get_parent_node(id)) {
109-
Some(Node::Param(_)) => true,
110-
_ => false,
111-
}
108+
matches!(map.find(map.get_parent_node(id)), Some(Node::Param(_)))
112109
}
113110

114111
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {

clippy_lints/src/eta_reduction.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ fn get_ufcs_type_name(cx: &LateContext<'_>, method_def_id: def_id::DefId, self_a
175175
fn match_borrow_depth(lhs: Ty<'_>, rhs: Ty<'_>) -> bool {
176176
match (&lhs.kind, &rhs.kind) {
177177
(ty::Ref(_, t1, mut1), ty::Ref(_, t2, mut2)) => mut1 == mut2 && match_borrow_depth(&t1, &t2),
178-
(l, r) => match (l, r) {
179-
(ty::Ref(_, _, _), _) | (_, ty::Ref(_, _, _)) => false,
180-
(_, _) => true,
181-
},
178+
(l, r) => !matches!((l, r), (ty::Ref(_, _, _), _) | (_, ty::Ref(_, _, _))),
182179
}
183180
}
184181

clippy_lints/src/formatting.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -305,18 +305,10 @@ fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
305305
}
306306

307307
fn is_block(expr: &Expr) -> bool {
308-
if let ExprKind::Block(..) = expr.kind {
309-
true
310-
} else {
311-
false
312-
}
308+
matches!(expr.kind, ExprKind::Block(..))
313309
}
314310

315311
/// Check if the expression is an `if` or `if let`
316312
fn is_if(expr: &Expr) -> bool {
317-
if let ExprKind::If(..) = expr.kind {
318-
true
319-
} else {
320-
false
321-
}
313+
matches!(expr.kind, ExprKind::If(..))
322314
}

clippy_lints/src/functions.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,7 @@ fn is_mutated_static(cx: &LateContext<'_>, e: &hir::Expr<'_>) -> bool {
645645
use hir::ExprKind::{Field, Index, Path};
646646

647647
match e.kind {
648-
Path(ref qpath) => {
649-
if let Res::Local(_) = qpath_res(cx, qpath, e.hir_id) {
650-
false
651-
} else {
652-
true
653-
}
654-
},
648+
Path(ref qpath) => !matches!(qpath_res(cx, qpath, e.hir_id), Res::Local(_)),
655649
Field(ref inner, _) | Index(ref inner, _) => is_mutated_static(cx, inner),
656650
_ => false,
657651
}

clippy_lints/src/lib.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ mod question_mark;
277277
mod ranges;
278278
mod redundant_clone;
279279
mod redundant_field_names;
280-
mod redundant_pattern_matching;
281280
mod redundant_pub_crate;
282281
mod redundant_static_lifetimes;
283282
mod reference;
@@ -623,11 +622,13 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
623622
&matches::INFALLIBLE_DESTRUCTURING_MATCH,
624623
&matches::MATCH_AS_REF,
625624
&matches::MATCH_BOOL,
625+
&matches::MATCH_LIKE_MATCHES_MACRO,
626626
&matches::MATCH_OVERLAPPING_ARM,
627627
&matches::MATCH_REF_PATS,
628628
&matches::MATCH_SINGLE_BINDING,
629629
&matches::MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
630630
&matches::MATCH_WILD_ERR_ARM,
631+
&matches::REDUNDANT_PATTERN_MATCHING,
631632
&matches::REST_PAT_IN_FULLY_BOUND_STRUCTS,
632633
&matches::SINGLE_MATCH,
633634
&matches::SINGLE_MATCH_ELSE,
@@ -757,7 +758,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
757758
&ranges::REVERSED_EMPTY_RANGES,
758759
&redundant_clone::REDUNDANT_CLONE,
759760
&redundant_field_names::REDUNDANT_FIELD_NAMES,
760-
&redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING,
761761
&redundant_pub_crate::REDUNDANT_PUB_CRATE,
762762
&redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES,
763763
&reference::DEREF_ADDROF,
@@ -956,7 +956,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
956956
store.register_late_pass(|| box missing_doc::MissingDoc::new());
957957
store.register_late_pass(|| box missing_inline::MissingInline);
958958
store.register_late_pass(|| box if_let_some_result::OkIfLet);
959-
store.register_late_pass(|| box redundant_pattern_matching::RedundantPatternMatching);
960959
store.register_late_pass(|| box partialeq_ne_impl::PartialEqNeImpl);
961960
store.register_late_pass(|| box unused_io_amount::UnusedIoAmount);
962961
let enum_variant_size_threshold = conf.enum_variant_size_threshold;
@@ -1295,9 +1294,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12951294
LintId::of(&map_unit_fn::RESULT_MAP_UNIT_FN),
12961295
LintId::of(&matches::INFALLIBLE_DESTRUCTURING_MATCH),
12971296
LintId::of(&matches::MATCH_AS_REF),
1297+
LintId::of(&matches::MATCH_LIKE_MATCHES_MACRO),
12981298
LintId::of(&matches::MATCH_OVERLAPPING_ARM),
12991299
LintId::of(&matches::MATCH_REF_PATS),
13001300
LintId::of(&matches::MATCH_SINGLE_BINDING),
1301+
LintId::of(&matches::REDUNDANT_PATTERN_MATCHING),
13011302
LintId::of(&matches::SINGLE_MATCH),
13021303
LintId::of(&matches::WILDCARD_IN_OR_PATTERNS),
13031304
LintId::of(&mem_discriminant::MEM_DISCRIMINANT_NON_ENUM),
@@ -1387,7 +1388,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13871388
LintId::of(&ranges::REVERSED_EMPTY_RANGES),
13881389
LintId::of(&redundant_clone::REDUNDANT_CLONE),
13891390
LintId::of(&redundant_field_names::REDUNDANT_FIELD_NAMES),
1390-
LintId::of(&redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING),
13911391
LintId::of(&redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES),
13921392
LintId::of(&reference::DEREF_ADDROF),
13931393
LintId::of(&reference::REF_IN_DEREF),
@@ -1488,8 +1488,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14881488
LintId::of(&manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE),
14891489
LintId::of(&map_clone::MAP_CLONE),
14901490
LintId::of(&matches::INFALLIBLE_DESTRUCTURING_MATCH),
1491+
LintId::of(&matches::MATCH_LIKE_MATCHES_MACRO),
14911492
LintId::of(&matches::MATCH_OVERLAPPING_ARM),
14921493
LintId::of(&matches::MATCH_REF_PATS),
1494+
LintId::of(&matches::REDUNDANT_PATTERN_MATCHING),
14931495
LintId::of(&matches::SINGLE_MATCH),
14941496
LintId::of(&mem_replace::MEM_REPLACE_OPTION_WITH_NONE),
14951497
LintId::of(&mem_replace::MEM_REPLACE_WITH_DEFAULT),
@@ -1526,7 +1528,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15261528
LintId::of(&ptr::PTR_ARG),
15271529
LintId::of(&question_mark::QUESTION_MARK),
15281530
LintId::of(&redundant_field_names::REDUNDANT_FIELD_NAMES),
1529-
LintId::of(&redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING),
15301531
LintId::of(&redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES),
15311532
LintId::of(&regex::TRIVIAL_REGEX),
15321533
LintId::of(&returns::NEEDLESS_RETURN),

clippy_lints/src/lifetimes.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ fn check_fn_inner<'tcx>(
129129
}
130130

131131
let mut bounds_lts = Vec::new();
132-
let types = generics.params.iter().filter(|param| match param.kind {
133-
GenericParamKind::Type { .. } => true,
134-
_ => false,
135-
});
132+
let types = generics
133+
.params
134+
.iter()
135+
.filter(|param| matches!(param.kind, GenericParamKind::Type { .. }));
136136
for typ in types {
137137
for bound in typ.bounds {
138138
let mut visitor = RefVisitor::new(cx);
@@ -337,10 +337,10 @@ impl<'a, 'tcx> RefVisitor<'a, 'tcx> {
337337
fn collect_anonymous_lifetimes(&mut self, qpath: &QPath<'_>, ty: &Ty<'_>) {
338338
if let Some(ref last_path_segment) = last_path_segment(qpath).args {
339339
if !last_path_segment.parenthesized
340-
&& !last_path_segment.args.iter().any(|arg| match arg {
341-
GenericArg::Lifetime(_) => true,
342-
_ => false,
343-
})
340+
&& !last_path_segment
341+
.args
342+
.iter()
343+
.any(|arg| matches!(arg, GenericArg::Lifetime(_)))
344344
{
345345
let hir_id = ty.hir_id;
346346
match self.cx.qpath_res(qpath, hir_id) {

clippy_lints/src/loops.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -2091,17 +2091,11 @@ fn var_def_id(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<HirId> {
20912091
}
20922092

20932093
fn is_loop(expr: &Expr<'_>) -> bool {
2094-
match expr.kind {
2095-
ExprKind::Loop(..) => true,
2096-
_ => false,
2097-
}
2094+
matches!(expr.kind, ExprKind::Loop(..))
20982095
}
20992096

21002097
fn is_conditional(expr: &Expr<'_>) -> bool {
2101-
match expr.kind {
2102-
ExprKind::Match(..) => true,
2103-
_ => false,
2104-
}
2098+
matches!(expr.kind, ExprKind::Match(..))
21052099
}
21062100

21072101
fn is_nested(cx: &LateContext<'_>, match_expr: &Expr<'_>, iter_expr: &Expr<'_>) -> bool {

0 commit comments

Comments
 (0)