internal: refactor missing or or some diagnostic

This commit is contained in:
Aleksey Kladov 2021-06-13 20:19:11 +03:00
parent 74f3cca85a
commit 949a6ec469
5 changed files with 43 additions and 57 deletions

View file

@ -37,6 +37,7 @@ diagnostics![
MacroError,
MismatchedArgCount,
MissingFields,
MissingOkOrSomeInTailExpr,
MissingUnsafe,
NoSuchField,
RemoveThisSemicolon,
@ -157,41 +158,13 @@ pub struct RemoveThisSemicolon {
pub expr: InFile<AstPtr<ast::Expr>>,
}
// Diagnostic: missing-ok-or-some-in-tail-expr
//
// This diagnostic is triggered if a block that should return `Result` returns a value not wrapped in `Ok`,
// or if a block that should return `Option` returns a value not wrapped in `Some`.
//
// Example:
//
// ```rust
// fn foo() -> Result<u8, ()> {
// 10
// }
// ```
#[derive(Debug)]
pub struct MissingOkOrSomeInTailExpr {
pub file: HirFileId,
pub expr: AstPtr<ast::Expr>,
pub expr: InFile<AstPtr<ast::Expr>>,
// `Some` or `Ok` depending on whether the return type is Result or Option
pub required: String,
}
impl Diagnostic for MissingOkOrSomeInTailExpr {
fn code(&self) -> DiagnosticCode {
DiagnosticCode("missing-ok-or-some-in-tail-expr")
}
fn message(&self) -> String {
format!("wrap return expression in {}", self.required)
}
fn display_source(&self) -> InFile<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.expr.clone().into() }
}
fn as_any(&self) -> &(dyn Any + Send + 'static) {
self
}
}
// Diagnostic: missing-match-arm
//
// This diagnostic is triggered if `match` block is missing one or more match arms.

View file

@ -1190,11 +1190,7 @@ impl Function {
}
BodyValidationDiagnostic::MissingOkOrSomeInTailExpr { expr, required } => {
match source_map.expr_syntax(expr) {
Ok(source_ptr) => sink.push(MissingOkOrSomeInTailExpr {
file: source_ptr.file_id,
expr: source_ptr.value,
required,
}),
Ok(expr) => acc.push(MissingOkOrSomeInTailExpr { expr, required }.into()),
Err(SyntheticSyntax) => (),
}
}