mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-24 07:44:10 +00:00
internal: refactor BreakOutsideOfLoop diagnostic
This commit is contained in:
parent
7166e8549b
commit
886b66cd03
@ -32,6 +32,7 @@ macro_rules! diagnostics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
diagnostics![
|
diagnostics![
|
||||||
|
BreakOutsideOfLoop,
|
||||||
InactiveCode,
|
InactiveCode,
|
||||||
MacroError,
|
MacroError,
|
||||||
MissingFields,
|
MissingFields,
|
||||||
@ -98,28 +99,9 @@ pub struct NoSuchField {
|
|||||||
pub field: InFile<AstPtr<ast::RecordExprField>>,
|
pub field: InFile<AstPtr<ast::RecordExprField>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Diagnostic: break-outside-of-loop
|
|
||||||
//
|
|
||||||
// This diagnostic is triggered if the `break` keyword is used outside of a loop.
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct BreakOutsideOfLoop {
|
pub struct BreakOutsideOfLoop {
|
||||||
pub file: HirFileId,
|
pub expr: InFile<AstPtr<ast::Expr>>,
|
||||||
pub expr: AstPtr<ast::Expr>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Diagnostic for BreakOutsideOfLoop {
|
|
||||||
fn code(&self) -> DiagnosticCode {
|
|
||||||
DiagnosticCode("break-outside-of-loop")
|
|
||||||
}
|
|
||||||
fn message(&self) -> String {
|
|
||||||
"break outside of loop".to_string()
|
|
||||||
}
|
|
||||||
fn display_source(&self) -> InFile<SyntaxNodePtr> {
|
|
||||||
InFile { file_id: self.file, value: self.expr.clone().into() }
|
|
||||||
}
|
|
||||||
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Diagnostic: missing-unsafe
|
// Diagnostic: missing-unsafe
|
||||||
|
@ -1080,10 +1080,10 @@ impl Function {
|
|||||||
acc.push(NoSuchField { field }.into())
|
acc.push(NoSuchField { field }.into())
|
||||||
}
|
}
|
||||||
hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr } => {
|
hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr } => {
|
||||||
let ptr = source_map
|
let expr = source_map
|
||||||
.expr_syntax(*expr)
|
.expr_syntax(*expr)
|
||||||
.expect("break outside of loop in synthetic syntax");
|
.expect("break outside of loop in synthetic syntax");
|
||||||
sink.push(BreakOutsideOfLoop { file: ptr.file_id, expr: ptr.value })
|
acc.push(BreakOutsideOfLoop { expr }.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
//! macro-expanded files, but we need to present them to the users in terms of
|
//! macro-expanded files, but we need to present them to the users in terms of
|
||||||
//! original files. So we need to map the ranges.
|
//! original files. So we need to map the ranges.
|
||||||
|
|
||||||
|
mod break_outside_of_loop;
|
||||||
mod inactive_code;
|
mod inactive_code;
|
||||||
mod macro_error;
|
mod macro_error;
|
||||||
mod missing_fields;
|
mod missing_fields;
|
||||||
@ -218,6 +219,7 @@ pub(crate) fn diagnostics(
|
|||||||
for diag in diags {
|
for diag in diags {
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
let d = match diag {
|
let d = match diag {
|
||||||
|
AnyDiagnostic::BreakOutsideOfLoop(d) => break_outside_of_loop::break_outside_of_loop(&ctx, &d),
|
||||||
AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),
|
AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),
|
||||||
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
|
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
|
||||||
AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d),
|
AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d),
|
||||||
@ -711,16 +713,6 @@ mod foo;
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn break_outside_of_loop() {
|
|
||||||
check_diagnostics(
|
|
||||||
r#"
|
|
||||||
fn foo() { break; }
|
|
||||||
//^^^^^ break outside of loop
|
|
||||||
"#,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn missing_unsafe_diagnostic_with_raw_ptr() {
|
fn missing_unsafe_diagnostic_with_raw_ptr() {
|
||||||
check_diagnostics(
|
check_diagnostics(
|
||||||
|
30
crates/ide/src/diagnostics/break_outside_of_loop.rs
Normal file
30
crates/ide/src/diagnostics/break_outside_of_loop.rs
Normal file
@ -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
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user