Skip to content

Commit 886b66c

Browse files
committed
internal: refactor BreakOutsideOfLoop diagnostic
1 parent 7166e85 commit 886b66c

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ macro_rules! diagnostics {
3232
}
3333

3434
diagnostics![
35+
BreakOutsideOfLoop,
3536
InactiveCode,
3637
MacroError,
3738
MissingFields,
@@ -98,28 +99,9 @@ pub struct NoSuchField {
9899
pub field: InFile<AstPtr<ast::RecordExprField>>,
99100
}
100101

101-
// Diagnostic: break-outside-of-loop
102-
//
103-
// This diagnostic is triggered if the `break` keyword is used outside of a loop.
104102
#[derive(Debug)]
105103
pub struct BreakOutsideOfLoop {
106-
pub file: HirFileId,
107-
pub expr: AstPtr<ast::Expr>,
108-
}
109-
110-
impl Diagnostic for BreakOutsideOfLoop {
111-
fn code(&self) -> DiagnosticCode {
112-
DiagnosticCode("break-outside-of-loop")
113-
}
114-
fn message(&self) -> String {
115-
"break outside of loop".to_string()
116-
}
117-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
118-
InFile { file_id: self.file, value: self.expr.clone().into() }
119-
}
120-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
121-
self
122-
}
104+
pub expr: InFile<AstPtr<ast::Expr>>,
123105
}
124106

125107
// Diagnostic: missing-unsafe

crates/hir/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,10 +1080,10 @@ impl Function {
10801080
acc.push(NoSuchField { field }.into())
10811081
}
10821082
hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr } => {
1083-
let ptr = source_map
1083+
let expr = source_map
10841084
.expr_syntax(*expr)
10851085
.expect("break outside of loop in synthetic syntax");
1086-
sink.push(BreakOutsideOfLoop { file: ptr.file_id, expr: ptr.value })
1086+
acc.push(BreakOutsideOfLoop { expr }.into())
10871087
}
10881088
}
10891089
}

crates/ide/src/diagnostics.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! macro-expanded files, but we need to present them to the users in terms of
55
//! original files. So we need to map the ranges.
66
7+
mod break_outside_of_loop;
78
mod inactive_code;
89
mod macro_error;
910
mod missing_fields;
@@ -218,6 +219,7 @@ pub(crate) fn diagnostics(
218219
for diag in diags {
219220
#[rustfmt::skip]
220221
let d = match diag {
222+
AnyDiagnostic::BreakOutsideOfLoop(d) => break_outside_of_loop::break_outside_of_loop(&ctx, &d),
221223
AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),
222224
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
223225
AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d),
@@ -711,16 +713,6 @@ mod foo;
711713
);
712714
}
713715

714-
#[test]
715-
fn break_outside_of_loop() {
716-
check_diagnostics(
717-
r#"
718-
fn foo() { break; }
719-
//^^^^^ break outside of loop
720-
"#,
721-
);
722-
}
723-
724716
#[test]
725717
fn missing_unsafe_diagnostic_with_raw_ptr() {
726718
check_diagnostics(
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
2+
3+
// Diagnostic: break-outside-of-loop
4+
//
5+
// This diagnostic is triggered if the `break` keyword is used outside of a loop.
6+
pub(super) fn break_outside_of_loop(
7+
ctx: &DiagnosticsContext<'_>,
8+
d: &hir::BreakOutsideOfLoop,
9+
) -> Diagnostic {
10+
Diagnostic::new(
11+
"break-outside-of-loop",
12+
"break outside of loop",
13+
ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range,
14+
)
15+
}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use crate::diagnostics::tests::check_diagnostics;
20+
21+
#[test]
22+
fn break_outside_of_loop() {
23+
check_diagnostics(
24+
r#"
25+
fn foo() { break; }
26+
//^^^^^ break outside of loop
27+
"#,
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)