Skip to content

Commit 5390e4c

Browse files
rosefromthedeadVeykril
authored andcommitted
feat: add non-exhaustive-let diagnostic
1 parent 69c2532 commit 5390e4c

File tree

5 files changed

+129
-5
lines changed

5 files changed

+129
-5
lines changed

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

+56-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use hir_expand::name;
1212
use itertools::Itertools;
1313
use rustc_hash::FxHashSet;
1414
use rustc_pattern_analysis::usefulness::{compute_match_usefulness, ValidityConstraint};
15+
use tracing::debug;
1516
use triomphe::Arc;
1617
use typed_arena::Arena;
1718

@@ -44,6 +45,10 @@ pub enum BodyValidationDiagnostic {
4445
match_expr: ExprId,
4546
uncovered_patterns: String,
4647
},
48+
NonExhaustiveLet {
49+
pat: PatId,
50+
uncovered_patterns: String,
51+
},
4752
RemoveTrailingReturn {
4853
return_expr: ExprId,
4954
},
@@ -68,7 +73,7 @@ struct ExprValidator {
6873
owner: DefWithBodyId,
6974
body: Arc<Body>,
7075
infer: Arc<InferenceResult>,
71-
pub(super) diagnostics: Vec<BodyValidationDiagnostic>,
76+
diagnostics: Vec<BodyValidationDiagnostic>,
7277
}
7378

7479
impl ExprValidator {
@@ -105,6 +110,9 @@ impl ExprValidator {
105110
Expr::If { .. } => {
106111
self.check_for_unnecessary_else(id, expr, &body);
107112
}
113+
Expr::Block { .. } => {
114+
self.validate_block(db, expr);
115+
}
108116
_ => {}
109117
}
110118
}
@@ -231,11 +239,55 @@ impl ExprValidator {
231239
if !witnesses.is_empty() {
232240
self.diagnostics.push(BodyValidationDiagnostic::MissingMatchArms {
233241
match_expr,
234-
uncovered_patterns: missing_match_arms(&cx, scrut_ty, witnesses, arms),
242+
uncovered_patterns: missing_match_arms(&cx, scrut_ty, witnesses, m_arms.is_empty()),
235243
});
236244
}
237245
}
238246

