diff options
author | Phil Ellison <[email protected]> | 2020-12-30 17:23:00 +0000 |
---|---|---|
committer | Phil Ellison <[email protected]> | 2021-01-07 19:01:33 +0000 |
commit | b2dbe6e43a28a22be2b5d8631dff83b644520f59 (patch) | |
tree | f68a822821336800792f80f3d4b1862c437956e5 /crates/ide | |
parent | 981a0d708ec352969f9ca075a3e0e50c6da48197 (diff) |
Add fix to wrap return expression in Some
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 53 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/fixes.rs | 13 |
2 files changed, 58 insertions, 8 deletions
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( | |||
125 | .on::<hir::diagnostics::MissingFields, _>(|d| { | 125 | .on::<hir::diagnostics::MissingFields, _>(|d| { |
126 | res.borrow_mut().push(diagnostic_with_fix(d, &sema)); | 126 | res.borrow_mut().push(diagnostic_with_fix(d, &sema)); |
127 | }) | 127 | }) |
128 | .on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| { | 128 | .on::<hir::diagnostics::MissingOkOrSomeInTailExpr, _>(|d| { |
129 | res.borrow_mut().push(diagnostic_with_fix(d, &sema)); | 129 | res.borrow_mut().push(diagnostic_with_fix(d, &sema)); |
130 | }) | 130 | }) |
131 | .on::<hir::diagnostics::NoSuchField, _>(|d| { | 131 | .on::<hir::diagnostics::NoSuchField, _>(|d| { |
@@ -305,6 +305,40 @@ mod tests { | |||
305 | } | 305 | } |
306 | 306 | ||
307 | #[test] | 307 | #[test] |
308 | fn test_wrap_return_type_option() { | ||
309 | check_fix( | ||
310 | r#" | ||
311 | //- /main.rs crate:main deps:core | ||
312 | use core::option::Option::{self, Some, None}; | ||
313 | |||
314 | fn div(x: i32, y: i32) -> Option<i32> { | ||
315 | if y == 0 { | ||
316 | return None; | ||
317 | } | ||
318 | x / y<|> | ||
319 | } | ||
320 | //- /core/lib.rs crate:core | ||
321 | pub mod result { | ||
322 | pub enum Result<T, E> { Ok(T), Err(E) } | ||
323 | } | ||
324 | pub mod option { | ||
325 | pub enum Option<T> { Some(T), None } | ||
326 | } | ||
327 | "#, | ||
328 | r#" | ||
329 | use core::option::Option::{self, Some, None}; | ||
330 | |||
331 | fn div(x: i32, y: i32) -> Option<i32> { | ||
332 | if y == 0 { | ||
333 | return None; | ||
334 | } | ||
335 | Some(x / y) | ||
336 | } | ||
337 | "#, | ||
338 | ); | ||
339 | } | ||
340 | |||
341 | #[test] | ||
308 | fn test_wrap_return_type() { | 342 | fn test_wrap_return_type() { |
309 | check_fix( | 343 | check_fix( |
310 | r#" | 344 | r#" |
@@ -321,6 +355,9 @@ fn div(x: i32, y: i32) -> Result<i32, ()> { | |||
321 | pub mod result { | 355 | pub mod result { |
322 | pub enum Result<T, E> { Ok(T), Err(E) } | 356 | pub enum Result<T, E> { Ok(T), Err(E) } |
323 | } | 357 | } |
358 | pub mod option { | ||
359 | pub enum Option<T> { Some(T), None } | ||
360 | } | ||
324 | "#, | 361 | "#, |
325 | r#" | 362 | r#" |
326 | use core::result::Result::{self, Ok, Err}; | 363 | use core::result::Result::{self, Ok, Err}; |
@@ -352,6 +389,9 @@ fn div<T>(x: T) -> Result<T, i32> { | |||
352 | pub mod result { | 389 | pub mod result { |
353 | pub enum Result<T, E> { Ok(T), Err(E) } | 390 | pub enum Result<T, E> { Ok(T), Err(E) } |
354 | } | 391 | } |
392 | pub mod option { | ||
393 | pub enum Option<T> { Some(T), None } | ||
394 | } | ||
355 | "#, | 395 | "#, |
356 | r#" | 396 | r#" |
357 | use core::result::Result::{self, Ok, Err}; | 397 | use core::result::Result::{self, Ok, Err}; |
@@ -385,6 +425,9 @@ fn div(x: i32, y: i32) -> MyResult<i32> { | |||
385 | pub mod result { | 425 | pub mod result { |
386 | pub enum Result<T, E> { Ok(T), Err(E) } | 426 | pub enum Result<T, E> { Ok(T), Err(E) } |
387 | } | 427 | } |
428 | pub mod option { | ||
429 | pub enum Option<T> { Some(T), None } | ||
430 | } | ||
388 | "#, | 431 | "#, |
389 | r#" | 432 | r#" |
390 | use core::result::Result::{self, Ok, Err}; | 433 | use core::result::Result::{self, Ok, Err}; |
@@ -414,12 +457,15 @@ fn foo() -> Result<(), i32> { 0 } | |||
414 | pub mod result { | 457 | pub mod result { |
415 | pub enum Result<T, E> { Ok(T), Err(E) } | 458 | pub enum Result<T, E> { Ok(T), Err(E) } |
416 | } | 459 | } |
460 | pub mod option { | ||
461 | pub enum Option<T> { Some(T), None } | ||
462 | } | ||
417 | "#, | 463 | "#, |
418 | ); | 464 | ); |
419 | } | 465 | } |
420 | 466 | ||
421 | #[test] | 467 | #[test] |
422 | fn test_wrap_return_type_not_applicable_when_return_type_is_not_result() { | 468 | fn test_wrap_return_type_not_applicable_when_return_type_is_not_result_or_option() { |
423 | check_no_diagnostics( | 469 | check_no_diagnostics( |
424 | r#" | 470 | r#" |
425 | //- /main.rs crate:main deps:core | 471 | //- /main.rs crate:main deps:core |
@@ -433,6 +479,9 @@ fn foo() -> SomeOtherEnum { 0 } | |||
433 | pub mod result { | 479 | pub mod result { |
434 | pub enum Result<T, E> { Ok(T), Err(E) } | 480 | pub enum Result<T, E> { Ok(T), Err(E) } |
435 | } | 481 | } |
482 | pub mod option { | ||
483 | pub enum Option<T> { Some(T), None } | ||
484 | } | ||
436 | "#, | 485 | "#, |
437 | ); | 486 | ); |
438 | } | 487 | } |
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 @@ | |||
3 | use hir::{ | 3 | use hir::{ |
4 | db::AstDatabase, | 4 | db::AstDatabase, |
5 | diagnostics::{ | 5 | diagnostics::{ |
6 | Diagnostic, IncorrectCase, MissingFields, MissingOkInTailExpr, NoSuchField, | 6 | Diagnostic, IncorrectCase, MissingFields, MissingOkOrSomeInTailExpr, NoSuchField, |
7 | RemoveThisSemicolon, UnresolvedModule, | 7 | RemoveThisSemicolon, UnresolvedModule, |
8 | }, | 8 | }, |
9 | HasSource, HirDisplay, InFile, Semantics, VariantDef, | 9 | HasSource, HirDisplay, InFile, Semantics, VariantDef, |
@@ -94,15 +94,16 @@ impl DiagnosticWithFix for MissingFields { | |||
94 | } | 94 | } |
95 | } | 95 | } |
96 | 96 | ||
97 | impl DiagnosticWithFix for MissingOkInTailExpr { | 97 | impl DiagnosticWithFix for MissingOkOrSomeInTailExpr { |
98 | fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> { | 98 | fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> { |
99 | let root = sema.db.parse_or_expand(self.file)?; | 99 | let root = sema.db.parse_or_expand(self.file)?; |
100 | let tail_expr = self.expr.to_node(&root); | 100 | let tail_expr = self.expr.to_node(&root); |
101 | let tail_expr_range = tail_expr.syntax().text_range(); | 101 | let tail_expr_range = tail_expr.syntax().text_range(); |
102 | let edit = TextEdit::replace(tail_expr_range, format!("Ok({})", tail_expr.syntax())); | 102 | let replacement = format!("{}({})", self.required, tail_expr.syntax()); |
103 | let source_change = | 103 | let edit = TextEdit::replace(tail_expr_range, replacement); |
104 | SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); | 104 | let source_change = SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); |
105 | Some(Fix::new("Wrap with ok", source_change, tail_expr_range)) | 105 | let name = if self.required == "Ok" { "Wrap with Ok" } else { "Wrap with Some" }; |
106 | Some(Fix::new(name, source_change, tail_expr_range)) | ||
106 | } | 107 | } |
107 | } | 108 | } |
108 | 109 | ||