aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/diagnostics.rs
diff options
context:
space:
mode:
authorPhil Ellison <[email protected]>2019-08-10 16:40:48 +0100
committerAleksey Kladov <[email protected]>2019-08-25 10:55:55 +0100
commitd00a285fa757307bbe0f8dac9e49ac247cf9dab1 (patch)
tree7ce56753d09f6b71c1a02b3c1b4078a2dd1e02f8 /crates/ra_ide_api/src/diagnostics.rs
parentfdece911fe8e2f3c22760ea22038a6d00cb70dfa (diff)
Initial implementation of Ok-wrapping
Diffstat (limited to 'crates/ra_ide_api/src/diagnostics.rs')
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs
index c2b959cb3..be5197767 100644
--- a/crates/ra_ide_api/src/diagnostics.rs
+++ b/crates/ra_ide_api/src/diagnostics.rs
@@ -75,6 +75,19 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
75 severity: Severity::Error, 75 severity: Severity::Error,
76 fix: Some(fix), 76 fix: Some(fix),
77 }) 77 })
78 })
79 .on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| {
80 let node = d.ast(db);
81 let mut builder = TextEditBuilder::default();
82 let replacement = format!("Ok({})", node.syntax().text());
83 builder.replace(node.syntax().text_range(), replacement);
84 let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, builder.finish());
85 res.borrow_mut().push(Diagnostic {
86 range: d.highlight_range(),
87 message: d.message(),
88 severity: Severity::Error,
89 fix: Some(fix),
90 })
78 }); 91 });
79 if let Some(m) = source_binder::module_from_file_id(db, file_id) { 92 if let Some(m) = source_binder::module_from_file_id(db, file_id) {
80 m.diagnostics(db, &mut sink); 93 m.diagnostics(db, &mut sink);
@@ -219,6 +232,43 @@ mod tests {
219 } 232 }
220 233
221 #[test] 234 #[test]
235 fn test_wrap_return_type() {
236 let before = r#"
237 enum Result<T, E> { Ok(T), Err(E) }
238 struct String { }
239
240 fn div(x: i32, y: i32) -> Result<i32, String> {
241 if y == 0 {
242 return Err("div by zero".into());
243 }
244 x / y
245 }
246 "#;
247 let after = r#"
248 enum Result<T, E> { Ok(T), Err(E) }
249 struct String { }
250
251 fn div(x: i32, y: i32) -> Result<i32, String> {
252 if y == 0 {
253 return Err("div by zero".into());
254 }
255 Ok(x / y)
256 }
257 "#;
258 check_apply_diagnostic_fix(before, after);
259 }
260
261 #[test]
262 fn test_wrap_return_type_not_applicable() {
263 let content = r#"
264 fn foo() -> Result<String, i32> {
265 0
266 }
267 "#;
268 check_no_diagnostic(content);
269 }
270
271 #[test]
222 fn test_fill_struct_fields_empty() { 272 fn test_fill_struct_fields_empty() {
223 let before = r" 273 let before = r"
224 struct TestStruct { 274 struct TestStruct {