Skip to content

Commit 982c51e

Browse files
committed
arm.pats -> arm.pat
1 parent 68c26b3 commit 982c51e

18 files changed

+137
-172
lines changed

clippy_lints/src/cognitive_complexity.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ impl<'tcx> Visitor<'tcx> for CCHelper {
112112
walk_expr(self, e);
113113
match e.node {
114114
ExprKind::Match(_, ref arms, _) => {
115-
let arms_n: u64 = arms.iter().map(|arm| arm.pats.len() as u64).sum();
116-
if arms_n > 1 {
115+
if arms.len() > 1 {
117116
self.cc += 1;
118117
}
119118
self.cc += arms.iter().filter(|arm| arm.guard.is_some()).count() as u64;

clippy_lints/src/copies.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr) {
193193
(min_index..=max_index).all(|index| arms[index].guard.is_none()) &&
194194
SpanlessEq::new(cx).eq_expr(&lhs.body, &rhs.body) &&
195195
// all patterns should have the same bindings
196-
same_bindings(cx, &bindings(cx, &lhs.pats[0]), &bindings(cx, &rhs.pats[0]))
196+
same_bindings(cx, &bindings(cx, &lhs.pat), &bindings(cx, &rhs.pat))
197197
};
198198

199199
let indexed_arms: Vec<(usize, &Arm)> = arms.iter().enumerate().collect();
@@ -213,27 +213,22 @@ fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr) {
213213
// span for the whole pattern, the suggestion is only shown when there is only
214214
// one pattern. The user should know about `|` if they are already using it…
215215

216-
if i.pats.len() == 1 && j.pats.len() == 1 {
217-
let lhs = snippet(cx, i.pats[0].span, "<pat1>");
218-
let rhs = snippet(cx, j.pats[0].span, "<pat2>");
219-
220-
if let PatKind::Wild = j.pats[0].node {
221-
// if the last arm is _, then i could be integrated into _
222-
// note that i.pats[0] cannot be _, because that would mean that we're
223-
// hiding all the subsequent arms, and rust won't compile
224-
db.span_note(
225-
i.body.span,
226-
&format!(
227-
"`{}` has the same arm body as the `_` wildcard, consider removing it`",
228-
lhs
229-
),
230-
);
231-
} else {
232-
db.span_help(
233-
i.pats[0].span,
234-
&format!("consider refactoring into `{} | {}`", lhs, rhs),
235-
);
236-
}
216+
let lhs = snippet(cx, i.pat.span, "<pat1>");
217+
let rhs = snippet(cx, j.pat.span, "<pat2>");
218+
219+
if let PatKind::Wild = j.pat.node {
220+
// if the last arm is _, then i could be integrated into _
221+
// note that i.pat cannot be _, because that would mean that we're
222+
// hiding all the subsequent arms, and rust won't compile
223+
db.span_note(
224+
i.body.span,
225+
&format!(
226+
"`{}` has the same arm body as the `_` wildcard, consider removing it`",
227+
lhs
228+
),
229+
);
230+
} else {
231+
db.span_help(i.pat.span, &format!("consider refactoring into `{} | {}`", lhs, rhs));
237232
}
238233
},
239234
);

clippy_lints/src/format.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
8484
if let ExprKind::Path(ref qpath) = args[1].node;
8585
if let Some(did) = resolve_node(cx, qpath, args[1].hir_id).opt_def_id();
8686
if match_def_path(cx, did, &paths::DISPLAY_FMT_METHOD);
87-
if arms[0].pats.len() == 1;
8887
// check `(arg0,)` in match block
89-
if let PatKind::Tuple(ref pats, None) = arms[0].pats[0].node;
88+
if let PatKind::Tuple(ref pats, None) = arms[0].pat.node;
9089
if pats.len() == 1;
9190
then {
9291
let ty = walk_ptrs_ty(cx.tables.pat_ty(&pats[0]));

clippy_lints/src/infallible_destructuring_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InfallibleDestructingMatch {
4747
if_chain! {
4848
if let Some(ref expr) = local.init;
4949
if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.node;
50-
if arms.len() == 1 && arms[0].pats.len() == 1 && arms[0].guard.is_none();
51-
if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pats[0].node;
50+
if arms.len() == 1 && arms[0].guard.is_none();
51+
if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pat.node;
5252
if args.len() == 1;
5353
if let Some(arg) = get_arg_name(&args[0]);
5454
let body = remove_blocks(&arms[0].body);

clippy_lints/src/loops.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
517517
match *source {
518518
MatchSource::Normal | MatchSource::IfLetDesugar { .. } => {
519519
if arms.len() == 2
520-
&& arms[0].pats.len() == 1
521520
&& arms[0].guard.is_none()
522-
&& arms[1].pats.len() == 1
523521
&& arms[1].guard.is_none()
524522
&& is_simple_break_expr(&arms[1].body)
525523
{
@@ -541,7 +539,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
541539
"try",
542540
format!(
543541
"while let {} = {} {{ .. }}",
544-
snippet_with_applicability(cx, arms[0].pats[0].span, "..", &mut applicability),
542+
snippet_with_applicability(cx, arms[0].pat.span, "..", &mut applicability),
545543
snippet_with_applicability(cx, matchexpr.span, "..", &mut applicability),
546544
),
547545
applicability,
@@ -554,7 +552,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
554552
}
555553
}
556554
if let ExprKind::Match(ref match_expr, ref arms, MatchSource::WhileLetDesugar) = expr.node {
557-
let pat = &arms[0].pats[0].node;
555+
let pat = &arms[0].pat.node;
558556
if let (
559557
&PatKind::TupleStruct(ref qpath, ref pat_args, _),
560558
&ExprKind::MethodCall(ref method_path, _, ref method_args),

clippy_lints/src/matches.rs

+32-51
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc::{declare_lint_pass, declare_tool_lint};
1414
use rustc_errors::Applicability;
1515
use std::cmp::Ordering;
1616
use std::collections::Bound;
17-
use std::ops::Deref;
1817
use syntax::ast::LitKind;
1918
use syntax::source_map::Span;
2019

@@ -255,9 +254,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches {
255254

256255
#[rustfmt::skip]
257256
fn check_single_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: &Expr) {
258-
if arms.len() == 2 &&
259-
arms[0].pats.len() == 1 && arms[0].guard.is_none() &&
260-
arms[1].pats.len() == 1 && arms[1].guard.is_none() {
257+
if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() {
261258
let els = remove_blocks(&arms[1].body);
262259
let els = if is_unit_expr(els) {
263260
None
@@ -283,7 +280,7 @@ fn check_single_match_single_pattern(
283280
expr: &Expr,
284281
els: Option<&Expr>,
285282
) {
286-
if is_wild(&arms[1].pats[0]) {
283+
if is_wild(&arms[1].pat) {
287284
report_single_match_single_pattern(cx, ex, arms, expr, els);
288285
}
289286
}
@@ -308,7 +305,7 @@ fn report_single_match_single_pattern(
308305
"try this",
309306
format!(
310307
"if let {} = {} {}{}",
311-
snippet(cx, arms[0].pats[0].span, ".."),
308+
snippet(cx, arms[0].pat.span, ".."),
312309
snippet(cx, ex.span, ".."),
313310
expr_block(cx, &arms[0].body, None, ".."),
314311
els_str,
@@ -336,7 +333,7 @@ fn check_single_match_opt_like(
336333
(&paths::RESULT, "Ok"),
337334
];
338335

339-
let path = match arms[1].pats[0].node {
336+
let path = match arms[1].pat.node {
340337
PatKind::TupleStruct(ref path, ref inner, _) => {
341338
// Contains any non wildcard patterns (e.g., `Err(err)`)?
342339
if !inner.iter().all(is_wild) {
@@ -365,9 +362,9 @@ fn check_match_bool(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: &Ex
365362
expr.span,
366363
"you seem to be trying to match on a boolean expression",
367364
move |db| {
368-
if arms.len() == 2 && arms[0].pats.len() == 1 {
365+
if arms.len() == 2 {
369366
// no guards
370-
let exprs = if let PatKind::Lit(ref arm_bool) = arms[0].pats[0].node {
367+
let exprs = if let PatKind::Lit(ref arm_bool) = arms[0].pat.node {
371368
if let ExprKind::Lit(ref lit) = arm_bool.node {
372369
match lit.node {
373370
LitKind::Bool(true) => Some((&*arms[0].body, &*arms[1].body)),
@@ -446,7 +443,7 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
446443
let ex_ty = walk_ptrs_ty(cx.tables.expr_ty(ex));
447444
if match_type(cx, ex_ty, &paths::RESULT) {
448445
for arm in arms {
449-
if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pats[0].node {
446+
if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pat.node {
450447
let path_str = print::to_string(print::NO_ANN, |s| s.print_qpath(path, false));
451448
if_chain! {
452449
if path_str == "Err";
@@ -457,9 +454,9 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
457454
// `Err(_)` arm with `panic!` found
458455
span_note_and_lint(cx,
459456
MATCH_WILD_ERR_ARM,
460-
arm.pats[0].span,
457+
arm.pat.span,
461458
"Err(_) will match all errors, maybe not a good idea",
462-
arm.pats[0].span,
459+
arm.pat.span,
463460
"to remove this warning, match each error separately \
464461
or use unreachable macro");
465462
}
@@ -482,13 +479,11 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
482479
let mut wildcard_span = None;
483480
let mut wildcard_ident = None;
484481
for arm in arms {
485-
for pat in &arm.pats {
486-
if let PatKind::Wild = pat.node {
487-
wildcard_span = Some(pat.span);
488-
} else if let PatKind::Binding(_, _, ident, None) = pat.node {
489-
wildcard_span = Some(pat.span);
490-
wildcard_ident = Some(ident);
491-
}
482+
if let PatKind::Wild = arm.pat.node {
483+
wildcard_span = Some(arm.pat.span);
484+
} else if let PatKind::Binding(_, _, ident, None) = arm.pat.node {
485+
wildcard_span = Some(arm.pat.span);
486+
wildcard_ident = Some(ident);
492487
}
493488
}
494489

@@ -510,15 +505,13 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
510505
// covered by the set of guards that cover it, but that's really hard to do.
511506
continue;
512507
}
513-
for pat in &arm.pats {
514-
if let PatKind::Path(ref path) = pat.deref().node {
515-
if let QPath::Resolved(_, p) = path {
516-
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id()));
517-
}
518-
} else if let PatKind::TupleStruct(ref path, ..) = pat.deref().node {
519-
if let QPath::Resolved(_, p) = path {
520-
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id()));
521-
}
508+
if let PatKind::Path(ref path) = arm.pat.node {
509+
if let QPath::Resolved(_, p) = path {
510+
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id()));
511+
}
512+
} else if let PatKind::TupleStruct(ref path, ..) = arm.pat.node {
513+
if let QPath::Resolved(_, p) = path {
514+
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id()));
522515
}
523516
}
524517
}
@@ -588,9 +581,9 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr:
588581
)
589582
};
590583

591-
suggs.extend(arms.iter().flat_map(|a| &a.pats).filter_map(|p| {
592-
if let PatKind::Ref(ref refp, _) = p.node {
593-
Some((p.span, snippet(cx, refp.span, "..").to_string()))
584+
suggs.extend(arms.iter().filter_map(|a| {
585+
if let PatKind::Ref(ref refp, _) = a.pat.node {
586+
Some((a.pat.span, snippet(cx, refp.span, "..").to_string()))
594587
} else {
595588
None
596589
}
@@ -605,12 +598,7 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr:
605598
}
606599

607600
fn check_match_as_ref(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: &Expr) {
608-
if arms.len() == 2
609-
&& arms[0].pats.len() == 1
610-
&& arms[0].guard.is_none()
611-
&& arms[1].pats.len() == 1
612-
&& arms[1].guard.is_none()
613-
{
601+
if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() {
614602
let arm_ref: Option<BindingAnnotation> = if is_none_arm(&arms[0]) {
615603
is_ref_some_arm(&arms[1])
616604
} else if is_none_arm(&arms[1]) {
@@ -666,14 +654,9 @@ fn all_ranges<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arms: &'tcx [Arm]) -> Vec<Sp
666654
arms.iter()
667655
.flat_map(|arm| {
668656
if let Arm {
669-
ref pats, guard: None, ..
657+
ref pat, guard: None, ..
670658
} = *arm
671659
{
672-
pats.iter()
673-
} else {
674-
[].iter()
675-
}
676-
.filter_map(|pat| {
677660
if let PatKind::Range(ref lhs, ref rhs, ref range_end) = pat.node {
678661
let lhs = constant(cx, cx.tables, lhs)?.0;
679662
let rhs = constant(cx, cx.tables, rhs)?.0;
@@ -694,9 +677,8 @@ fn all_ranges<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arms: &'tcx [Arm]) -> Vec<Sp
694677
node: (value.clone(), Bound::Included(value)),
695678
});
696679
}
697-
698-
None
699-
})
680+
}
681+
None
700682
})
701683
.collect()
702684
}
@@ -743,7 +725,7 @@ fn is_unit_expr(expr: &Expr) -> bool {
743725

744726
// Checks if arm has the form `None => None`
745727
fn is_none_arm(arm: &Arm) -> bool {
746-
match arm.pats[0].node {
728+
match arm.pat.node {
747729
PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => true,
748730
_ => false,
749731
}
@@ -752,7 +734,7 @@ fn is_none_arm(arm: &Arm) -> bool {
752734
// Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
753735
fn is_ref_some_arm(arm: &Arm) -> Option<BindingAnnotation> {
754736
if_chain! {
755-
if let PatKind::TupleStruct(ref path, ref pats, _) = arm.pats[0].node;
737+
if let PatKind::TupleStruct(ref path, ref pats, _) = arm.pat.node;
756738
if pats.len() == 1 && match_qpath(path, &paths::OPTION_SOME);
757739
if let PatKind::Binding(rb, .., ident, _) = pats[0].node;
758740
if rb == BindingAnnotation::Ref || rb == BindingAnnotation::RefMut;
@@ -772,9 +754,8 @@ fn is_ref_some_arm(arm: &Arm) -> Option<BindingAnnotation> {
772754
fn has_only_ref_pats(arms: &[Arm]) -> bool {
773755
let mapped = arms
774756
.iter()
775-
.flat_map(|a| &a.pats)
776-
.map(|p| {
777-
match p.node {
757+
.map(|a| {
758+
match a.pat.node {
778759
PatKind::Ref(..) => Some(true), // &-patterns
779760
PatKind::Wild => Some(false), // an "anything" wildcard is also fine
780761
_ => None, // any other pattern is not fine

clippy_lints/src/ok_if_let.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
4242
if let ExprKind::Match(ref op, ref body, ref source) = expr.node; //test if expr is a match
4343
if let MatchSource::IfLetDesugar { .. } = *source; //test if it is an If Let
4444
if let ExprKind::MethodCall(_, _, ref result_types) = op.node; //check is expr.ok() has type Result<T,E>.ok()
45-
if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _) = body[0].pats[0].node; //get operation
45+
if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _) = body[0].pat.node; //get operation
4646
if method_chain_args(op, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
4747

4848
then {

0 commit comments

Comments
 (0)