-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathcollapsible_match.rs
More file actions
322 lines (301 loc) · 12.6 KB
/
collapsible_match.rs
File metadata and controls
322 lines (301 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::higher::{If, IfLetOrMatch};
use clippy_utils::msrvs::Msrv;
use clippy_utils::res::{MaybeDef, MaybeResPath};
use clippy_utils::source::{IntoSpan, SpanRangeExt, snippet};
use clippy_utils::usage::mutated_variables;
use clippy_utils::visitors::is_local_used;
use clippy_utils::{SpanlessEq, get_ref_operators, is_unit_expr, peel_blocks_with_stmt, peel_ref_operators};
use rustc_ast::BorrowKind;
use rustc_errors::{Applicability, MultiSpan};
use rustc_hir::LangItem::OptionNone;
use rustc_hir::{Arm, Expr, ExprKind, HirId, HirIdSet, Pat, PatExpr, PatExprKind, PatKind};
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
use rustc_lint::LateContext;
use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty;
use rustc_span::{BytePos, Ident, Span, SyntaxContext};
use super::{COLLAPSIBLE_MATCH, pat_contains_disallowed_or};
use crate::collapsible_if::{parens_around, peel_parens};
pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arms: &'tcx [Arm<'_>], msrv: Msrv) {
if let Some(els_arm) = arms.iter().rfind(|arm| arm_is_wild_like(cx, arm)) {
let last_non_wildcard = arms.iter().rposition(|arm| !arm_is_wild_like(cx, arm));
for (idx, arm) in arms.iter().enumerate() {
let only_wildcards_after = last_non_wildcard.is_none_or(|lnw| idx >= lnw);
check_arm(
cx,
arm.span.ctxt(),
true,
arm.pat,
expr,
arm.body,
arm.guard,
Some(els_arm.body),
msrv,
only_wildcards_after,
);
}
}
}
pub(super) fn check_if_let<'tcx>(
cx: &LateContext<'tcx>,
ctxt: SyntaxContext,
pat: &'tcx Pat<'_>,
body: &'tcx Expr<'_>,
else_expr: Option<&'tcx Expr<'_>>,
let_expr: &'tcx Expr<'_>,
msrv: Msrv,
) {
check_arm(cx, ctxt, false, pat, let_expr, body, None, else_expr, msrv, false);
}
#[expect(clippy::too_many_arguments, clippy::too_many_lines)]
fn check_arm<'tcx>(
cx: &LateContext<'tcx>,
ctxt: SyntaxContext,
outer_is_match: bool,
outer_pat: &'tcx Pat<'tcx>,
outer_cond: &'tcx Expr<'tcx>,
outer_then_body: &'tcx Expr<'tcx>,
outer_guard: Option<&'tcx Expr<'tcx>>,
outer_else_body: Option<&'tcx Expr<'tcx>>,
msrv: Msrv,
only_wildcards_after: bool,
) {
let inner_expr = peel_blocks_with_stmt(outer_then_body);
if let Some(inner) = IfLetOrMatch::parse(cx, inner_expr)
&& let Some((inner_scrutinee, inner_then_pat, inner_else_body)) = match inner {
IfLetOrMatch::IfLet(scrutinee, pat, _, els, _) => Some((scrutinee, pat, els)),
IfLetOrMatch::Match(scrutinee, arms, ..) => {
if arms.len() == 2 && arms.iter().all(|a| a.guard.is_none())
// if there are more than two arms, collapsing would be non-trivial
// one of the arms must be "wild-like"
&& let Some(wild_idx) = arms.iter().rposition(|a| arm_is_wild_like(cx, a))
{
let (then, els) = (&arms[1 - wild_idx], &arms[wild_idx]);
Some((scrutinee, then.pat, Some(els.body)))
} else {
None
}
},
}
&& outer_pat.span.eq_ctxt(inner_scrutinee.span)
// match expression must be a local binding
// match <local> { .. }
&& let Some(binding_id) = peel_ref_operators(cx, inner_scrutinee).res_local_id()
&& !pat_contains_disallowed_or(cx, inner_then_pat, msrv)
// the binding must come from the pattern of the containing match arm
// ..<local>.. => match <local> { .. }
&& let (Some((binding_ident, binding_span)), is_innermost_parent_pat_struct) =
find_pat_binding_and_is_innermost_parent_pat_struct(outer_pat, binding_id)
// the "else" branches must be equal
&& match (outer_else_body, inner_else_body) {
(None, None) => true,
(None, Some(e)) | (Some(e), None) => is_unit_expr(e),
(Some(a), Some(b)) => SpanlessEq::new(cx).eq_expr(ctxt, a, b),
}
// the binding must not be used in the if guard
&& outer_guard.is_none_or(|e| !is_local_used(cx, e, binding_id))
// ...or anywhere in the inner expression
&& match inner {
IfLetOrMatch::IfLet(_, _, body, els, _) => {
!is_local_used(cx, body, binding_id) && els.is_none_or(|e| !is_local_used(cx, e, binding_id))
},
IfLetOrMatch::Match(_, arms, ..) => !arms.iter().any(|arm| is_local_used(cx, arm, binding_id)),
}
// Check if the inner expression contains any borrows/dereferences
&& let ref_types = get_ref_operators(cx, inner_scrutinee)
&& let Some(method) = build_ref_method_chain(ref_types)
{
let msg = format!(
"this `{}` can be collapsed into the outer `{}`",
if matches!(inner, IfLetOrMatch::Match(..)) {
"match"
} else {
"if let"
},
if outer_is_match { "match" } else { "if let" },
);
// collapsing patterns need an explicit field name in struct pattern matching
// ex: Struct {x: Some(1)}
let replace_msg = if is_innermost_parent_pat_struct {
format!(", prefixed by `{binding_ident}: `")
} else {
String::new()
};
span_lint_hir_and_then(cx, COLLAPSIBLE_MATCH, inner_expr.hir_id, inner_expr.span, msg, |diag| {
let mut help_span = MultiSpan::from_spans(vec![binding_span, inner_then_pat.span]);
help_span.push_span_label(binding_span, "replace this binding");
help_span.push_span_label(inner_then_pat.span, format!("with this pattern{replace_msg}"));
if !method.is_empty() {
let outer_cond_msg = format!("use: `{}{}`", snippet(cx, outer_cond.span, ".."), method);
help_span.push_span_label(outer_cond.span, outer_cond_msg);
}
diag.span_help(
help_span,
"the outer pattern can be modified to include the inner pattern",
);
});
} else if outer_is_match // Leave if-let to the `collapsible_if` lint
&& only_wildcards_after // adding a guard allows fall-through; unsafe if other arms follow
&& let Some(inner) = If::hir(inner_expr)
&& outer_pat.span.eq_ctxt(inner.cond.span)
&& match (outer_else_body, inner.r#else) {
(None, None) => true,
(None, Some(e)) | (Some(e), None) => is_unit_expr(e),
(Some(a), Some(b)) => SpanlessEq::new(cx).eq_expr(ctxt, a, b),
}
&& !pat_bindings_moved_or_mutated(cx, outer_pat, inner.cond)
{
span_lint_hir_and_then(
cx,
COLLAPSIBLE_MATCH,
inner_expr.hir_id,
inner_expr.span,
"this `if` can be collapsed into the outer `match`",
|diag| {
let outer_then_open_bracket = outer_then_body
.span
.split_at(1)
.0
.with_leading_whitespace(cx)
.into_span();
let outer_then_closing_bracket = {
let end = outer_then_body.span.shrink_to_hi();
end.with_lo(end.lo() - BytePos(1))
.with_leading_whitespace(cx)
.into_span()
};
let outer_arrow_end = if let Some(outer_guard) = outer_guard {
outer_guard.span.shrink_to_hi()
} else {
outer_pat.span.shrink_to_hi()
};
let (paren_start, inner_if_span, paren_end) = peel_parens(cx, inner_expr.span);
let inner_if = inner_if_span.split_at(2).0;
let mut sugg = vec![
(inner.then.span.shrink_to_lo(), "=> ".to_string()),
(outer_arrow_end.to(outer_then_open_bracket), String::new()),
(outer_then_closing_bracket, String::new()),
];
if let Some(outer_guard) = outer_guard {
sugg.extend(parens_around(outer_guard));
sugg.push((inner_if, "&&".to_string()));
}
if !paren_start.is_empty() {
sugg.push((paren_start, String::new()));
}
if !paren_end.is_empty() {
sugg.push((paren_end, String::new()));
}
sugg.extend(parens_around(inner.cond));
if let Some(else_inner) = inner.r#else {
let else_inner_span = inner.then.span.shrink_to_hi().to(else_inner.span);
sugg.push((else_inner_span, String::new()));
}
diag.multipart_suggestion("collapse nested if block", sugg, Applicability::MachineApplicable);
},
);
}
}
/// A "wild-like" arm has a wild (`_`) or `None` pattern and no guard. Such arms can be "collapsed"
/// into a single wild arm without any significant loss in semantics or readability.
fn arm_is_wild_like(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
if arm.guard.is_some() {
return false;
}
match arm.pat.kind {
PatKind::Binding(..) | PatKind::Wild => true,
PatKind::Expr(PatExpr {
kind: PatExprKind::Path(qpath),
hir_id,
..
}) => cx
.qpath_res(qpath, *hir_id)
.ctor_parent(cx)
.is_lang_item(cx, OptionNone),
_ => false,
}
}
fn find_pat_binding_and_is_innermost_parent_pat_struct(pat: &Pat<'_>, hir_id: HirId) -> (Option<(Ident, Span)>, bool) {
let mut binding = None;
let mut is_innermost_parent_pat_struct = false;
pat.walk_short(|p| match p.kind {
// ignore OR patterns
PatKind::Or(_) => false,
PatKind::Binding(_bm, _, ident, _) => {
let found = p.hir_id == hir_id;
if found {
binding = Some((ident, p.span));
}
!found
},
_ => {
is_innermost_parent_pat_struct = matches!(p.kind, PatKind::Struct(..));
true
},
});
(binding, is_innermost_parent_pat_struct)
}
/// Builds a chain of reference-manipulation method calls (e.g., `.as_ref()`, `.as_mut()`,
/// `.copied()`) based on reference operators
fn build_ref_method_chain(expr: Vec<&Expr<'_>>) -> Option<String> {
let mut req_method_calls = String::new();
for ref_operator in expr {
match ref_operator.kind {
ExprKind::AddrOf(BorrowKind::Raw, _, _) => {
return None;
},
ExprKind::AddrOf(_, m, _) if m.is_mut() => {
req_method_calls.push_str(".as_mut()");
},
ExprKind::AddrOf(_, _, _) => {
req_method_calls.push_str(".as_ref()");
},
// Deref operator is the only operator that this function should have received
ExprKind::Unary(_, _) => {
req_method_calls.push_str(".copied()");
},
_ => (),
}
}
Some(req_method_calls)
}
/// Checks if any of the bindings in the `pat` are moved or mutated in the `expr`. It is invalid to
/// move or mutate bindings in `if` guards.
fn pat_bindings_moved_or_mutated<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
let mut delegate = MovedVarDelegate {
moved: HirIdSet::default(),
};
if ExprUseVisitor::for_clippy(cx, expr.hir_id.owner.def_id, &mut delegate)
.walk_expr(expr)
.is_err()
{
return true;
}
let mut candidates = delegate.moved;
if let Some(mutated) = mutated_variables(expr, cx) {
candidates.extend(mutated);
}
!pat.walk_short(|pat| {
if let PatKind::Binding(_, hir_id, ..) = pat.kind
&& candidates.contains(&hir_id)
{
return false;
}
true
})
}
struct MovedVarDelegate {
moved: HirIdSet,
}
impl<'tcx> Delegate<'tcx> for MovedVarDelegate {
fn consume(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId) {
if let PlaceBase::Local(hir_id) = cmt.place.base {
self.moved.insert(hir_id);
}
}
fn use_cloned(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
fn borrow(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {}
fn mutate(&mut self, _: &PlaceWithHirId<'tcx>, _: HirId) {}
fn fake_read(&mut self, _: &PlaceWithHirId<'tcx>, _: FakeReadCause, _: HirId) {}
}