mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +00:00
internal: refactor missing or or some diagnostic
This commit is contained in:
parent
74f3cca85a
commit
949a6ec469
5 changed files with 43 additions and 57 deletions
|
@ -37,6 +37,7 @@ diagnostics![
|
||||||
MacroError,
|
MacroError,
|
||||||
MismatchedArgCount,
|
MismatchedArgCount,
|
||||||
MissingFields,
|
MissingFields,
|
||||||
|
MissingOkOrSomeInTailExpr,
|
||||||
MissingUnsafe,
|
MissingUnsafe,
|
||||||
NoSuchField,
|
NoSuchField,
|
||||||
RemoveThisSemicolon,
|
RemoveThisSemicolon,
|
||||||
|
@ -157,41 +158,13 @@ pub struct RemoveThisSemicolon {
|
||||||
pub expr: InFile<AstPtr<ast::Expr>>,
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct MissingOkOrSomeInTailExpr {
|
pub struct MissingOkOrSomeInTailExpr {
|
||||||
pub file: HirFileId,
|
pub expr: InFile<AstPtr<ast::Expr>>,
|
||||||
pub expr: AstPtr<ast::Expr>,
|
|
||||||
// `Some` or `Ok` depending on whether the return type is Result or Option
|
// `Some` or `Ok` depending on whether the return type is Result or Option
|
||||||
pub required: String,
|
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
|
// Diagnostic: missing-match-arm
|
||||||
//
|
//
|
||||||
// This diagnostic is triggered if `match` block is missing one or more match arms.
|
// This diagnostic is triggered if `match` block is missing one or more match arms.
|
||||||
|
|
|
@ -1190,11 +1190,7 @@ impl Function {
|
||||||
}
|
}
|
||||||
BodyValidationDiagnostic::MissingOkOrSomeInTailExpr { expr, required } => {
|
BodyValidationDiagnostic::MissingOkOrSomeInTailExpr { expr, required } => {
|
||||||
match source_map.expr_syntax(expr) {
|
match source_map.expr_syntax(expr) {
|
||||||
Ok(source_ptr) => sink.push(MissingOkOrSomeInTailExpr {
|
Ok(expr) => acc.push(MissingOkOrSomeInTailExpr { expr, required }.into()),
|
||||||
file: source_ptr.file_id,
|
|
||||||
expr: source_ptr.value,
|
|
||||||
required,
|
|
||||||
}),
|
|
||||||
Err(SyntheticSyntax) => (),
|
Err(SyntheticSyntax) => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ mod inactive_code;
|
||||||
mod macro_error;
|
mod macro_error;
|
||||||
mod mismatched_arg_count;
|
mod mismatched_arg_count;
|
||||||
mod missing_fields;
|
mod missing_fields;
|
||||||
|
mod missing_ok_or_some_in_tail_expr;
|
||||||
mod missing_unsafe;
|
mod missing_unsafe;
|
||||||
mod no_such_field;
|
mod no_such_field;
|
||||||
mod remove_this_semicolon;
|
mod remove_this_semicolon;
|
||||||
|
@ -163,9 +164,6 @@ pub(crate) fn diagnostics(
|
||||||
}
|
}
|
||||||
let res = RefCell::new(res);
|
let res = RefCell::new(res);
|
||||||
let sink_builder = DiagnosticSinkBuilder::new()
|
let sink_builder = DiagnosticSinkBuilder::new()
|
||||||
.on::<hir::diagnostics::MissingOkOrSomeInTailExpr, _>(|d| {
|
|
||||||
res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve));
|
|
||||||
})
|
|
||||||
.on::<hir::diagnostics::IncorrectCase, _>(|d| {
|
.on::<hir::diagnostics::IncorrectCase, _>(|d| {
|
||||||
res.borrow_mut().push(warning_with_fix(d, &sema, resolve));
|
res.borrow_mut().push(warning_with_fix(d, &sema, resolve));
|
||||||
})
|
})
|
||||||
|
@ -223,6 +221,7 @@ pub(crate) fn diagnostics(
|
||||||
AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),
|
AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),
|
||||||
AnyDiagnostic::MismatchedArgCount(d) => mismatched_arg_count::mismatched_arg_count(&ctx, &d),
|
AnyDiagnostic::MismatchedArgCount(d) => mismatched_arg_count::mismatched_arg_count(&ctx, &d),
|
||||||
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
|
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
|
||||||
|
AnyDiagnostic::MissingOkOrSomeInTailExpr(d) => missing_ok_or_some_in_tail_expr::missing_ok_or_some_in_tail_expr(&ctx, &d),
|
||||||
AnyDiagnostic::MissingUnsafe(d) => missing_unsafe::missing_unsafe(&ctx, &d),
|
AnyDiagnostic::MissingUnsafe(d) => missing_unsafe::missing_unsafe(&ctx, &d),
|
||||||
AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d),
|
AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d),
|
||||||
AnyDiagnostic::RemoveThisSemicolon(d) => remove_this_semicolon::remove_this_semicolon(&ctx, &d),
|
AnyDiagnostic::RemoveThisSemicolon(d) => remove_this_semicolon::remove_this_semicolon(&ctx, &d),
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
//! The same module also has all curret custom fixes for the diagnostics implemented.
|
//! The same module also has all curret custom fixes for the diagnostics implemented.
|
||||||
mod change_case;
|
mod change_case;
|
||||||
mod replace_with_find_map;
|
mod replace_with_find_map;
|
||||||
mod wrap_tail_expr;
|
|
||||||
|
|
||||||
use hir::{diagnostics::Diagnostic, Semantics};
|
use hir::{diagnostics::Diagnostic, Semantics};
|
||||||
use ide_assists::AssistResolveStrategy;
|
use ide_assists::AssistResolveStrategy;
|
||||||
|
|
|
@ -1,26 +1,45 @@
|
||||||
use hir::{db::AstDatabase, diagnostics::MissingOkOrSomeInTailExpr, Semantics};
|
use hir::{db::AstDatabase, Semantics};
|
||||||
use ide_assists::{Assist, AssistResolveStrategy};
|
use ide_assists::Assist;
|
||||||
use ide_db::{source_change::SourceChange, RootDatabase};
|
use ide_db::source_change::SourceChange;
|
||||||
use syntax::AstNode;
|
use syntax::AstNode;
|
||||||
use text_edit::TextEdit;
|
use text_edit::TextEdit;
|
||||||
|
|
||||||
use crate::diagnostics::{fix, DiagnosticWithFixes};
|
use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
|
||||||
|
|
||||||
impl DiagnosticWithFixes for MissingOkOrSomeInTailExpr {
|
// Diagnostic: missing-ok-or-some-in-tail-expr
|
||||||
fn fixes(
|
//
|
||||||
&self,
|
// This diagnostic is triggered if a block that should return `Result` returns a value not wrapped in `Ok`,
|
||||||
sema: &Semantics<RootDatabase>,
|
// or if a block that should return `Option` returns a value not wrapped in `Some`.
|
||||||
_resolve: &AssistResolveStrategy,
|
//
|
||||||
) -> Option<Vec<Assist>> {
|
// Example:
|
||||||
let root = sema.db.parse_or_expand(self.file)?;
|
//
|
||||||
let tail_expr = self.expr.to_node(&root);
|
// ```rust
|
||||||
let tail_expr_range = tail_expr.syntax().text_range();
|
// fn foo() -> Result<u8, ()> {
|
||||||
let replacement = format!("{}({})", self.required, tail_expr.syntax());
|
// 10
|
||||||
let edit = TextEdit::replace(tail_expr_range, replacement);
|
// }
|
||||||
let source_change = SourceChange::from_text_edit(self.file.original_file(sema.db), edit);
|
// ```
|
||||||
let name = if self.required == "Ok" { "Wrap with Ok" } else { "Wrap with Some" };
|
pub(super) fn missing_ok_or_some_in_tail_expr(
|
||||||
Some(vec![fix("wrap_tail_expr", name, source_change, tail_expr_range)])
|
ctx: &DiagnosticsContext<'_>,
|
||||||
}
|
d: &hir::MissingOkOrSomeInTailExpr,
|
||||||
|
) -> Diagnostic {
|
||||||
|
Diagnostic::new(
|
||||||
|
"missing-ok-or-some-in-tail-expr",
|
||||||
|
format!("wrap return expression in {}", d.required),
|
||||||
|
ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range,
|
||||||
|
)
|
||||||
|
.with_fixes(fixes(ctx, d))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingOkOrSomeInTailExpr) -> Option<Vec<Assist>> {
|
||||||
|
let root = ctx.sema.db.parse_or_expand(d.expr.file_id)?;
|
||||||
|
let tail_expr = d.expr.value.to_node(&root);
|
||||||
|
let tail_expr_range = tail_expr.syntax().text_range();
|
||||||
|
let replacement = format!("{}({})", d.required, tail_expr.syntax());
|
||||||
|
let edit = TextEdit::replace(tail_expr_range, replacement);
|
||||||
|
let source_change =
|
||||||
|
SourceChange::from_text_edit(d.expr.file_id.original_file(ctx.sema.db), edit);
|
||||||
|
let name = if d.required == "Ok" { "Wrap with Ok" } else { "Wrap with Some" };
|
||||||
|
Some(vec![fix("wrap_tail_expr", name, source_change, tail_expr_range)])
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
Loading…
Add table
Add a link
Reference in a new issue