diff options
author | Aleksey Kladov <[email protected]> | 2021-06-13 17:51:19 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-06-13 17:51:19 +0100 |
commit | 886b66cd03cbe7cb13e248d7c7bbdeba66c7796a (patch) | |
tree | 4479618bebc4f3b6bcc6159ad887cfc1591091b4 /crates | |
parent | 7166e8549bad95b05f66acd508d07a6cd97d3dc0 (diff) |
internal: refactor BreakOutsideOfLoop diagnostic
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir/src/diagnostics.rs | 22 | ||||
-rw-r--r-- | crates/hir/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ide/src/diagnostics.rs | 12 | ||||
-rw-r--r-- | crates/ide/src/diagnostics/break_outside_of_loop.rs | 30 |
4 files changed, 36 insertions, 32 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index c7702e09f..47d17ba70 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs | |||
@@ -32,6 +32,7 @@ macro_rules! diagnostics { | |||
32 | } | 32 | } |
33 | 33 | ||
34 | diagnostics![ | 34 | diagnostics![ |
35 | BreakOutsideOfLoop, | ||
35 | InactiveCode, | 36 | InactiveCode, |
36 | MacroError, | 37 | MacroError, |
37 | MissingFields, | 38 | MissingFields, |
@@ -98,28 +99,9 @@ pub struct NoSuchField { | |||
98 | pub field: InFile<AstPtr<ast::RecordExprField>>, | 99 | pub field: InFile<AstPtr<ast::RecordExprField>>, |
99 | } | 100 | } |
100 | 101 | ||
101 | // Diagnostic: break-outside-of-loop | ||
102 | // | ||
103 | // This diagnostic is triggered if the `break` keyword is used outside of a loop. | ||
104 | #[derive(Debug)] | 102 | #[derive(Debug)] |
105 | pub struct BreakOutsideOfLoop { | 103 | pub struct BreakOutsideOfLoop { |
106 | pub file: HirFileId, | 104 | pub expr: InFile<AstPtr<ast::Expr>>, |
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 | } | ||
123 | } | 105 | } |
124 | 106 | ||
125 | // Diagnostic: missing-unsafe | 107 | // Diagnostic: missing-unsafe |
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 7faf6ec1f..2f507b83b 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -1080,10 +1080,10 @@ impl Function { | |||
1080 | acc.push(NoSuchField { field }.into()) | 1080 | acc.push(NoSuchField { field }.into()) |
1081 | } | 1081 | } |
1082 | hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr } => { | 1082 | hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr } => { |
1083 | let ptr = source_map | 1083 | let expr = source_map |
1084 | .expr_syntax(*expr) | 1084 | .expr_syntax(*expr) |
1085 | .expect("break outside of loop in synthetic syntax"); | 1085 | .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()) |
1087 | } | 1087 | } |
1088 | } | 1088 | } |
1089 | } | 1089 | } |
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index caaa89e0a..e8f22c889 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -4,6 +4,7 @@ | |||
4 | //! macro-expanded files, but we need to present them to the users in terms of | 4 | //! macro-expanded files, but we need to present them to the users in terms of |
5 | //! original files. So we need to map the ranges. | 5 | //! original files. So we need to map the ranges. |
6 | 6 | ||
7 | mod break_outside_of_loop; | ||
7 | mod inactive_code; | 8 | mod inactive_code; |
8 | mod macro_error; | 9 | mod macro_error; |
9 | mod missing_fields; | 10 | mod missing_fields; |
@@ -218,6 +219,7 @@ pub(crate) fn diagnostics( | |||
218 | for diag in diags { | 219 | for diag in diags { |
219 | #[rustfmt::skip] | 220 | #[rustfmt::skip] |
220 | let d = match diag { | 221 | let d = match diag { |
222 | AnyDiagnostic::BreakOutsideOfLoop(d) => break_outside_of_loop::break_outside_of_loop(&ctx, &d), | ||
221 | AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d), | 223 | AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d), |
222 | AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), | 224 | AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), |
223 | AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d), | 225 | AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d), |
@@ -712,16 +714,6 @@ mod foo; | |||
712 | } | 714 | } |
713 | 715 | ||
714 | #[test] | 716 | #[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 | |||
724 | #[test] | ||
725 | fn missing_unsafe_diagnostic_with_raw_ptr() { | 717 | fn missing_unsafe_diagnostic_with_raw_ptr() { |
726 | check_diagnostics( | 718 | check_diagnostics( |
727 | r#" | 719 | r#" |
diff --git a/crates/ide/src/diagnostics/break_outside_of_loop.rs b/crates/ide/src/diagnostics/break_outside_of_loop.rs new file mode 100644 index 000000000..80e68f3cc --- /dev/null +++ b/crates/ide/src/diagnostics/break_outside_of_loop.rs | |||
@@ -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 | } | ||