Skip to content

Commit db2a989

Browse files
committed
internal: don't use #[should_panic] for tests
1 parent 06a633f commit db2a989

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

crates/ide/src/goto_definition.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ mod tests {
110110
assert_eq!(expected, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() });
111111
}
112112

113+
fn check_unresolved(ra_fixture: &str) {
114+
let (analysis, position) = fixture::position(ra_fixture);
115+
let navs = analysis.goto_definition(position).unwrap().expect("no definition found").info;
116+
117+
assert!(navs.is_empty(), "didn't expect this to resolve anywhere: {:?}", navs)
118+
}
119+
113120
#[test]
114121
fn goto_def_for_extern_crate() {
115122
check(
@@ -927,17 +934,12 @@ fn f() -> impl Iterator<Item$0 = u8> {}
927934
}
928935

929936
#[test]
930-
#[should_panic = "unresolved reference"]
931937
fn unknown_assoc_ty() {
932-
check(
938+
check_unresolved(
933939
r#"
934-
trait Iterator {
935-
type Item;
936-
//^^^^
937-
}
938-
940+
trait Iterator { type Item; }
939941
fn f() -> impl Iterator<Invalid$0 = u8> {}
940-
"#,
942+
"#,
941943
)
942944
}
943945

crates/ide_assists/src/handlers/flip_comma.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,12 @@ mod tests {
6666
}
6767

6868
#[test]
69-
#[should_panic]
7069
fn flip_comma_before_punct() {
7170
// See https://github.com/rust-analyzer/rust-analyzer/issues/1619
7271
// "Flip comma" assist shouldn't be applicable to the last comma in enum or struct
7372
// declaration body.
74-
check_assist_target(
75-
flip_comma,
76-
"pub enum Test { \
77-
A,$0 \
78-
}",
79-
",",
80-
);
81-
82-
check_assist_target(
83-
flip_comma,
84-
"pub struct Test { \
85-
foo: usize,$0 \
86-
}",
87-
",",
88-
);
73+
check_assist_not_applicable(flip_comma, "pub enum Test { A,$0 }");
74+
check_assist_not_applicable(flip_comma, "pub struct Test { foo: usize,$0 }");
8975
}
9076

9177
#[test]

docs/dev/style.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ Do not reuse marks between several tests.
152152

153153
**Rationale:** marks provide an easy way to find the canonical test for each bit of code.
154154
This makes it much easier to understand.
155+
More than one mark per test / code branch doesn't add significantly to understanding.
156+
157+
## `#[should_panic]`
158+
159+
Do not use `#[should_panic]` tests.
160+
Instead, explicitly check for `None`, `Err`, etc.
161+
162+
**Rationale:**a `#[should_panic]` is a tool for library authors, to makes sure that API does not fail silently, when misused.
163+
`rust-analyzer` is not a library, we don't need to test for API misuse, and we have to handle any user input without panics.
164+
Panic messages in the logs from the `#[should_panic]` tests are confusing.
155165

156166
## Function Preconditions
157167

0 commit comments

Comments
 (0)