fix conflicting "unnecessary else" and "trailing return" diagnostics tests

This commit is contained in:
davidsemakula 2024-02-08 19:29:27 +03:00
parent a250c2dde0
commit 602acfcb70
4 changed files with 42 additions and 12 deletions

View File

@ -67,9 +67,9 @@ fn from(d: $diag) -> AnyDiagnostic {
NoSuchField,
PrivateAssocItem,
PrivateField,
ReplaceFilterMapNextWithFindMap,
RemoveTrailingReturn,
RemoveUnnecessaryElse,
ReplaceFilterMapNextWithFindMap,
TraitImplIncorrectSafety,
TraitImplMissingAssocItems,
TraitImplOrphan,

View File

@ -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() {
@ -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 {
@ -138,6 +140,7 @@ fn foo(x: usize) -> u8 {
} //^^^^^^^^^ 💡 weak: replace return <expr>; with <expr>
}
"#,
std::iter::once("remove-unnecessary-else".to_string()),
);
}
@ -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 {
@ -306,6 +309,7 @@ fn foo(x: usize) -> u8 {
}
}
"#,
std::iter::once("remove-unnecessary-else".to_string()),
);
check_fix(
r#"

View File

@ -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 {
@ -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 {
@ -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 {
@ -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 {

View File

@ -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