1use 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 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 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 pub fn fancy_format_stderr(&self, source: &SourceCode) {
48 Emitter::stderr(ColorConfig::Auto, Some(&*source.codemap()))
49 .emit(&[self.diagnostic(source)]);
50 }
51
52 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 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 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 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}