247+
fn validate_block(&mut self, db: &dyn HirDatabase, expr: &Expr) {
248+
let Expr::Block { statements, .. } = expr else { return };
249+
let pattern_arena = Arena::new();
250+
let cx = MatchCheckCtx::new(self.owner.module(db.upcast()), self.owner, db);
251+
for stmt in &**statements {
252+
let &Statement::Let { pat, initializer, else_branch: None, .. } = stmt else {
253+
continue;
254+
};
255+
let Some(initializer) = initializer else { continue };
256+
let ty = &self.infer[initializer];
257+
258+
let mut have_errors = false;
259+
let deconstructed_pat = self.lower_pattern(&cx, pat, db, &mut have_errors);
260+
let match_arm = rustc_pattern_analysis::MatchArm {
261+
pat: pattern_arena.alloc(deconstructed_pat),
262+
has_guard: false,
263+
arm_data: (),
264+
};
265+
if have_errors {
266+
continue;
267+
}
268+
269+
let report = match compute_match_usefulness(
270+
&cx,
271+
&[match_arm],
272+
ty.clone(),
273+
ValidityConstraint::ValidOnly,
274+
) {
275+
Ok(v) => v,
276+
Err(e) => {
277+
debug!(?e, "match usefulness error");
278+
continue;
279+
}
280+
};
281+
let witnesses = report.non_exhaustiveness_witnesses;
282+
if !witnesses.is_empty() {
283+
self.diagnostics.push(BodyValidationDiagnostic::NonExhaustiveLet {
284+
pat,
285+
uncovered_patterns: missing_match_arms(&cx, ty, witnesses, false),
286+
});
287+
}
288+
}
289+
}
290+
239291
fn lower_pattern<'p>(
240292
&self,
241293
cx: &MatchCheckCtx<'p>,
@@ -444,7 +496,7 @@ fn missing_match_arms<'p>(
444496
cx: &MatchCheckCtx<'p>,
445497
scrut_ty: &Ty,
446498
witnesses: Vec<WitnessPat<'p>>,
447-
arms: &[MatchArm],
499+
arms_is_empty: bool,
448500
) -> String {
449501
struct DisplayWitness<'a, 'p>(&'a WitnessPat<'p>, &'a MatchCheckCtx<'p>);
450502
impl fmt::Display for DisplayWitness<'_, '_> {
@@ -459,7 +511,7 @@ fn missing_match_arms<'p>(
459511
Some((AdtId::EnumId(e), _)) => !cx.db.enum_data(e).variants.is_empty(),
460512
_ => false,
461513
};
462-
if arms.is_empty() && !non_empty_enum {
514+
if arms_is_empty && !non_empty_enum {
463515
format!("type `{}` is non-empty", scrut_ty.display(cx.db))
464516
} else {
465517
let pat_display = |witness| DisplayWitness(witness, cx);

crates/hir/src/diagnostics.rs

+23
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ diagnostics![
6464
MissingUnsafe,
6565
MovedOutOfRef,
6666
NeedMut,
67+
NonExhaustiveLet,
6768
NoSuchField,
6869
PrivateAssocItem,
6970
PrivateField,
@@ -280,6 +281,12 @@ pub struct MissingMatchArms {
280281
pub uncovered_patterns: String,
281282
}
282283

284+
#[derive(Debug)]
285+
pub struct NonExhaustiveLet {
286+
pub pat: InFile<AstPtr<ast::Pat>>,
287+
pub uncovered_patterns: String,
288+
}
289+
283290
#[derive(Debug)]
284291
pub struct TypeMismatch {
285292
pub expr_or_pat: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
@@ -456,6 +463,22 @@ impl AnyDiagnostic {
456463
Err(SyntheticSyntax) => (),
457464
}
458465
}
466+
BodyValidationDiagnostic::NonExhaustiveLet { pat, uncovered_patterns } => {
467+
match source_map.pat_syntax(pat) {
468+
Ok(source_ptr) => {
469+
if let Some(ast_pat) = source_ptr.value.cast::<ast::Pat>() {
470+
return Some(
471+
NonExhaustiveLet {
472+
pat: InFile::new(source_ptr.file_id, ast_pat),
473+
uncovered_patterns,
474+
}
475+
.into(),
476+
);
477+
}
478+
}
479+
Err(SyntheticSyntax) => {}
480+
}
481+
}
459482
BodyValidationDiagnostic::RemoveTrailingReturn { return_expr } => {
460483
if let Ok(source_ptr) = source_map.expr_syntax(return_expr) {
461484
// Filters out desugared return expressions (e.g. desugared try operators).

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ fn f() {
817817
//- minicore: option
818818
fn f(_: i32) {}
819819
fn main() {
820-
let ((Some(mut x), None) | (_, Some(mut x))) = (None, Some(7));
820+
let ((Some(mut x), None) | (_, Some(mut x))) = (None, Some(7)) else { return };
821821
//^^^^^ 💡 warn: variable does not need to be mutable
822822
f(x);
823823
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2+
3+
// Diagnostic: non-exhaustive-let
4+
//
5+
// This diagnostic is triggered if a `let` statement without an `else` branch has a non-exhaustive
6+
// pattern.
7+
pub(crate) fn non_exhaustive_let(
8+
ctx: &DiagnosticsContext<'_>,
9+
d: &hir::NonExhaustiveLet,
10+
) -> Diagnostic {
11+
Diagnostic::new_with_syntax_node_ptr(
12+
ctx,
13+
DiagnosticCode::RustcHardError("E0005"),
14+
format!("non-exhaustive pattern: {}", d.uncovered_patterns),
15+
d.pat.map(Into::into),
16+
)
17+
}
18+
19+
#[cfg(test)]
20+
mod tests {
21+
use crate::tests::check_diagnostics;
22+
23+
#[test]
24+
fn option_nonexhaustive() {
25+
check_diagnostics(
26+
r#"
27+
//- minicore: option
28+
fn main() {
29+
let None = Some(5);
30+
//^^^^ error: non-exhaustive pattern: `Some(_)` not covered
31+
}
32+
"#,
33+
);
34+
}
35+
36+
#[test]
37+
fn option_exhaustive() {
38+
check_diagnostics(
39+
r#"
40+
//- minicore: option
41+
fn main() {
42+
let Some(_) | None = Some(5);
43+
}
44+
"#,
45+
);
46+
}
47+
}

crates/ide-diagnostics/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod handlers {
4141
pub(crate) mod moved_out_of_ref;
4242
pub(crate) mod mutability_errors;
4343
pub(crate) mod no_such_field;
44+
pub(crate) mod non_exhaustive_let;
4445
pub(crate) mod private_assoc_item;
4546
pub(crate) mod private_field;
4647
pub(crate) mod remove_trailing_return;
@@ -359,6 +360,7 @@ pub fn diagnostics(
359360
AnyDiagnostic::MissingUnsafe(d) => handlers::missing_unsafe::missing_unsafe(&ctx, &d),
360361
AnyDiagnostic::MovedOutOfRef(d) => handlers::moved_out_of_ref::moved_out_of_ref(&ctx, &d),
361362
AnyDiagnostic::NeedMut(d) => handlers::mutability_errors::need_mut(&ctx, &d),
363+
AnyDiagnostic::NonExhaustiveLet(d) => handlers::non_exhaustive_let::non_exhaustive_let(&ctx, &d),
362364
AnyDiagnostic::NoSuchField(d) => handlers::no_such_field::no_such_field(&ctx, &d),
363365
AnyDiagnostic::PrivateAssocItem(d) => handlers::private_assoc_item::private_assoc_item(&ctx, &d),
364366
AnyDiagnostic::PrivateField(d) => handlers::private_field::private_field(&ctx, &d),

0 commit comments

Comments
 (0)