Skip to content

Commit 6620949

Browse files
theotherphilmatklad
authored andcommitted
Simplify checking return type, add new test
1 parent 6a04e9c commit 6620949

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

crates/ra_hir/src/expr/validation.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ use ra_syntax::ast::{AstNode, RecordLit};
66
use super::{Expr, ExprId, RecordLitField};
77
use crate::{
88
adt::AdtDef,
9-
code_model::Enum,
109
diagnostics::{DiagnosticSink, MissingFields, MissingOkInTailExpr},
1110
expr::AstPtr,
1211
name,
1312
path::{PathKind, PathSegment},
14-
ty::{InferenceResult, Ty, TypeCtor},
13+
ty::{ApplicationTy, InferenceResult, Ty, TypeCtor},
1514
Function, HasSource, HirDatabase, ModuleDef, Name, Path, PerNs, Resolution,
1615
};
1716
use ra_syntax::ast;
@@ -120,28 +119,12 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
120119
_ => return,
121120
};
122121

123-
let std_result_type = std_result_enum.ty(db);
124-
125-
fn enum_from_type(ty: &Ty) -> Option<Enum> {
126-
match ty {
127-
Ty::Apply(t) => match t.ctor {
128-
TypeCtor::Adt(AdtDef::Enum(e)) => Some(e),
129-
_ => None,
130-
},
131-
_ => None,
132-
}
133-
}
134-
135-
if enum_from_type(&mismatch.expected) != enum_from_type(&std_result_type) {
136-
return;
137-
}
138-
139-
let ret = match &mismatch.expected {
140-
Ty::Apply(t) => t,
122+
let std_result_ctor = TypeCtor::Adt(AdtDef::Enum(std_result_enum));
123+
let params = match &mismatch.expected {
124+
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &std_result_ctor => parameters,
141125
_ => return,
142126
};
143127

144-
let params = &ret.parameters;
145128
if params.len() == 2 && &params[0] == &mismatch.actual {
146129
let source_map = self.func.body_source_map(db);
147130
let file_id = self.func.source(db).file_id;

crates/ra_ide_api/src/diagnostics.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
7979
.on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| {
8080
let node = d.ast(db);
8181
let mut builder = TextEditBuilder::default();
82-
let replacement = format!("Ok({})", node.syntax().text());
82+
let replacement = format!("Ok({})", node.syntax());
8383
builder.replace(node.syntax().text_range(), replacement);
8484
let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, builder.finish());
8585
res.borrow_mut().push(Diagnostic {
@@ -353,7 +353,7 @@ fn div(x: i32, y: i32) -> MyResult<i32> {
353353
}
354354

355355
#[test]
356-
fn test_wrap_return_type_not_applicable() {
356+
fn test_wrap_return_type_not_applicable_when_expr_type_does_not_match_ok_type() {
357357
let content = r#"
358358
//- /main.rs
359359
use std::{string::String, result::Result::{self, Ok, Err}};
@@ -373,6 +373,32 @@ fn div(x: i32, y: i32) -> MyResult<i32> {
373373
check_no_diagnostic_for_target_file("/main.rs", content);
374374
}
375375

376+
#[test]
377+
fn test_wrap_return_type_not_applicable_when_return_type_is_not_result() {
378+
let content = r#"
379+
//- /main.rs
380+
use std::{string::String, result::Result::{self, Ok, Err}};
381+
382+
enum SomeOtherEnum {
383+
Ok(i32),
384+
Err(String),
385+
}
386+
387+
fn foo() -> SomeOtherEnum {
388+
0
389+
}
390+
391+
//- /std/lib.rs
392+
pub mod string {
393+
pub struct String { }
394+
}
395+
pub mod result {
396+
pub enum Result<T, E> { Ok(T), Err(E) }
397+
}
398+
"#;
399+
check_no_diagnostic_for_target_file("/main.rs", content);
400+
}
401+
376402
#[test]
377403
fn test_fill_struct_fields_empty() {
378404
let before = r"

0 commit comments

Comments
 (0)