Diagnose incorrect usages of the question mark operator

This commit is contained in:
Lukas Wirth 2022-10-05 19:15:07 +02:00
parent 8406380b5a
commit 381366f1dd
13 changed files with 289 additions and 125 deletions

View file

@ -81,11 +81,12 @@ use crate::db::{DefDatabase, HirDatabase};
pub use crate::{
attrs::{HasAttrs, Namespace},
diagnostics::{
AnyDiagnostic, BreakOutsideOfLoop, InactiveCode, IncorrectCase, InvalidDeriveTarget,
MacroError, MalformedDerive, MismatchedArgCount, MissingFields, MissingMatchArms,
MissingUnsafe, NoSuchField, ReplaceFilterMapNextWithFindMap, TypeMismatch,
UnimplementedBuiltinMacro, UnresolvedExternCrate, UnresolvedImport, UnresolvedMacroCall,
UnresolvedModule, UnresolvedProcMacro,
AnyDiagnostic, BreakOutsideOfLoop, InactiveCode, IncorrectCase, IncorrectTryExpr,
InvalidDeriveTarget, MacroError, MalformedDerive, MismatchedArgCount, MissingFields,
MissingMatchArms, MissingUnsafe, NoSuchField, NotImplemented,
ReplaceFilterMapNextWithFindMap, TypeMismatch, UnimplementedBuiltinMacro,
UnresolvedExternCrate, UnresolvedImport, UnresolvedMacroCall, UnresolvedModule,
UnresolvedProcMacro,
},
has_source::HasSource,
semantics::{PathResolution, Semantics, SemanticsScope, TypeInfo, VisibleTraits},
@ -1282,30 +1283,45 @@ impl DefWithBody {
let infer = db.infer(self.into());
let source_map = Lazy::new(|| db.body_with_source_map(self.into()).1);
for d in &infer.diagnostics {
match d {
match *d {
hir_ty::InferenceDiagnostic::NoSuchField { expr } => {
let field = source_map.field_syntax(*expr);
let field = source_map.field_syntax(expr);
acc.push(NoSuchField { field }.into())
}
&hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr, is_break } => {
hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr, is_break } => {
let expr = source_map
.expr_syntax(expr)
.expect("break outside of loop in synthetic syntax");
acc.push(BreakOutsideOfLoop { expr, is_break }.into())
}
hir_ty::InferenceDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
match source_map.expr_syntax(*call_expr) {
match source_map.expr_syntax(call_expr) {
Ok(source_ptr) => acc.push(
MismatchedArgCount {
call_expr: source_ptr,
expected: *expected,
found: *found,
expected: expected,
found: found,
}
.into(),
),
Err(SyntheticSyntax) => (),
}
}
hir_ty::InferenceDiagnostic::IncorrectTryTarget { expr } => {
let expr = source_map.expr_syntax(expr).expect("try in synthetic syntax");
acc.push(IncorrectTryExpr { expr }.into())
}
hir_ty::InferenceDiagnostic::DoesNotImplement { expr, trait_, ref ty } => {
let expr = source_map.expr_syntax(expr).expect("try in synthetic syntax");
acc.push(
NotImplemented {
expr,
trait_,
ty: Type::new(db, DefWithBodyId::from(self), ty.clone()),
}
.into(),
)
}
}
}
for (expr, mismatch) in infer.expr_type_mismatches() {