From b2dbe6e43a28a22be2b5d8631dff83b644520f59 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Wed, 30 Dec 2020 17:23:00 +0000 Subject: Add fix to wrap return expression in Some --- crates/ide/src/diagnostics.rs | 53 +++++++++++++++++++++++++++++++++++-- crates/ide/src/diagnostics/fixes.rs | 13 ++++----- 2 files changed, 58 insertions(+), 8 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 6931a6190..0799999e4 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs @@ -125,7 +125,7 @@ pub(crate) fn diagnostics( .on::(|d| { res.borrow_mut().push(diagnostic_with_fix(d, &sema)); }) - .on::(|d| { + .on::(|d| { res.borrow_mut().push(diagnostic_with_fix(d, &sema)); }) .on::(|d| { @@ -304,6 +304,40 @@ mod tests { expect.assert_debug_eq(&diagnostics) } + #[test] + fn test_wrap_return_type_option() { + check_fix( + r#" +//- /main.rs crate:main deps:core +use core::option::Option::{self, Some, None}; + +fn div(x: i32, y: i32) -> Option { + if y == 0 { + return None; + } + x / y<|> +} +//- /core/lib.rs crate:core +pub mod result { + pub enum Result { Ok(T), Err(E) } +} +pub mod option { + pub enum Option { Some(T), None } +} +"#, + r#" +use core::option::Option::{self, Some, None}; + +fn div(x: i32, y: i32) -> Option { + if y == 0 { + return None; + } + Some(x / y) +} +"#, + ); + } + #[test] fn test_wrap_return_type() { check_fix( @@ -321,6 +355,9 @@ fn div(x: i32, y: i32) -> Result { pub mod result { pub enum Result { Ok(T), Err(E) } } +pub mod option { + pub enum Option { Some(T), None } +} "#, r#" use core::result::Result::{self, Ok, Err}; @@ -352,6 +389,9 @@ fn div(x: T) -> Result { pub mod result { pub enum Result { Ok(T), Err(E) } } +pub mod option { + pub enum Option { Some(T), None } +} "#, r#" use core::result::Result::{self, Ok, Err}; @@ -385,6 +425,9 @@ fn div(x: i32, y: i32) -> MyResult { pub mod result { pub enum Result { Ok(T), Err(E) } } +pub mod option { + pub enum Option { Some(T), None } +} "#, r#" use core::result::Result::{self, Ok, Err}; @@ -414,12 +457,15 @@ fn foo() -> Result<(), i32> { 0 } pub mod result { pub enum Result { Ok(T), Err(E) } } +pub mod option { + pub enum Option { Some(T), None } +} "#, ); } #[test] - fn test_wrap_return_type_not_applicable_when_return_type_is_not_result() { + fn test_wrap_return_type_not_applicable_when_return_type_is_not_result_or_option() { check_no_diagnostics( r#" //- /main.rs crate:main deps:core @@ -433,6 +479,9 @@ fn foo() -> SomeOtherEnum { 0 } pub mod result { pub enum Result { Ok(T), Err(E) } } +pub mod option { + pub enum Option { Some(T), None } +} "#, ); } diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index 71ec4df92..50c18d02b 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -3,7 +3,7 @@ use hir::{ db::AstDatabase, diagnostics::{ - Diagnostic, IncorrectCase, MissingFields, MissingOkInTailExpr, NoSuchField, + Diagnostic, IncorrectCase, MissingFields, MissingOkOrSomeInTailExpr, NoSuchField, RemoveThisSemicolon, UnresolvedModule, }, HasSource, HirDisplay, InFile, Semantics, VariantDef, @@ -94,15 +94,16 @@ impl DiagnosticWithFix for MissingFields { } } -impl DiagnosticWithFix for MissingOkInTailExpr { +impl DiagnosticWithFix for MissingOkOrSomeInTailExpr { fn fix(&self, sema: &Semantics) -> Option { let root = sema.db.parse_or_expand(self.file)?; let tail_expr = self.expr.to_node(&root); let tail_expr_range = tail_expr.syntax().text_range(); - let edit = TextEdit::replace(tail_expr_range, format!("Ok({})", tail_expr.syntax())); - let source_change = - SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); - Some(Fix::new("Wrap with ok", source_change, tail_expr_range)) + let replacement = format!("{}({})", self.required, tail_expr.syntax()); + let edit = TextEdit::replace(tail_expr_range, replacement); + let source_change = SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); + let name = if self.required == "Ok" { "Wrap with Ok" } else { "Wrap with Some" }; + Some(Fix::new(name, source_change, tail_expr_range)) } } -- cgit v1.2.3