mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Add fix to wrap return expression in Some
This commit is contained in:
parent
981a0d708e
commit
b2dbe6e43a
7 changed files with 90 additions and 24 deletions
|
@ -186,9 +186,10 @@ impl Diagnostic for MissingMatchArms {
|
|||
}
|
||||
}
|
||||
|
||||
// Diagnostic: missing-ok-in-tail-expr
|
||||
// Diagnostic: missing-ok-or-some-in-tail-expr
|
||||
//
|
||||
// This diagnostic is triggered if block that should return `Result` returns a value not wrapped in `Ok`.
|
||||
// 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:
|
||||
//
|
||||
|
@ -198,17 +199,19 @@ impl Diagnostic for MissingMatchArms {
|
|||
// }
|
||||
// ```
|
||||
#[derive(Debug)]
|
||||
pub struct MissingOkInTailExpr {
|
||||
pub struct MissingOkOrSomeInTailExpr {
|
||||
pub file: HirFileId,
|
||||
pub expr: AstPtr<ast::Expr>,
|
||||
// `Some` or `Ok` depending on whether the return type is Result or Option
|
||||
pub required: String,
|
||||
}
|
||||
|
||||
impl Diagnostic for MissingOkInTailExpr {
|
||||
impl Diagnostic for MissingOkOrSomeInTailExpr {
|
||||
fn code(&self) -> DiagnosticCode {
|
||||
DiagnosticCode("missing-ok-in-tail-expr")
|
||||
DiagnosticCode("missing-ok-or-some-in-tail-expr")
|
||||
}
|
||||
fn message(&self) -> String {
|
||||
"wrap return expression in Ok".to_string()
|
||||
format!("wrap return expression in {}", self.required)
|
||||
}
|
||||
fn display_source(&self) -> InFile<SyntaxNodePtr> {
|
||||
InFile { file_id: self.file, value: self.expr.clone().into() }
|
||||
|
|
|
@ -11,7 +11,7 @@ use crate::{
|
|||
db::HirDatabase,
|
||||
diagnostics::{
|
||||
match_check::{is_useful, MatchCheckCtx, Matrix, PatStack, Usefulness},
|
||||
MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr, MissingPatFields,
|
||||
MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkOrSomeInTailExpr, MissingPatFields,
|
||||
RemoveThisSemicolon,
|
||||
},
|
||||
utils::variant_data,
|
||||
|
@ -306,27 +306,37 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
|
|||
};
|
||||
|
||||
let core_result_path = path![core::result::Result];
|
||||
let core_option_path = path![core::option::Option];
|
||||
|
||||
let resolver = self.owner.resolver(db.upcast());
|
||||
let core_result_enum = match resolver.resolve_known_enum(db.upcast(), &core_result_path) {
|
||||
Some(it) => it,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
let core_result_ctor = TypeCtor::Adt(AdtId::EnumId(core_result_enum));
|
||||
let params = match &mismatch.expected {
|
||||
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &core_result_ctor => {
|
||||
parameters
|
||||
}
|
||||
let core_option_enum = match resolver.resolve_known_enum(db.upcast(), &core_option_path) {
|
||||
Some(it) => it,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
if params.len() == 2 && params[0] == mismatch.actual {
|
||||
let core_result_ctor = TypeCtor::Adt(AdtId::EnumId(core_result_enum));
|
||||
let core_option_ctor = TypeCtor::Adt(AdtId::EnumId(core_option_enum));
|
||||
|
||||
let (params, required) = match &mismatch.expected {
|
||||
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &core_result_ctor => {
|
||||
(parameters, "Ok".to_string())
|
||||
},
|
||||
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &core_option_ctor => {
|
||||
(parameters, "Some".to_string())
|
||||
},
|
||||
_ => return,
|
||||
};
|
||||
|
||||
if params.len() > 0 && params[0] == mismatch.actual {
|
||||
let (_, source_map) = db.body_with_source_map(self.owner.into());
|
||||
|
||||
if let Ok(source_ptr) = source_map.expr_syntax(id) {
|
||||
self.sink
|
||||
.push(MissingOkInTailExpr { file: source_ptr.file_id, expr: source_ptr.value });
|
||||
.push(MissingOkOrSomeInTailExpr { file: source_ptr.file_id, expr: source_ptr.value, required });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue