Skip to content

Commit c86a9c0

Browse files
committed
Introduce DotDotPos.
This shrinks `hir::Pat` from 88 to 72 bytes.
1 parent 977b6e2 commit c86a9c0

File tree

6 files changed

+15
-7
lines changed

6 files changed

+15
-7
lines changed

clippy_lints/src/equatable_if_let.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ fn unary_pattern(pat: &Pat<'_>) -> bool {
5151
false
5252
},
5353
PatKind::Struct(_, a, etc) => !etc && a.iter().all(|x| unary_pattern(x.pat)),
54-
PatKind::Tuple(a, etc) | PatKind::TupleStruct(_, a, etc) => !etc.is_some() && array_rec(a),
54+
PatKind::Tuple(a, etc) | PatKind::TupleStruct(_, a, etc) => {
55+
!etc.as_opt_usize().is_some() && array_rec(a)
56+
}
5557
PatKind::Ref(x, _) | PatKind::Box(x) => unary_pattern(x),
5658
PatKind::Path(_) | PatKind::Lit(_) => true,
5759
}

clippy_lints/src/matches/match_same_arms.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<'a> NormalizedPat<'a> {
248248
} else {
249249
(None, adt.non_enum_variant())
250250
};
251-
let (front, back) = match wild_idx {
251+
let (front, back) = match wild_idx.as_opt_usize() {
252252
Some(i) => pats.split_at(i),
253253
None => (pats, [].as_slice()),
254254
};
@@ -268,7 +268,7 @@ impl<'a> NormalizedPat<'a> {
268268
ty::Tuple(subs) => subs.len(),
269269
_ => return Self::Wild,
270270
};
271-
let (front, back) = match wild_idx {
271+
let (front, back) = match wild_idx.as_opt_usize() {
272272
Some(i) => pats.split_at(i),
273273
None => (pats, [].as_slice()),
274274
};

clippy_lints/src/matches/single_match.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ fn form_exhaustive_matches<'a>(cx: &LateContext<'a>, ty: Ty<'a>, left: &Pat<'_>,
200200
// We don't actually know the position and the presence of the `..` (dotdot) operator
201201
// in the arms, so we need to evaluate the correct offsets here in order to iterate in
202202
// both arms at the same time.
203+
let left_pos = left_pos.as_opt_usize();
204+
let right_pos = right_pos.as_opt_usize();
203205
let len = max(
204206
left_in.len() + {
205207
if left_pos.is_some() { 1 } else { 0 }

clippy_lints/src/question_mark.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ fn check_if_let_some_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr:
122122
if_chain! {
123123
if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else }) = higher::IfLet::hir(cx, expr);
124124
if !is_else_clause(cx.tcx, expr);
125-
if let PatKind::TupleStruct(ref path1, [field], None) = let_pat.kind;
125+
if let PatKind::TupleStruct(ref path1, [field], ddpos) = let_pat.kind;
126+
if ddpos.as_opt_usize().is_none();
126127
if let PatKind::Binding(BindingAnnotation(by_ref, _), bind_id, ident, None) = field.kind;
127128
let caller_ty = cx.typeck_results().expr_ty(let_expr);
128129
let if_block = IfBlockType::IfLet(path1, caller_ty, ident.name, let_expr, if_then, if_else);

clippy_lints/src/unit_types/let_unit_value.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
1919
&& cx.typeck_results().pat_ty(local.pat).is_unit()
2020
{
2121
if (local.ty.map_or(false, |ty| !matches!(ty.kind, TyKind::Infer))
22-
|| matches!(local.pat.kind, PatKind::Tuple([], None)))
22+
|| matches!(local.pat.kind, PatKind::Tuple([], ddpos) if ddpos.as_opt_usize().is_none()))
2323
&& expr_needs_inferred_result(cx, init)
2424
{
25-
if !matches!(local.pat.kind, PatKind::Wild | PatKind::Tuple([], None)) {
25+
if !matches!(local.pat.kind, PatKind::Wild)
26+
&& !matches!(local.pat.kind, PatKind::Tuple([], ddpos) if ddpos.as_opt_usize().is_none())
27+
{
2628
span_lint_and_then(
2729
cx,
2830
LET_UNIT_VALUE,

clippy_utils/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,8 @@ pub fn iter_input_pats<'tcx>(decl: &FnDecl<'_>, body: &'tcx Body<'_>) -> impl It
15521552
pub fn is_try<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
15531553
fn is_ok(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
15541554
if_chain! {
1555-
if let PatKind::TupleStruct(ref path, pat, None) = arm.pat.kind;
1555+
if let PatKind::TupleStruct(ref path, pat, ddpos) = arm.pat.kind;
1556+
if ddpos.as_opt_usize().is_none();
15561557
if is_lang_ctor(cx, path, ResultOk);
15571558
if let PatKind::Binding(_, hir_id, _, None) = pat[0].kind;
15581559
if path_to_local_id(arm.body, hir_id);

0 commit comments

Comments
 (0)