Skip to main content

rune/diagnostics/
warning.rs

1use core::fmt;
2
3use crate::alloc::String;
4use crate::ast::Span;
5use crate::ast::Spanned;
6use crate::SourceId;
7
8/// Warning diagnostic emitted during compilation. Warning diagnostics indicates
9/// an recoverable issues.
10#[derive(Debug)]
11pub struct WarningDiagnostic {
12    /// The id of the source where the warning happened.
13    pub(crate) source_id: SourceId,
14    /// The kind of the warning.
15    pub(crate) kind: WarningDiagnosticKind,
16}
17
18impl WarningDiagnostic {
19    /// The source id where the warning originates from.
20    pub fn source_id(&self) -> SourceId {
21        self.source_id
22    }
23
24    /// The kind of the warning.
25    #[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    /// Access context of warning, if any is available.
36    #[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    /// Get the span of the warning.
51    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/// The kind of a [WarningDiagnostic].
78#[derive(Debug)]
79#[allow(missing_docs)]
80#[non_exhaustive]
81pub(crate) enum WarningDiagnosticKind {
82    /// Item identified by the span is not used.
83    NotUsed {
84        /// The span that is not used.
85        span: Span,
86        /// The context in which the value was not used.
87        #[cfg_attr(not(feature = "emit"), allow(dead_code))]
88        context: Option<Span>,
89    },
90    /// Unreachable code.
91    Unreachable {
92        /// The span that is not used.
93        span: Span,
94        /// The span which caused the code to be unreachable.
95        #[cfg_attr(not(feature = "emit"), allow(dead_code))]
96        cause: Span,
97    },
98    /// Warning that an unconditional let pattern will panic if it doesn't
99    /// match.
100    LetPatternMightPanic {
101        /// The span of the pattern.
102        span: Span,
103        /// The context in which it is used.
104        #[cfg_attr(not(feature = "emit"), allow(dead_code))]
105        context: Option<Span>,
106    },
107    /// Encountered a template string without an expansion.
108    TemplateWithoutExpansions {
109        /// Span that caused the error.
110        span: Span,
111        /// The context in which it is used.
112        #[cfg_attr(not(feature = "emit"), allow(dead_code))]
113        context: Option<Span>,
114    },
115    /// Suggestion that call parameters could be removed.
116    RemoveTupleCallParams {
117        /// The span of the call.
118        span: Span,
119        /// The span of the variant being built.
120        #[cfg_attr(not(feature = "emit"), allow(dead_code))]
121        variant: Span,
122        /// The context in which it is used.
123        #[cfg_attr(not(feature = "emit"), allow(dead_code))]
124        context: Option<Span>,
125    },
126    UsedDeprecated {
127        /// The span which is deprecated
128        span: Span,
129        /// The context in which it is used.
130        #[cfg_attr(not(feature = "emit"), allow(dead_code))]
131        context: Option<Span>,
132        /// Deprecated message.
133        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}