]> git.proxmox.com Git - rustc.git/blame - src/librustc/lint.rs
New upstream version 1.42.0+dfsg1
[rustc.git] / src / librustc / lint.rs
CommitLineData
dfeec247
XL
1use std::cmp;
2
3use crate::ich::StableHashingContext;
4use rustc_data_structures::fx::FxHashMap;
5use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
6use rustc_errors::{pluralize, Applicability, DiagnosticBuilder, DiagnosticId};
7use rustc_hir::HirId;
8pub use rustc_session::lint::{builtin, Level, Lint, LintId, LintPass};
9use rustc_session::{DiagnosticMessageId, Session};
10use rustc_span::hygiene::MacroKind;
11use rustc_span::source_map::{DesugaringKind, ExpnKind, MultiSpan};
12use rustc_span::{Span, Symbol};
13
14/// How a lint level was set.
15#[derive(Clone, Copy, PartialEq, Eq, HashStable)]
16pub enum LintSource {
17 /// Lint is at the default level as declared
18 /// in rustc or a plugin.
19 Default,
20
21 /// Lint level was set by an attribute.
22 Node(Symbol, Span, Option<Symbol> /* RFC 2383 reason */),
23
24 /// Lint level was set by a command-line flag.
25 CommandLine(Symbol),
26}
27
28pub type LevelSource = (Level, LintSource);
29
30pub struct LintLevelSets {
31 pub list: Vec<LintSet>,
32 pub lint_cap: Level,
33}
34
35pub enum LintSet {
36 CommandLine {
37 // -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
38 // flag.
39 specs: FxHashMap<LintId, LevelSource>,
40 },
41
42 Node {
43 specs: FxHashMap<LintId, LevelSource>,
44 parent: u32,
45 },
46}
47
48impl LintLevelSets {
49 pub fn new() -> Self {
50 LintLevelSets { list: Vec::new(), lint_cap: Level::Forbid }
51 }
52
53 pub fn get_lint_level(
54 &self,
55 lint: &'static Lint,
56 idx: u32,
57 aux: Option<&FxHashMap<LintId, LevelSource>>,
58 sess: &Session,
59 ) -> LevelSource {
60 let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
61
62 // If `level` is none then we actually assume the default level for this
63 // lint.
64 let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));
65
66 // If we're about to issue a warning, check at the last minute for any
67 // directives against the warnings "lint". If, for example, there's an
68 // `allow(warnings)` in scope then we want to respect that instead.
69 if level == Level::Warn {
70 let (warnings_level, warnings_src) =
71 self.get_lint_id_level(LintId::of(builtin::WARNINGS), idx, aux);
72 if let Some(configured_warning_level) = warnings_level {
73 if configured_warning_level != Level::Warn {
74 level = configured_warning_level;
75 src = warnings_src;
76 }
77 }
78 }
79
80 // Ensure that we never exceed the `--cap-lints` argument.
81 level = cmp::min(level, self.lint_cap);
82
83 if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
84 // Ensure that we never exceed driver level.
85 level = cmp::min(*driver_level, level);
86 }
87
88 return (level, src);
89 }
90
91 pub fn get_lint_id_level(
92 &self,
93 id: LintId,
94 mut idx: u32,
95 aux: Option<&FxHashMap<LintId, LevelSource>>,
96 ) -> (Option<Level>, LintSource) {
97 if let Some(specs) = aux {
98 if let Some(&(level, src)) = specs.get(&id) {
99 return (Some(level), src);
100 }
101 }
102 loop {
103 match self.list[idx as usize] {
104 LintSet::CommandLine { ref specs } => {
105 if let Some(&(level, src)) = specs.get(&id) {
106 return (Some(level), src);
107 }
108 return (None, LintSource::Default);
109 }
110 LintSet::Node { ref specs, parent } => {
111 if let Some(&(level, src)) = specs.get(&id) {
112 return (Some(level), src);
113 }
114 idx = parent;
115 }
116 }
117 }
118 }
119}
120
121pub struct LintLevelMap {
122 pub sets: LintLevelSets,
123 pub id_to_set: FxHashMap<HirId, u32>,
124}
125
126impl LintLevelMap {
127 /// If the `id` was previously registered with `register_id` when building
128 /// this `LintLevelMap` this returns the corresponding lint level and source
129 /// of the lint level for the lint provided.
130 ///
131 /// If the `id` was not previously registered, returns `None`. If `None` is
132 /// returned then the parent of `id` should be acquired and this function
133 /// should be called again.
134 pub fn level_and_source(
135 &self,
136 lint: &'static Lint,
137 id: HirId,
138 session: &Session,
139 ) -> Option<LevelSource> {
140 self.id_to_set.get(&id).map(|idx| self.sets.get_lint_level(lint, *idx, None, session))
141 }
142}
143
144impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
145 #[inline]
146 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
147 let LintLevelMap { ref sets, ref id_to_set } = *self;
148
149 id_to_set.hash_stable(hcx, hasher);
150
151 let LintLevelSets { ref list, lint_cap } = *sets;
152
153 lint_cap.hash_stable(hcx, hasher);
154
155 hcx.while_hashing_spans(true, |hcx| {
156 list.len().hash_stable(hcx, hasher);
157
158 // We are working under the assumption here that the list of
159 // lint-sets is built in a deterministic order.
160 for lint_set in list {
161 ::std::mem::discriminant(lint_set).hash_stable(hcx, hasher);
162
163 match *lint_set {
164 LintSet::CommandLine { ref specs } => {
165 specs.hash_stable(hcx, hasher);
166 }
167 LintSet::Node { ref specs, parent } => {
168 specs.hash_stable(hcx, hasher);
169 parent.hash_stable(hcx, hasher);
170 }
171 }
172 }
173 })
174 }
175}
176
177pub fn struct_lint_level<'a>(
178 sess: &'a Session,
179 lint: &'static Lint,
180 level: Level,
181 src: LintSource,
182 span: Option<MultiSpan>,
183 msg: &str,
184) -> DiagnosticBuilder<'a> {
185 let mut err = match (level, span) {
186 (Level::Allow, _) => return sess.diagnostic().struct_dummy(),
187 (Level::Warn, Some(span)) => sess.struct_span_warn(span, msg),
188 (Level::Warn, None) => sess.struct_warn(msg),
189 (Level::Deny, Some(span)) | (Level::Forbid, Some(span)) => sess.struct_span_err(span, msg),
190 (Level::Deny, None) | (Level::Forbid, None) => sess.struct_err(msg),
191 };
192
193 // Check for future incompatibility lints and issue a stronger warning.
194 let lint_id = LintId::of(lint);
195 let future_incompatible = lint.future_incompatible;
196
197 // If this code originates in a foreign macro, aka something that this crate
198 // did not itself author, then it's likely that there's nothing this crate
199 // can do about it. We probably want to skip the lint entirely.
200 if err.span.primary_spans().iter().any(|s| in_external_macro(sess, *s)) {
201 // Any suggestions made here are likely to be incorrect, so anything we
202 // emit shouldn't be automatically fixed by rustfix.
203 err.allow_suggestions(false);
204
205 // If this is a future incompatible lint it'll become a hard error, so
206 // we have to emit *something*. Also allow lints to whitelist themselves
207 // on a case-by-case basis for emission in a foreign macro.
208 if future_incompatible.is_none() && !lint.report_in_external_macro {
209 err.cancel();
210 // Don't continue further, since we don't want to have
211 // `diag_span_note_once` called for a diagnostic that isn't emitted.
212 return err;
213 }
214 }
215
216 let name = lint.name_lower();
217 match src {
218 LintSource::Default => {
219 sess.diag_note_once(
220 &mut err,
221 DiagnosticMessageId::from(lint),
222 &format!("`#[{}({})]` on by default", level.as_str(), name),
223 );
224 }
225 LintSource::CommandLine(lint_flag_val) => {
226 let flag = match level {
227 Level::Warn => "-W",
228 Level::Deny => "-D",
229 Level::Forbid => "-F",
230 Level::Allow => panic!(),
231 };
232 let hyphen_case_lint_name = name.replace("_", "-");
233 if lint_flag_val.as_str() == name {
234 sess.diag_note_once(
235 &mut err,
236 DiagnosticMessageId::from(lint),
237 &format!(
238 "requested on the command line with `{} {}`",
239 flag, hyphen_case_lint_name
240 ),
241 );
242 } else {
243 let hyphen_case_flag_val = lint_flag_val.as_str().replace("_", "-");
244 sess.diag_note_once(
245 &mut err,
246 DiagnosticMessageId::from(lint),
247 &format!(
248 "`{} {}` implied by `{} {}`",
249 flag, hyphen_case_lint_name, flag, hyphen_case_flag_val
250 ),
251 );
252 }
253 }
254 LintSource::Node(lint_attr_name, src, reason) => {
255 if let Some(rationale) = reason {
256 err.note(&rationale.as_str());
257 }
258 sess.diag_span_note_once(
259 &mut err,
260 DiagnosticMessageId::from(lint),
261 src,
262 "lint level defined here",
263 );
264 if lint_attr_name.as_str() != name {
265 let level_str = level.as_str();
266 sess.diag_note_once(
267 &mut err,
268 DiagnosticMessageId::from(lint),
269 &format!(
270 "`#[{}({})]` implied by `#[{}({})]`",
271 level_str, name, level_str, lint_attr_name
272 ),
273 );
274 }
275 }
276 }
277
278 err.code(DiagnosticId::Lint(name));
279
280 if let Some(future_incompatible) = future_incompatible {
281 const STANDARD_MESSAGE: &str = "this was previously accepted by the compiler but is being phased out; \
282 it will become a hard error";
283
284 let explanation = if lint_id == LintId::of(builtin::UNSTABLE_NAME_COLLISIONS) {
285 "once this method is added to the standard library, \
286 the ambiguity may cause an error or change in behavior!"
287 .to_owned()
288 } else if lint_id == LintId::of(builtin::MUTABLE_BORROW_RESERVATION_CONFLICT) {
289 "this borrowing pattern was not meant to be accepted, \
290 and may become a hard error in the future"
291 .to_owned()
292 } else if let Some(edition) = future_incompatible.edition {
293 format!("{} in the {} edition!", STANDARD_MESSAGE, edition)
294 } else {
295 format!("{} in a future release!", STANDARD_MESSAGE)
296 };
297 let citation = format!("for more information, see {}", future_incompatible.reference);
298 err.warn(&explanation);
299 err.note(&citation);
300 }
301
302 return err;
303}
304
305/// Returns whether `span` originates in a foreign crate's external macro.
306///
307/// This is used to test whether a lint should not even begin to figure out whether it should
308/// be reported on the current node.
309pub fn in_external_macro(sess: &Session, span: Span) -> bool {
310 let expn_data = span.ctxt().outer_expn_data();
311 match expn_data.kind {
312 ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop) => false,
313 ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
314 ExpnKind::Macro(MacroKind::Bang, _) => {
315 if expn_data.def_site.is_dummy() {
316 // Dummy span for the `def_site` means it's an external macro.
317 return true;
318 }
319 match sess.source_map().span_to_snippet(expn_data.def_site) {
320 Ok(code) => !code.starts_with("macro_rules"),
321 // No snippet means external macro or compiler-builtin expansion.
322 Err(_) => true,
323 }
324 }
325 ExpnKind::Macro(..) => true, // definitely a plugin
326 }
327}
328
329pub fn add_elided_lifetime_in_path_suggestion(
330 sess: &Session,
331 db: &mut DiagnosticBuilder<'_>,
332 n: usize,
333 path_span: Span,
334 incl_angl_brckt: bool,
335 insertion_span: Span,
336 anon_lts: String,
337) {
338 let (replace_span, suggestion) = if incl_angl_brckt {
339 (insertion_span, anon_lts)
340 } else {
341 // When possible, prefer a suggestion that replaces the whole
342 // `Path<T>` expression with `Path<'_, T>`, rather than inserting `'_, `
343 // at a point (which makes for an ugly/confusing label)
344 if let Ok(snippet) = sess.source_map().span_to_snippet(path_span) {
345 // But our spans can get out of whack due to macros; if the place we think
346 // we want to insert `'_` isn't even within the path expression's span, we
347 // should bail out of making any suggestion rather than panicking on a
348 // subtract-with-overflow or string-slice-out-out-bounds (!)
349 // FIXME: can we do better?
350 if insertion_span.lo().0 < path_span.lo().0 {
351 return;
352 }
353 let insertion_index = (insertion_span.lo().0 - path_span.lo().0) as usize;
354 if insertion_index > snippet.len() {
355 return;
356 }
357 let (before, after) = snippet.split_at(insertion_index);
358 (path_span, format!("{}{}{}", before, anon_lts, after))
359 } else {
360 (insertion_span, anon_lts)
361 }
362 };
363 db.span_suggestion(
364 replace_span,
365 &format!("indicate the anonymous lifetime{}", pluralize!(n)),
366 suggestion,
367 Applicability::MachineApplicable,
368 );
369}