snix_eval/
warnings.rs

1//! Implements warnings that are emitted in cases where code passed to
2//! Snix exhibits problems that the user could address.
3
4use codemap_diagnostic::{ColorConfig, Diagnostic, Emitter, Level, SpanLabel, SpanStyle};
5
6use crate::SourceCode;
7
8#[derive(Debug)]
9pub enum WarningKind {
10    DeprecatedLiteralURL,
11    UselessInherit,
12    UnusedBinding,
13    ShadowedGlobal(&'static str),
14    DeprecatedLegacyLet,
15    InvalidNixPath(String),
16    UselessBoolOperation(&'static str),
17    DeadCode,
18    EmptyInherit,
19    EmptyLet,
20    ShadowedOutput(String),
21    SRIHashWrongPadding,
22
23    /// Snix internal warning for features triggered by users that are
24    /// not actually implemented yet, but do not cause runtime failures.
25    NotImplemented(&'static str),
26}
27
28#[derive(Debug)]
29pub struct EvalWarning {
30    pub kind: WarningKind,
31    pub span: codemap::Span,
32}
33
34impl EvalWarning {
35    /// Render a fancy, human-readable output of this warning and
36    /// return it as a String. Note that this version of the output
37    /// does not include any colours or font styles.
38    pub fn fancy_format_str(&self, source: &SourceCode) -> String {
39        let mut out = vec![];
40        Emitter::vec(&mut out, Some(&*source.codemap())).emit(&[self.diagnostic(source)]);
41        String::from_utf8_lossy(&out).to_string()
42    }
43
44    /// Render a fancy, human-readable output of this warning and
45    /// print it to stderr. If rendered in a terminal that supports
46    /// colours and font styles, the output will include those.
47    pub fn fancy_format_stderr(&self, source: &SourceCode) {
48        Emitter::stderr(ColorConfig::Auto, Some(&*source.codemap()))
49            .emit(&[self.diagnostic(source)]);
50    }
51
52    /// Render a fancy, human-readable output of this warning and
53    /// print it to a std::io::Write. If rendered in a terminal that supports
54    /// colours and font styles, the output will include those.
55    pub fn fancy_format_write<E: std::io::Write + std::marker::Send>(
56        &self,
57        stderr: &mut E,
58        source: &SourceCode,
59    ) {
60        Emitter::new(Box::new(stderr), Some(&*source.codemap())).emit(&[self.diagnostic(source)]);
61    }
62
63    /// Create the optional span label displayed as an annotation on
64    /// the underlined span of the warning.
65    fn span_label(&self) -> Option<String> {
66        match self.kind {
67            WarningKind::UnusedBinding | WarningKind::ShadowedGlobal(_) => {
68                Some("variable declared here".into())
69            }
70            _ => None,
71        }
72    }
73
74    /// Create the primary warning message displayed to users for a
75    /// warning.
76    fn message(&self, source: &SourceCode) -> String {
77        match self.kind {
78            WarningKind::DeprecatedLiteralURL => {
79                "URL literal syntax is deprecated, use a quoted string instead".to_string()
80            }
81
82            WarningKind::UselessInherit => {
83                "inherit does nothing (this variable already exists with the same value)"
84                    .to_string()
85            }
86
87            WarningKind::UnusedBinding => {
88                format!(
89                    "variable '{}' is declared, but never used:",
90                    source.source_slice(self.span)
91                )
92            }
93
94            WarningKind::ShadowedGlobal(name) => {
95                format!("declared variable '{}' shadows a built-in global!", name)
96            }
97
98            WarningKind::DeprecatedLegacyLet => {
99                "legacy `let` syntax used, please rewrite this as `let .. in ...`".to_string()
100            }
101
102            WarningKind::InvalidNixPath(ref err) => {
103                format!("invalid NIX_PATH resulted in a parse error: {}", err)
104            }
105
106            WarningKind::UselessBoolOperation(msg) => {
107                format!("useless operation on boolean: {}", msg)
108            }
109
110            WarningKind::DeadCode => "this code will never be executed".to_string(),
111
112            WarningKind::EmptyInherit => "this `inherit` statement is empty".to_string(),
113
114            WarningKind::EmptyLet => "this `let`-expression contains no bindings".to_string(),
115
116            WarningKind::ShadowedOutput(ref out) => format!(
117                "this derivation's environment shadows the output name {}",
118                out
119            ),
120            WarningKind::SRIHashWrongPadding => "SRI hash has wrong padding".to_string(),
121
122            WarningKind::NotImplemented(what) => {
123                format!("feature not yet implemented in snix: {}", what)
124            }
125        }
126    }
127
128    /// Return the unique warning code for this variant which can be
129    /// used to refer users to documentation.
130    fn code(&self) -> &'static str {
131        match self.kind {
132            WarningKind::DeprecatedLiteralURL => "W001",
133            WarningKind::UselessInherit => "W002",
134            WarningKind::UnusedBinding => "W003",
135            WarningKind::ShadowedGlobal(_) => "W004",
136            WarningKind::DeprecatedLegacyLet => "W005",
137            WarningKind::InvalidNixPath(_) => "W006",
138            WarningKind::UselessBoolOperation(_) => "W007",
139            WarningKind::DeadCode => "W008",
140            WarningKind::EmptyInherit => "W009",
141            WarningKind::EmptyLet => "W010",
142            WarningKind::ShadowedOutput(_) => "W011",
143            WarningKind::SRIHashWrongPadding => "W012",
144
145            WarningKind::NotImplemented(_) => "W999",
146        }
147    }
148
149    fn diagnostic(&self, source: &SourceCode) -> Diagnostic {
150        let span_label = SpanLabel {
151            label: self.span_label(),
152            span: self.span,
153            style: SpanStyle::Primary,
154        };
155
156        Diagnostic {
157            level: Level::Warning,
158            message: self.message(source),
159            spans: vec![span_label],
160            code: Some(self.code().into()),
161        }
162    }
163}