Skip to content

Commit

Permalink
Improve expected_type when returning an atom
Browse files Browse the repository at this point in the history
Summary: When eqwalizer reports the return type as an atom, and the spec has a simple type only, add an assist to make the return type the atom.

Reviewed By: jcpetruzza

Differential Revision: D59853168

fbshipit-source-id: bdd55b25449ade74ed4de9ee7c39ecd5c4f52003
  • Loading branch information
alanz authored and facebook-github-bot committed Jul 17, 2024
1 parent 3791250 commit a168778
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/ide/src/diagnostics/eqwalizer_assists/expected_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ pub fn expected_type(
_ => {}
}
}

// Anything in spec, atom in body
(_other, Type::AtomLitType(_)) => {
add_spec_fix(sema, file_id, got, diagnostic);
}

_ => {}
}
}
Expand Down Expand Up @@ -119,6 +125,12 @@ fn add_spec_fix(
}
_ => {}
},
TypeExpr::Call { .. } => {
if let &Type::AtomLitType(_) = got {
let fix_label = format!("Update function spec to return '{got}'");
make_spec_fix(sema, file_id, spec_id, sig, fix_label, got, diagnostic)?;
}
}
_ => {}
}
}
Expand Down Expand Up @@ -270,4 +282,26 @@ mod tests {
"#]],
)
}

#[test]
fn mismatched_integer_fix_spec() {
check_specific_fix(
"Update function spec to return 'ok'",
r#"
//- eqwalizer
//- /play/src/bar.erl app:play
-module(bar).
-spec foo() -> integer().
foo() -> o~k.
%% ^^ 💡 error: eqwalizer: incompatible_types
"#,
expect![[r#"
-module(bar).
-spec foo() -> ok.
foo() -> ok.
"#]],
)
}
}

0 comments on commit a168778

Please sign in to comment.