Skip to content

Commit

Permalink
fix conflicting "unnecessary else" and "trailing return" diagnostics …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
davidsemakula committed Feb 8, 2024
1 parent a250c2d commit 602acfc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/hir/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ diagnostics![
NoSuchField,
PrivateAssocItem,
PrivateField,
ReplaceFilterMapNextWithFindMap,
RemoveTrailingReturn,
RemoveUnnecessaryElse,
ReplaceFilterMapNextWithFindMap,
TraitImplIncorrectSafety,
TraitImplMissingAssocItems,
TraitImplOrphan,
Expand Down
10 changes: 7 additions & 3 deletions crates/ide-diagnostics/src/handlers/remove_trailing_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveTrailingReturn) -> Option<Vec<A

#[cfg(test)]
mod tests {
use crate::tests::{check_diagnostics, check_fix};
use crate::tests::{
check_diagnostics, check_diagnostics_with_disabled, check_fix, check_fix_with_disabled,
};

#[test]
fn remove_trailing_return() {
Expand Down Expand Up @@ -127,7 +129,7 @@ fn foo() -> u8 {

#[test]
fn remove_trailing_return_in_if() {
check_diagnostics(
check_diagnostics_with_disabled(
r#"
fn foo(x: usize) -> u8 {
if x > 0 {
Expand All @@ -138,6 +140,7 @@ fn foo(x: usize) -> u8 {
} //^^^^^^^^^ 💡 weak: replace return <expr>; with <expr>
}
"#,
std::iter::once("remove-unnecessary-else".to_string()),
);
}

Expand Down Expand Up @@ -287,7 +290,7 @@ fn foo() -> u8 {

#[test]
fn replace_in_if() {
check_fix(
check_fix_with_disabled(
r#"
fn foo(x: usize) -> u8 {
if x > 0 {
Expand All @@ -306,6 +309,7 @@ fn foo(x: usize) -> u8 {
}
}
"#,
std::iter::once("remove-unnecessary-else".to_string()),
);
check_fix(
r#"
Expand Down
14 changes: 9 additions & 5 deletions crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveUnnecessaryElse) -> Option<Vec<

#[cfg(test)]
mod tests {
use crate::tests::{check_diagnostics, check_fix};
use crate::tests::{check_diagnostics, check_diagnostics_with_disabled, check_fix};

fn check_diagnostics_with_needless_return_disabled(ra_fixture: &str) {
check_diagnostics_with_disabled(ra_fixture, std::iter::once("needless_return".to_string()));
}

#[test]
fn remove_unnecessary_else_for_return() {
check_diagnostics(
check_diagnostics_with_needless_return_disabled(
r#"
fn test() {
if foo {
Expand Down Expand Up @@ -126,7 +130,7 @@ fn test() {

#[test]
fn remove_unnecessary_else_for_return2() {
check_diagnostics(
check_diagnostics_with_needless_return_disabled(
r#"
fn test() {
if foo {
Expand Down Expand Up @@ -169,7 +173,7 @@ fn test() {

#[test]
fn remove_unnecessary_else_for_return_in_child_if_expr() {
check_diagnostics(
check_diagnostics_with_needless_return_disabled(
r#"
fn test() {
if foo {
Expand Down Expand Up @@ -371,7 +375,7 @@ fn test() {

#[test]
fn no_diagnostic_if_no_divergence_in_else_branch() {
check_diagnostics(
check_diagnostics_with_needless_return_disabled(
r#"
fn test() {
if foo {
Expand Down
28 changes: 25 additions & 3 deletions crates/ide-diagnostics/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,35 @@ pub(crate) fn check_fixes(ra_fixture_before: &str, ra_fixtures_after: Vec<&str>)

#[track_caller]
fn check_nth_fix(nth: usize, ra_fixture_before: &str, ra_fixture_after: &str) {
let mut config = DiagnosticsConfig::test_sample();
config.expr_fill_default = ExprFillDefaultMode::Default;
check_nth_fix_with_config(config, nth, ra_fixture_before, ra_fixture_after)
}

#[track_caller]
pub(crate) fn check_fix_with_disabled(
ra_fixture_before: &str,
ra_fixture_after: &str,
disabled: impl Iterator<Item = String>,
) {
let mut config = DiagnosticsConfig::test_sample();
config.expr_fill_default = ExprFillDefaultMode::Default;
config.disabled.extend(disabled);
check_nth_fix_with_config(config, 0, ra_fixture_before, ra_fixture_after)
}

#[track_caller]
fn check_nth_fix_with_config(
config: DiagnosticsConfig,
nth: usize,
ra_fixture_before: &str,
ra_fixture_after: &str,
) {
let after = trim_indent(ra_fixture_after);

let (db, file_position) = RootDatabase::with_position(ra_fixture_before);
let mut conf = DiagnosticsConfig::test_sample();
conf.expr_fill_default = ExprFillDefaultMode::Default;
let diagnostic =
super::diagnostics(&db, &conf, &AssistResolveStrategy::All, file_position.file_id)
super::diagnostics(&db, &config, &AssistResolveStrategy::All, file_position.file_id)
.pop()
.expect("no diagnostics");
let fix = &diagnostic
Expand Down

0 comments on commit 602acfc

Please sign in to comment.