rune/diagnostics/
warning.rs1use core::fmt;
2
3use crate::alloc::String;
4use crate::ast::Span;
5use crate::ast::Spanned;
6use crate::SourceId;
7
8#[derive(Debug)]
11pub struct WarningDiagnostic {
12 pub(crate) source_id: SourceId,
14 pub(crate) kind: WarningDiagnosticKind,
16}
17
18impl WarningDiagnostic {
19 pub fn source_id(&self) -> SourceId {
21 self.source_id
22 }
23
24 #[cfg(feature = "emit")]
26 pub(crate) fn kind(&self) -> &WarningDiagnosticKind {
27 &self.kind
28 }
29
30 #[cfg(test)]
31 pub(crate) fn into_kind(self) -> WarningDiagnosticKind {
32 self.kind
33 }
34
35 #[cfg(feature = "emit")]
37 pub(crate) fn context(&self) -> Option<Span> {
38 match &self.kind {
39 WarningDiagnosticKind::LetPatternMightPanic { context, .. }
40 | WarningDiagnosticKind::RemoveTupleCallParams { context, .. }
41 | WarningDiagnosticKind::NotUsed { context, .. }
42 | WarningDiagnosticKind::UsedDeprecated { context, .. }
43 | WarningDiagnosticKind::TemplateWithoutExpansions { context, .. } => *context,
44 _ => None,
45 }
46 }
47}
48
49impl Spanned for WarningDiagnostic {
50 fn span(&self) -> Span {
52 match &self.kind {
53 WarningDiagnosticKind::NotUsed { span, .. } => *span,
54 WarningDiagnosticKind::Unreachable { span, .. } => *span,
55 WarningDiagnosticKind::LetPatternMightPanic { span, .. } => *span,
56 WarningDiagnosticKind::TemplateWithoutExpansions { span, .. } => *span,
57 WarningDiagnosticKind::RemoveTupleCallParams { span, .. } => *span,
58 WarningDiagnosticKind::UsedDeprecated { span, .. } => *span,
59 }
60 }
61}
62
63impl fmt::Display for WarningDiagnostic {
64 #[inline]
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 fmt::Display::fmt(&self.kind, f)
67 }
68}
69
70impl core::error::Error for WarningDiagnostic {
71 #[inline]
72 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
73 None
74 }
75}
76
77#[derive(Debug)]
79#[allow(missing_docs)]
80#[non_exhaustive]
81pub(crate) enum WarningDiagnosticKind {
82 NotUsed {
84 span: Span,
86 #[cfg_attr(not(feature = "emit"), allow(dead_code))]
88 context: Option<Span>,
89 },
90 Unreachable {
92 span: Span,
94 #[cfg_attr(not(feature = "emit"), allow(dead_code))]
96 cause: Span,
97 },
98 LetPatternMightPanic {
101 span: Span,
103 #[cfg_attr(not(feature = "emit"), allow(dead_code))]
105 context: Option<Span>,
106 },
107 TemplateWithoutExpansions {
109 span: Span,
111 #[cfg_attr(not(feature = "emit"), allow(dead_code))]
113 context: Option<Span>,
114 },
115 RemoveTupleCallParams {
117 span: Span,
119 #[cfg_attr(not(feature = "emit"), allow(dead_code))]
121 variant: Span,
122 #[cfg_attr(not(feature = "emit"), allow(dead_code))]
124 context: Option<Span>,
125 },
126 UsedDeprecated {
127 span: Span,
129 #[cfg_attr(not(feature = "emit"), allow(dead_code))]
131 context: Option<Span>,
132 message: String,
134 },
135}
136
137impl fmt::Display for WarningDiagnosticKind {
138 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139 match self {
140 WarningDiagnosticKind::NotUsed { .. } => write!(f, "Not used"),
141 WarningDiagnosticKind::Unreachable { .. } => write!(f, "Unreachable code"),
142 WarningDiagnosticKind::LetPatternMightPanic { .. } => {
143 write!(f, "Pattern might panic")
144 }
145 WarningDiagnosticKind::TemplateWithoutExpansions { .. } => write!(
146 f,
147 "Using a template string without expansions, like `Hello World`"
148 ),
149 WarningDiagnosticKind::RemoveTupleCallParams { .. } => {
150 write!(f, "Call paramters are not needed here")
151 }
152 WarningDiagnosticKind::UsedDeprecated { message, .. } => {
153 write!(f, "Used deprecated function: {message}")
154 }
155 }
156 }
157}