mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Initial implementation of Ok-wrapping
This commit is contained in:
parent
fdece911fe
commit
d00a285fa7
4 changed files with 136 additions and 3 deletions
|
@ -75,6 +75,19 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
|||
severity: Severity::Error,
|
||||
fix: Some(fix),
|
||||
})
|
||||
})
|
||||
.on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| {
|
||||
let node = d.ast(db);
|
||||
let mut builder = TextEditBuilder::default();
|
||||
let replacement = format!("Ok({})", node.syntax().text());
|
||||
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 {
|
||||
range: d.highlight_range(),
|
||||
message: d.message(),
|
||||
severity: Severity::Error,
|
||||
fix: Some(fix),
|
||||
})
|
||||
});
|
||||
if let Some(m) = source_binder::module_from_file_id(db, file_id) {
|
||||
m.diagnostics(db, &mut sink);
|
||||
|
@ -218,6 +231,43 @@ mod tests {
|
|||
assert_eq!(diagnostics.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_return_type() {
|
||||
let before = r#"
|
||||
enum Result<T, E> { Ok(T), Err(E) }
|
||||
struct String { }
|
||||
|
||||
fn div(x: i32, y: i32) -> Result<i32, String> {
|
||||
if y == 0 {
|
||||
return Err("div by zero".into());
|
||||
}
|
||||
x / y
|
||||
}
|
||||
"#;
|
||||
let after = r#"
|
||||
enum Result<T, E> { Ok(T), Err(E) }
|
||||
struct String { }
|
||||
|
||||
fn div(x: i32, y: i32) -> Result<i32, String> {
|
||||
if y == 0 {
|
||||
return Err("div by zero".into());
|
||||
}
|
||||
Ok(x / y)
|
||||
}
|
||||
"#;
|
||||
check_apply_diagnostic_fix(before, after);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_return_type_not_applicable() {
|
||||
let content = r#"
|
||||
fn foo() -> Result<String, i32> {
|
||||
0
|
||||
}
|
||||
"#;
|
||||
check_no_diagnostic(content);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fill_struct_fields_empty() {
|
||||
let before = r"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue