Skip to content

Commit 602acfc

Browse files
committed
fix conflicting "unnecessary else" and "trailing return" diagnostics tests
1 parent a250c2d commit 602acfc

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ diagnostics![
6767
NoSuchField,
6868
PrivateAssocItem,
6969
PrivateField,
70-
ReplaceFilterMapNextWithFindMap,
7170
RemoveTrailingReturn,
7271
RemoveUnnecessaryElse,
72+
ReplaceFilterMapNextWithFindMap,
7373
TraitImplIncorrectSafety,
7474
TraitImplMissingAssocItems,
7575
TraitImplOrphan,

crates/ide-diagnostics/src/handlers/remove_trailing_return.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveTrailingReturn) -> Option<Vec<A
5454

5555
#[cfg(test)]
5656
mod tests {
57-
use crate::tests::{check_diagnostics, check_fix};
57+
use crate::tests::{
58+
check_diagnostics, check_diagnostics_with_disabled, check_fix, check_fix_with_disabled,
59+
};
5860

5961
#[test]
6062
fn remove_trailing_return() {
@@ -127,7 +129,7 @@ fn foo() -> u8 {
127129

128130
#[test]
129131
fn remove_trailing_return_in_if() {
130-
check_diagnostics(
132+
check_diagnostics_with_disabled(
131133
r#"
132134
fn foo(x: usize) -> u8 {
133135
if x > 0 {
@@ -138,6 +140,7 @@ fn foo(x: usize) -> u8 {
138140
} //^^^^^^^^^ 💡 weak: replace return <expr>; with <expr>
139141
}
140142
"#,
143+
std::iter::once("remove-unnecessary-else".to_string()),
141144
);
142145
}
143146

@@ -287,7 +290,7 @@ fn foo() -> u8 {
287290

288291
#[test]
289292
fn replace_in_if() {
290-
check_fix(
293+
check_fix_with_disabled(
291294
r#"
292295
fn foo(x: usize) -> u8 {
293296
if x > 0 {
@@ -306,6 +309,7 @@ fn foo(x: usize) -> u8 {
306309
}
307310
}
308311
"#,
312+
std::iter::once("remove-unnecessary-else".to_string()),
309313
);
310314
check_fix(
311315
r#"

crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,15 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveUnnecessaryElse) -> Option<Vec<
8787

8888
#[cfg(test)]
8989
mod tests {
90-
use crate::tests::{check_diagnostics, check_fix};
90+
use crate::tests::{check_diagnostics, check_diagnostics_with_disabled, check_fix};
91+
92+
fn check_diagnostics_with_needless_return_disabled(ra_fixture: &str) {
93+
check_diagnostics_with_disabled(ra_fixture, std::iter::once("needless_return".to_string()));
94+
}
9195

9296
#[test]
9397
fn remove_unnecessary_else_for_return() {
94-
check_diagnostics(
98+
check_diagnostics_with_needless_return_disabled(
9599
r#"
96100
fn test() {
97101
if foo {
@@ -126,7 +130,7 @@ fn test() {
126130

127131
#[test]
128132
fn remove_unnecessary_else_for_return2() {
129-
check_diagnostics(
133+
check_diagnostics_with_needless_return_disabled(
130134
r#"
131135
fn test() {
132136
if foo {
@@ -169,7 +173,7 @@ fn test() {
169173

170174
#[test]
171175
fn remove_unnecessary_else_for_return_in_child_if_expr() {
172-
check_diagnostics(
176+
check_diagnostics_with_needless_return_disabled(
173177
r#"
174178
fn test() {
175179
if foo {
@@ -371,7 +375,7 @@ fn test() {
371375

372376
#[test]
373377
fn no_diagnostic_if_no_divergence_in_else_branch() {
374-
check_diagnostics(
378+
check_diagnostics_with_needless_return_disabled(
375379
r#"
376380
fn test() {
377381
if foo {

crates/ide-diagnostics/src/tests.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,35 @@ pub(crate) fn check_fixes(ra_fixture_before: &str, ra_fixtures_after: Vec<&str>)
3434

3535
#[track_caller]
3636
fn check_nth_fix(nth: usize, ra_fixture_before: &str, ra_fixture_after: &str) {
37+
let mut config = DiagnosticsConfig::test_sample();
38+
config.expr_fill_default = ExprFillDefaultMode::Default;
39+
check_nth_fix_with_config(config, nth, ra_fixture_before, ra_fixture_after)
40+
}
41+
42+
#[track_caller]
43+
pub(crate) fn check_fix_with_disabled(
44+
ra_fixture_before: &str,
45+
ra_fixture_after: &str,
46+
disabled: impl Iterator<Item = String>,
47+
) {
48+
let mut config = DiagnosticsConfig::test_sample();
49+
config.expr_fill_default = ExprFillDefaultMode::Default;
50+
config.disabled.extend(disabled);
51+
check_nth_fix_with_config(config, 0, ra_fixture_before, ra_fixture_after)
52+
}
53+
54+
#[track_caller]
55+
fn check_nth_fix_with_config(
56+
config: DiagnosticsConfig,
57+
nth: usize,
58+
ra_fixture_before: &str,
59+
ra_fixture_after: &str,
60+
) {
3761
let after = trim_indent(ra_fixture_after);
3862

3963
let (db, file_position) = RootDatabase::with_position(ra_fixture_before);
40-
let mut conf = DiagnosticsConfig::test_sample();
41-
conf.expr_fill_default = ExprFillDefaultMode::Default;
4264
let diagnostic =
43-
super::diagnostics(&db, &conf, &AssistResolveStrategy::All, file_position.file_id)
65+
super::diagnostics(&db, &config, &AssistResolveStrategy::All, file_position.file_id)
4466
.pop()
4567
.expect("no diagnostics");
4668
let fix = &diagnostic

0 commit comments

Comments
 (0)