From 6620949caee25128416acf285590b0d5558e4597 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sat, 17 Aug 2019 06:31:53 +0100 Subject: Simplify checking return type, add new test --- crates/ra_ide_api/src/diagnostics.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 0b9bb5a66..94424dc16 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -79,7 +79,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec .on::(|d| { let node = d.ast(db); let mut builder = TextEditBuilder::default(); - let replacement = format!("Ok({})", node.syntax().text()); + let replacement = format!("Ok({})", node.syntax()); builder.replace(node.syntax().text_range(), replacement); let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, builder.finish()); res.borrow_mut().push(Diagnostic { @@ -353,7 +353,7 @@ fn div(x: i32, y: i32) -> MyResult { } #[test] - fn test_wrap_return_type_not_applicable() { + fn test_wrap_return_type_not_applicable_when_expr_type_does_not_match_ok_type() { let content = r#" //- /main.rs use std::{string::String, result::Result::{self, Ok, Err}}; @@ -373,6 +373,32 @@ fn div(x: i32, y: i32) -> MyResult { check_no_diagnostic_for_target_file("/main.rs", content); } + #[test] + fn test_wrap_return_type_not_applicable_when_return_type_is_not_result() { + let content = r#" + //- /main.rs + use std::{string::String, result::Result::{self, Ok, Err}}; + + enum SomeOtherEnum { + Ok(i32), + Err(String), + } + + fn foo() -> SomeOtherEnum { + 0 + } + + //- /std/lib.rs + pub mod string { + pub struct String { } + } + pub mod result { + pub enum Result { Ok(T), Err(E) } + } + "#; + check_no_diagnostic_for_target_file("/main.rs", content); + } + #[test] fn test_fill_struct_fields_empty() { let before = r" -- cgit v1.2.3