From 886b66cd03cbe7cb13e248d7c7bbdeba66c7796a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 19:51:19 +0300 Subject: internal: refactor BreakOutsideOfLoop diagnostic --- .../ide/src/diagnostics/break_outside_of_loop.rs | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 crates/ide/src/diagnostics/break_outside_of_loop.rs (limited to 'crates/ide/src/diagnostics/break_outside_of_loop.rs') 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 @@ +use crate::diagnostics::{Diagnostic, DiagnosticsContext}; + +// Diagnostic: break-outside-of-loop +// +// This diagnostic is triggered if the `break` keyword is used outside of a loop. +pub(super) fn break_outside_of_loop( + ctx: &DiagnosticsContext<'_>, + d: &hir::BreakOutsideOfLoop, +) -> Diagnostic { + Diagnostic::new( + "break-outside-of-loop", + "break outside of loop", + ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range, + ) +} + +#[cfg(test)] +mod tests { + use crate::diagnostics::tests::check_diagnostics; + + #[test] + fn break_outside_of_loop() { + check_diagnostics( + r#" +fn foo() { break; } + //^^^^^ break outside of loop +"#, + ); + } +} -- cgit v1.2.3