-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #16303 - rosefromthedead:non-exhaustive-let, r=Veykril
feat: add non-exhaustive-let diagnostic I want this to have a quickfix to add an else branch but I couldn't figure out how to do that, so here's the diagnostic on its own. It pretends a `let` is a match with one arm, and asks the match checking whether that match would be exhaustive. Previously the pattern was checked based on its own type, but that was causing a panic in `match_check` (while processing e.g. `crates/hir/src/lib.rs`) so I changed it to use the initialiser's type instead, to align with the checking of actual match expressions. I think the panic can still happen, but I hear that `match_check` is going to be updated to a new version from rustc, so I'm posting this now in the hopes that the panic will magically go away when that happens.
- Loading branch information
Showing
5 changed files
with
137 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext}; | ||
|
||
// Diagnostic: non-exhaustive-let | ||
// | ||
// This diagnostic is triggered if a `let` statement without an `else` branch has a non-exhaustive | ||
// pattern. | ||
pub(crate) fn non_exhaustive_let( | ||
ctx: &DiagnosticsContext<'_>, | ||
d: &hir::NonExhaustiveLet, | ||
) -> Diagnostic { | ||
Diagnostic::new_with_syntax_node_ptr( | ||
ctx, | ||
DiagnosticCode::RustcHardError("E0005"), | ||
format!("non-exhaustive pattern: {}", d.uncovered_patterns), | ||
d.pat.map(Into::into), | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::tests::check_diagnostics; | ||
|
||
#[test] | ||
fn option_nonexhaustive() { | ||
check_diagnostics( | ||
r#" | ||
//- minicore: option | ||
fn main() { | ||
let None = Some(5); | ||
//^^^^ error: non-exhaustive pattern: `Some(_)` not covered | ||
} | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn option_exhaustive() { | ||
check_diagnostics( | ||
r#" | ||
//- minicore: option | ||
fn main() { | ||
let Some(_) | None = Some(5); | ||
} | ||
"#, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters