rune/diagnostics/
warning.rsuse core::fmt;
use crate::alloc::String;
use crate::ast::Span;
use crate::ast::Spanned;
use crate::SourceId;
#[derive(Debug)]
pub struct WarningDiagnostic {
pub(crate) source_id: SourceId,
pub(crate) kind: WarningDiagnosticKind,
}
impl WarningDiagnostic {
pub fn source_id(&self) -> SourceId {
self.source_id
}
#[cfg(feature = "emit")]
pub(crate) fn kind(&self) -> &WarningDiagnosticKind {
&self.kind
}
#[cfg(test)]
pub(crate) fn into_kind(self) -> WarningDiagnosticKind {
self.kind
}
#[cfg(feature = "emit")]
pub(crate) fn context(&self) -> Option<Span> {
match &self.kind {
WarningDiagnosticKind::LetPatternMightPanic { context, .. }
| WarningDiagnosticKind::RemoveTupleCallParams { context, .. }
| WarningDiagnosticKind::NotUsed { context, .. }
| WarningDiagnosticKind::UsedDeprecated { context, .. }
| WarningDiagnosticKind::TemplateWithoutExpansions { context, .. } => *context,
_ => None,
}
}
}
impl Spanned for WarningDiagnostic {
fn span(&self) -> Span {
match &self.kind {
WarningDiagnosticKind::NotUsed { span, .. } => *span,
WarningDiagnosticKind::Unreachable { span, .. } => *span,
WarningDiagnosticKind::LetPatternMightPanic { span, .. } => *span,
WarningDiagnosticKind::TemplateWithoutExpansions { span, .. } => *span,
WarningDiagnosticKind::RemoveTupleCallParams { span, .. } => *span,
WarningDiagnosticKind::UnnecessarySemiColon { span, .. } => *span,
WarningDiagnosticKind::UsedDeprecated { span, .. } => *span,
}
}
}
impl fmt::Display for WarningDiagnostic {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.kind, f)
}
}
impl core::error::Error for WarningDiagnostic {
#[inline]
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
None
}
}
#[derive(Debug)]
#[allow(missing_docs)]
#[non_exhaustive]
pub(crate) enum WarningDiagnosticKind {
NotUsed {
span: Span,
#[cfg_attr(not(feature = "emit"), allow(dead_code))]
context: Option<Span>,
},
Unreachable {
span: Span,
#[cfg_attr(not(feature = "emit"), allow(dead_code))]
cause: Span,
},
LetPatternMightPanic {
span: Span,
#[cfg_attr(not(feature = "emit"), allow(dead_code))]
context: Option<Span>,
},
TemplateWithoutExpansions {
span: Span,
#[cfg_attr(not(feature = "emit"), allow(dead_code))]
context: Option<Span>,
},
RemoveTupleCallParams {
span: Span,
#[cfg_attr(not(feature = "emit"), allow(dead_code))]
variant: Span,
#[cfg_attr(not(feature = "emit"), allow(dead_code))]
context: Option<Span>,
},
UnnecessarySemiColon {
span: Span,
},
UsedDeprecated {
span: Span,
#[cfg_attr(not(feature = "emit"), allow(dead_code))]
context: Option<Span>,
message: String,
},
}
impl fmt::Display for WarningDiagnosticKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
WarningDiagnosticKind::NotUsed { .. } => write!(f, "Not used"),
WarningDiagnosticKind::Unreachable { .. } => write!(f, "Unreachable code"),
WarningDiagnosticKind::LetPatternMightPanic { .. } => {
write!(f, "Pattern might panic")
}
WarningDiagnosticKind::TemplateWithoutExpansions { .. } => write!(
f,
"Using a template string without expansions, like `Hello World`"
),
WarningDiagnosticKind::RemoveTupleCallParams { .. } => {
write!(f, "Call paramters are not needed here")
}
WarningDiagnosticKind::UnnecessarySemiColon { .. } => {
write!(f, "Unnecessary semicolon")
}
WarningDiagnosticKind::UsedDeprecated { message, .. } => {
write!(f, "Used deprecated function: {message}")
}
}
}
}