From 15557506eb90cb9ea3d0d8b0835d284fb4bbd9a5 Mon Sep 17 00:00:00 2001 From: Shoyu Vanilla Date: Fri, 24 Oct 2025 22:31:47 +0900 Subject: [PATCH] fix: Fix a bug on inhabitedness checks for arrays --- .../crates/hir-ty/src/inhabitedness.rs | 2 +- .../src/handlers/non_exhaustive_let.rs | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/inhabitedness.rs b/src/tools/rust-analyzer/crates/hir-ty/src/inhabitedness.rs index 8aed2608d6cd..5e742bba3ebe 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/inhabitedness.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/inhabitedness.rs @@ -90,7 +90,7 @@ impl<'db> TypeVisitor> for UninhabitedFrom<'_, 'db> { TyKind::Tuple(..) => ty.super_visit_with(self), TyKind::Array(item_ty, len) => match try_const_usize(self.infcx.interner.db, len) { Some(0) | None => CONTINUE_OPAQUELY_INHABITED, - Some(1..) => item_ty.super_visit_with(self), + Some(1..) => item_ty.visit_with(self), }, _ => CONTINUE_OPAQUELY_INHABITED, }; diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs index e31367f3b14e..c86ecd2f03b9 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs @@ -152,6 +152,58 @@ impl Deref for Foo { fn test(x: Foo<(i32, bool)>) { let (_a, _b): &(i32, bool) = &x; } +"#, + ); + } + + #[test] + fn uninhabited_variants() { + check_diagnostics( + r#" +//- minicore: result +enum Infallible {} + +trait Foo { + type Bar; +} + +struct Wrapper { + error: T, +} + +struct FooWrapper { + error: T::Bar, +} + +fn foo>(result: Result) -> T { + let Ok(ok) = result; + ok +} + +fn bar>(result: Result) -> T { + let Ok(ok) = result; + ok +} + +fn baz>(result: Result>) -> T { + let Ok(ok) = result; + ok +} + +fn qux>(result: Result>) -> T { + let Ok(ok) = result; + ok +} + +fn quux>(result: Result) -> T { + let Ok(ok) = result; + ok +} + +fn corge>(result: Result) -> T { + let Ok(ok) = result; + ok +} "#, ); }