Skip to content

Commit 805ac66

Browse files
committed
fix: consider assignee expressions in record fields exhaustiveness check
1 parent 64758bd commit 805ac66

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

crates/hir-ty/src/diagnostics/expr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ pub fn record_literal_missing_fields(
305305
expr: &Expr,
306306
) -> Option<(VariantId, Vec<LocalFieldId>, /*exhaustive*/ bool)> {
307307
let (fields, exhaustive) = match expr {
308-
Expr::RecordLit { fields, spread, .. } => (fields, spread.is_none()),
308+
Expr::RecordLit { fields, spread, ellipsis, is_assignee_expr, .. } => {
309+
let exhaustive = if *is_assignee_expr { !*ellipsis } else { spread.is_none() };
310+
(fields, exhaustive)
311+
}
309312
_ => return None,
310313
};
311314

crates/ide-diagnostics/src/handlers/missing_fields.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,37 @@ fn x(a: S) {
292292
)
293293
}
294294

295+
#[test]
296+
fn missing_record_expr_in_assignee_expr() {
297+
check_diagnostics(
298+
r"
299+
struct S { s: usize, t: usize }
300+
struct S2 { s: S, t: () }
301+
struct T(S);
302+
fn regular(a: S) {
303+
let s;
304+
S { s, .. } = a;
305+
}
306+
fn nested(a: S2) {
307+
let s;
308+
S2 { s: S { s, .. }, .. } = a;
309+
}
310+
fn in_tuple(a: (S,)) {
311+
let s;
312+
(S { s, .. },) = a;
313+
}
314+
fn in_array(a: [S;1]) {
315+
let s;
316+
[S { s, .. },] = a;
317+
}
318+
fn in_tuple_struct(a: T) {
319+
let s;
320+
T(S { s, .. }) = a;
321+
}
322+
",
323+
);
324+
}
325+
295326
#[test]
296327
fn range_mapping_out_of_macros() {
297328
check_fix(

0 commit comments

Comments
 (0)