]>
Commit | Line | Data |
---|---|---|
c34b1796 | 1 | // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
1a4d82fc JJ |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | //! Implementation of lint checking. | |
12 | //! | |
13 | //! The lint checking is mostly consolidated into one pass which runs just | |
14 | //! before translation to LLVM bytecode. Throughout compilation, lint warnings | |
15 | //! can be added via the `add_lint` method on the Session structure. This | |
16 | //! requires a span and an id of the node that the lint is being added to. The | |
17 | //! lint isn't actually emitted at that time because it is unknown what the | |
18 | //! actual lint level at that location is. | |
19 | //! | |
20 | //! To actually emit lint warnings/errors, a separate pass is used just before | |
21 | //! translation. A context keeps track of the current state of all lint levels. | |
22 | //! Upon entering a node of the ast which can modify the lint settings, the | |
23 | //! previous lint state is pushed onto a stack and the ast is then recursed | |
24 | //! upon. As the ast is traversed, this keeps track of the current lint level | |
25 | //! for all lint attributes. | |
26 | use self::TargetLint::*; | |
27 | ||
28 | use middle::privacy::ExportedItems; | |
29 | use middle::ty::{self, Ty}; | |
30 | use session::{early_error, Session}; | |
1a4d82fc | 31 | use lint::{Level, LevelSource, Lint, LintId, LintArray, LintPass, LintPassObject}; |
62682a34 | 32 | use lint::{Default, CommandLine, Node, Allow, Warn, Deny, Forbid}; |
1a4d82fc JJ |
33 | use lint::builtin; |
34 | use util::nodemap::FnvHashMap; | |
35 | ||
36 | use std::cell::RefCell; | |
c1a9b12d | 37 | use std::cmp; |
1a4d82fc JJ |
38 | use std::mem; |
39 | use syntax::ast_util::IdVisitingOperation; | |
40 | use syntax::attr::AttrMetaMethods; | |
41 | use syntax::attr; | |
42 | use syntax::codemap::Span; | |
43 | use syntax::visit::{Visitor, FnKind}; | |
44 | use syntax::parse::token::InternedString; | |
45 | use syntax::{ast, ast_util, visit}; | |
46 | ||
47 | /// Information about the registered lints. | |
48 | /// | |
49 | /// This is basically the subset of `Context` that we can | |
50 | /// build early in the compile pipeline. | |
51 | pub struct LintStore { | |
52 | /// Registered lints. The bool is true if the lint was | |
53 | /// added by a plugin. | |
54 | lints: Vec<(&'static Lint, bool)>, | |
55 | ||
56 | /// Trait objects for each lint pass. | |
57 | /// This is only `None` while iterating over the objects. See the definition | |
58 | /// of run_lints. | |
59 | passes: Option<Vec<LintPassObject>>, | |
60 | ||
61 | /// Lints indexed by name. | |
62 | by_name: FnvHashMap<String, TargetLint>, | |
63 | ||
64 | /// Current levels of each lint, and where they were set. | |
65 | levels: FnvHashMap<LintId, LevelSource>, | |
66 | ||
67 | /// Map of registered lint groups to what lints they expand to. The bool | |
68 | /// is true if the lint group was added by a plugin. | |
69 | lint_groups: FnvHashMap<&'static str, (Vec<LintId>, bool)>, | |
c1a9b12d SL |
70 | |
71 | /// Maximum level a lint can be | |
72 | lint_cap: Option<Level>, | |
1a4d82fc JJ |
73 | } |
74 | ||
75 | /// The targed of the `by_name` map, which accounts for renaming/deprecation. | |
76 | enum TargetLint { | |
77 | /// A direct lint target | |
78 | Id(LintId), | |
79 | ||
80 | /// Temporary renaming, used for easing migration pain; see #16545 | |
81 | Renamed(String, LintId), | |
c1a9b12d SL |
82 | |
83 | /// Lint with this name existed previously, but has been removed/deprecated. | |
84 | /// The string argument is the reason for removal. | |
85 | Removed(String), | |
86 | } | |
87 | ||
88 | enum FindLintError { | |
89 | NotFound, | |
90 | Removed | |
1a4d82fc JJ |
91 | } |
92 | ||
93 | impl LintStore { | |
94 | fn get_level_source(&self, lint: LintId) -> LevelSource { | |
95 | match self.levels.get(&lint) { | |
96 | Some(&s) => s, | |
97 | None => (Allow, Default), | |
98 | } | |
99 | } | |
100 | ||
c1a9b12d SL |
101 | fn set_level(&mut self, lint: LintId, mut lvlsrc: LevelSource) { |
102 | if let Some(cap) = self.lint_cap { | |
103 | lvlsrc.0 = cmp::min(lvlsrc.0, cap); | |
104 | } | |
1a4d82fc JJ |
105 | if lvlsrc.0 == Allow { |
106 | self.levels.remove(&lint); | |
107 | } else { | |
108 | self.levels.insert(lint, lvlsrc); | |
109 | } | |
110 | } | |
111 | ||
112 | pub fn new() -> LintStore { | |
113 | LintStore { | |
114 | lints: vec!(), | |
115 | passes: Some(vec!()), | |
85aaf69f SL |
116 | by_name: FnvHashMap(), |
117 | levels: FnvHashMap(), | |
118 | lint_groups: FnvHashMap(), | |
c1a9b12d | 119 | lint_cap: None, |
1a4d82fc JJ |
120 | } |
121 | } | |
122 | ||
123 | pub fn get_lints<'t>(&'t self) -> &'t [(&'static Lint, bool)] { | |
c34b1796 | 124 | &self.lints |
1a4d82fc JJ |
125 | } |
126 | ||
127 | pub fn get_lint_groups<'t>(&'t self) -> Vec<(&'static str, Vec<LintId>, bool)> { | |
128 | self.lint_groups.iter().map(|(k, v)| (*k, | |
129 | v.0.clone(), | |
130 | v.1)).collect() | |
131 | } | |
132 | ||
133 | pub fn register_pass(&mut self, sess: Option<&Session>, | |
134 | from_plugin: bool, pass: LintPassObject) { | |
85aaf69f | 135 | for &lint in pass.get_lints() { |
1a4d82fc JJ |
136 | self.lints.push((*lint, from_plugin)); |
137 | ||
138 | let id = LintId::of(*lint); | |
139 | if self.by_name.insert(lint.name_lower(), Id(id)).is_some() { | |
140 | let msg = format!("duplicate specification of lint {}", lint.name_lower()); | |
141 | match (sess, from_plugin) { | |
142 | // We load builtin lints first, so a duplicate is a compiler bug. | |
143 | // Use early_error when handling -W help with no crate. | |
85aaf69f SL |
144 | (None, _) => early_error(&msg[..]), |
145 | (Some(sess), false) => sess.bug(&msg[..]), | |
1a4d82fc JJ |
146 | |
147 | // A duplicate name from a plugin is a user error. | |
85aaf69f | 148 | (Some(sess), true) => sess.err(&msg[..]), |
1a4d82fc JJ |
149 | } |
150 | } | |
151 | ||
152 | if lint.default_level != Allow { | |
153 | self.levels.insert(id, (lint.default_level, Default)); | |
154 | } | |
155 | } | |
156 | self.passes.as_mut().unwrap().push(pass); | |
157 | } | |
158 | ||
159 | pub fn register_group(&mut self, sess: Option<&Session>, | |
160 | from_plugin: bool, name: &'static str, | |
161 | to: Vec<LintId>) { | |
162 | let new = self.lint_groups.insert(name, (to, from_plugin)).is_none(); | |
163 | ||
164 | if !new { | |
165 | let msg = format!("duplicate specification of lint group {}", name); | |
166 | match (sess, from_plugin) { | |
167 | // We load builtin lints first, so a duplicate is a compiler bug. | |
168 | // Use early_error when handling -W help with no crate. | |
85aaf69f SL |
169 | (None, _) => early_error(&msg[..]), |
170 | (Some(sess), false) => sess.bug(&msg[..]), | |
1a4d82fc JJ |
171 | |
172 | // A duplicate name from a plugin is a user error. | |
85aaf69f | 173 | (Some(sess), true) => sess.err(&msg[..]), |
1a4d82fc JJ |
174 | } |
175 | } | |
176 | } | |
177 | ||
c34b1796 | 178 | pub fn register_renamed(&mut self, old_name: &str, new_name: &str) { |
1a4d82fc JJ |
179 | let target = match self.by_name.get(new_name) { |
180 | Some(&Id(lint_id)) => lint_id.clone(), | |
181 | _ => panic!("invalid lint renaming of {} to {}", old_name, new_name) | |
182 | }; | |
183 | self.by_name.insert(old_name.to_string(), Renamed(new_name.to_string(), target)); | |
184 | } | |
185 | ||
c1a9b12d SL |
186 | pub fn register_removed(&mut self, name: &str, reason: &str) { |
187 | self.by_name.insert(name.into(), Removed(reason.into())); | |
188 | } | |
189 | ||
1a4d82fc JJ |
190 | #[allow(unused_variables)] |
191 | fn find_lint(&self, lint_name: &str, sess: &Session, span: Option<Span>) | |
c1a9b12d | 192 | -> Result<LintId, FindLintError> |
1a4d82fc JJ |
193 | { |
194 | match self.by_name.get(lint_name) { | |
c1a9b12d | 195 | Some(&Id(lint_id)) => Ok(lint_id), |
1a4d82fc JJ |
196 | Some(&Renamed(ref new_name, lint_id)) => { |
197 | let warning = format!("lint {} has been renamed to {}", | |
198 | lint_name, new_name); | |
199 | match span { | |
85aaf69f SL |
200 | Some(span) => sess.span_warn(span, &warning[..]), |
201 | None => sess.warn(&warning[..]), | |
1a4d82fc | 202 | }; |
c1a9b12d SL |
203 | Ok(lint_id) |
204 | }, | |
205 | Some(&Removed(ref reason)) => { | |
206 | let warning = format!("lint {} has been removed: {}", lint_name, reason); | |
207 | match span { | |
208 | Some(span) => sess.span_warn(span, &warning[..]), | |
209 | None => sess.warn(&warning[..]) | |
210 | } | |
211 | Err(FindLintError::Removed) | |
212 | }, | |
213 | None => Err(FindLintError::NotFound) | |
1a4d82fc JJ |
214 | } |
215 | } | |
216 | ||
217 | pub fn process_command_line(&mut self, sess: &Session) { | |
85aaf69f SL |
218 | for &(ref lint_name, level) in &sess.opts.lint_opts { |
219 | match self.find_lint(&lint_name[..], sess, None) { | |
c1a9b12d SL |
220 | Ok(lint_id) => self.set_level(lint_id, (level, CommandLine)), |
221 | Err(_) => { | |
1a4d82fc JJ |
222 | match self.lint_groups.iter().map(|(&x, pair)| (x, pair.0.clone())) |
223 | .collect::<FnvHashMap<&'static str, | |
224 | Vec<LintId>>>() | |
85aaf69f | 225 | .get(&lint_name[..]) { |
1a4d82fc JJ |
226 | Some(v) => { |
227 | v.iter() | |
228 | .map(|lint_id: &LintId| | |
229 | self.set_level(*lint_id, (level, CommandLine))) | |
230 | .collect::<Vec<()>>(); | |
231 | } | |
232 | None => sess.err(&format!("unknown {} flag: {}", | |
c34b1796 | 233 | level.as_str(), lint_name)), |
1a4d82fc JJ |
234 | } |
235 | } | |
236 | } | |
237 | } | |
c1a9b12d SL |
238 | |
239 | self.lint_cap = sess.opts.lint_cap; | |
240 | if let Some(cap) = self.lint_cap { | |
241 | for level in self.levels.iter_mut().map(|p| &mut (p.1).0) { | |
242 | *level = cmp::min(*level, cap); | |
243 | } | |
244 | } | |
1a4d82fc | 245 | } |
1a4d82fc JJ |
246 | } |
247 | ||
248 | /// Context for lint checking. | |
249 | pub struct Context<'a, 'tcx: 'a> { | |
250 | /// Type context we're checking in. | |
251 | pub tcx: &'a ty::ctxt<'tcx>, | |
252 | ||
253 | /// The crate being checked. | |
254 | pub krate: &'a ast::Crate, | |
255 | ||
256 | /// Items exported from the crate being checked. | |
257 | pub exported_items: &'a ExportedItems, | |
258 | ||
259 | /// The store of registered lints. | |
260 | lints: LintStore, | |
261 | ||
262 | /// When recursing into an attributed node of the ast which modifies lint | |
263 | /// levels, this stack keeps track of the previous lint levels of whatever | |
264 | /// was modified. | |
265 | level_stack: Vec<(LintId, LevelSource)>, | |
266 | ||
267 | /// Level of lints for certain NodeIds, stored here because the body of | |
268 | /// the lint needs to run in trans. | |
269 | node_levels: RefCell<FnvHashMap<(ast::NodeId, LintId), LevelSource>>, | |
270 | } | |
271 | ||
272 | /// Convenience macro for calling a `LintPass` method on every pass in the context. | |
273 | macro_rules! run_lints { ($cx:expr, $f:ident, $($args:expr),*) => ({ | |
274 | // Move the vector of passes out of `$cx` so that we can | |
275 | // iterate over it mutably while passing `$cx` to the methods. | |
276 | let mut passes = $cx.lints.passes.take().unwrap(); | |
85aaf69f | 277 | for obj in &mut passes { |
1a4d82fc JJ |
278 | obj.$f($cx, $($args),*); |
279 | } | |
280 | $cx.lints.passes = Some(passes); | |
281 | }) } | |
282 | ||
283 | /// Parse the lint attributes into a vector, with `Err`s for malformed lint | |
284 | /// attributes. Writing this as an iterator is an enormous mess. | |
285 | pub fn gather_attrs(attrs: &[ast::Attribute]) | |
286 | -> Vec<Result<(InternedString, Level, Span), Span>> { | |
287 | let mut out = vec!(); | |
85aaf69f SL |
288 | for attr in attrs { |
289 | let level = match Level::from_str(&attr.name()) { | |
1a4d82fc JJ |
290 | None => continue, |
291 | Some(lvl) => lvl, | |
292 | }; | |
293 | ||
294 | attr::mark_used(attr); | |
295 | ||
296 | let meta = &attr.node.value; | |
297 | let metas = match meta.node { | |
298 | ast::MetaList(_, ref metas) => metas, | |
299 | _ => { | |
300 | out.push(Err(meta.span)); | |
301 | continue; | |
302 | } | |
303 | }; | |
304 | ||
85aaf69f | 305 | for meta in metas { |
1a4d82fc JJ |
306 | out.push(match meta.node { |
307 | ast::MetaWord(ref lint_name) => Ok((lint_name.clone(), level, meta.span)), | |
308 | _ => Err(meta.span), | |
309 | }); | |
310 | } | |
311 | } | |
312 | out | |
313 | } | |
314 | ||
315 | /// Emit a lint as a warning or an error (or not at all) | |
316 | /// according to `level`. | |
317 | /// | |
318 | /// This lives outside of `Context` so it can be used by checks | |
319 | /// in trans that run after the main lint pass is finished. Most | |
320 | /// lints elsewhere in the compiler should call | |
321 | /// `Session::add_lint()` instead. | |
322 | pub fn raw_emit_lint(sess: &Session, lint: &'static Lint, | |
323 | lvlsrc: LevelSource, span: Option<Span>, msg: &str) { | |
324 | let (mut level, source) = lvlsrc; | |
325 | if level == Allow { return } | |
326 | ||
327 | let name = lint.name_lower(); | |
328 | let mut def = None; | |
1a4d82fc JJ |
329 | let msg = match source { |
330 | Default => { | |
331 | format!("{}, #[{}({})] on by default", msg, | |
332 | level.as_str(), name) | |
333 | }, | |
334 | CommandLine => { | |
335 | format!("{} [-{} {}]", msg, | |
336 | match level { | |
337 | Warn => 'W', Deny => 'D', Forbid => 'F', | |
338 | Allow => panic!() | |
339 | }, name.replace("_", "-")) | |
340 | }, | |
341 | Node(src) => { | |
342 | def = Some(src); | |
343 | msg.to_string() | |
344 | } | |
1a4d82fc JJ |
345 | }; |
346 | ||
347 | // For purposes of printing, we can treat forbid as deny. | |
348 | if level == Forbid { level = Deny; } | |
349 | ||
350 | match (level, span) { | |
85aaf69f SL |
351 | (Warn, Some(sp)) => sess.span_warn(sp, &msg[..]), |
352 | (Warn, None) => sess.warn(&msg[..]), | |
353 | (Deny, Some(sp)) => sess.span_err(sp, &msg[..]), | |
354 | (Deny, None) => sess.err(&msg[..]), | |
1a4d82fc JJ |
355 | _ => sess.bug("impossible level in raw_emit_lint"), |
356 | } | |
357 | ||
85aaf69f | 358 | if let Some(span) = def { |
1a4d82fc JJ |
359 | sess.span_note(span, "lint level defined here"); |
360 | } | |
361 | } | |
362 | ||
363 | impl<'a, 'tcx> Context<'a, 'tcx> { | |
364 | fn new(tcx: &'a ty::ctxt<'tcx>, | |
365 | krate: &'a ast::Crate, | |
366 | exported_items: &'a ExportedItems) -> Context<'a, 'tcx> { | |
367 | // We want to own the lint store, so move it out of the session. | |
368 | let lint_store = mem::replace(&mut *tcx.sess.lint_store.borrow_mut(), | |
369 | LintStore::new()); | |
370 | ||
371 | Context { | |
372 | tcx: tcx, | |
373 | krate: krate, | |
374 | exported_items: exported_items, | |
375 | lints: lint_store, | |
376 | level_stack: vec![], | |
85aaf69f | 377 | node_levels: RefCell::new(FnvHashMap()), |
1a4d82fc JJ |
378 | } |
379 | } | |
380 | ||
381 | /// Get the overall compiler `Session` object. | |
382 | pub fn sess(&'a self) -> &'a Session { | |
383 | &self.tcx.sess | |
384 | } | |
385 | ||
386 | /// Get the level of `lint` at the current position of the lint | |
387 | /// traversal. | |
388 | pub fn current_level(&self, lint: &'static Lint) -> Level { | |
389 | self.lints.levels.get(&LintId::of(lint)).map_or(Allow, |&(lvl, _)| lvl) | |
390 | } | |
391 | ||
392 | fn lookup_and_emit(&self, lint: &'static Lint, span: Option<Span>, msg: &str) { | |
393 | let (level, src) = match self.lints.levels.get(&LintId::of(lint)) { | |
394 | None => return, | |
395 | Some(&(Warn, src)) => { | |
396 | let lint_id = LintId::of(builtin::WARNINGS); | |
397 | (self.lints.get_level_source(lint_id).0, src) | |
398 | } | |
399 | Some(&pair) => pair, | |
400 | }; | |
401 | ||
402 | raw_emit_lint(&self.tcx.sess, lint, (level, src), span, msg); | |
403 | } | |
404 | ||
405 | /// Emit a lint at the appropriate level, with no associated span. | |
406 | pub fn lint(&self, lint: &'static Lint, msg: &str) { | |
407 | self.lookup_and_emit(lint, None, msg); | |
408 | } | |
409 | ||
410 | /// Emit a lint at the appropriate level, for a particular span. | |
411 | pub fn span_lint(&self, lint: &'static Lint, span: Span, msg: &str) { | |
412 | self.lookup_and_emit(lint, Some(span), msg); | |
413 | } | |
414 | ||
415 | /// Merge the lints specified by any lint attributes into the | |
416 | /// current lint context, call the provided function, then reset the | |
417 | /// lints in effect to their previous state. | |
418 | fn with_lint_attrs<F>(&mut self, | |
419 | attrs: &[ast::Attribute], | |
420 | f: F) where | |
421 | F: FnOnce(&mut Context), | |
422 | { | |
423 | // Parse all of the lint attributes, and then add them all to the | |
424 | // current dictionary of lint information. Along the way, keep a history | |
425 | // of what we changed so we can roll everything back after invoking the | |
426 | // specified closure | |
85aaf69f | 427 | let mut pushed = 0; |
1a4d82fc | 428 | |
85aaf69f | 429 | for result in gather_attrs(attrs) { |
1a4d82fc JJ |
430 | let v = match result { |
431 | Err(span) => { | |
432 | self.tcx.sess.span_err(span, "malformed lint attribute"); | |
433 | continue; | |
434 | } | |
435 | Ok((lint_name, level, span)) => { | |
85aaf69f | 436 | match self.lints.find_lint(&lint_name, &self.tcx.sess, Some(span)) { |
c1a9b12d SL |
437 | Ok(lint_id) => vec![(lint_id, level, span)], |
438 | Err(FindLintError::NotFound) => { | |
85aaf69f | 439 | match self.lints.lint_groups.get(&lint_name[..]) { |
1a4d82fc JJ |
440 | Some(&(ref v, _)) => v.iter() |
441 | .map(|lint_id: &LintId| | |
442 | (*lint_id, level, span)) | |
443 | .collect(), | |
444 | None => { | |
445 | self.span_lint(builtin::UNKNOWN_LINTS, span, | |
85aaf69f SL |
446 | &format!("unknown `{}` attribute: `{}`", |
447 | level.as_str(), lint_name)); | |
1a4d82fc JJ |
448 | continue; |
449 | } | |
450 | } | |
c1a9b12d SL |
451 | }, |
452 | Err(FindLintError::Removed) => { continue; } | |
1a4d82fc JJ |
453 | } |
454 | } | |
455 | }; | |
456 | ||
85aaf69f | 457 | for (lint_id, level, span) in v { |
1a4d82fc JJ |
458 | let now = self.lints.get_level_source(lint_id).0; |
459 | if now == Forbid && level != Forbid { | |
460 | let lint_name = lint_id.as_str(); | |
461 | self.tcx.sess.span_err(span, | |
462 | &format!("{}({}) overruled by outer forbid({})", | |
463 | level.as_str(), lint_name, | |
c34b1796 | 464 | lint_name)); |
1a4d82fc JJ |
465 | } else if now != level { |
466 | let src = self.lints.get_level_source(lint_id).1; | |
467 | self.level_stack.push((lint_id, (now, src))); | |
468 | pushed += 1; | |
469 | self.lints.set_level(lint_id, (level, Node(span))); | |
470 | } | |
471 | } | |
472 | } | |
473 | ||
474 | run_lints!(self, enter_lint_attrs, attrs); | |
475 | f(self); | |
476 | run_lints!(self, exit_lint_attrs, attrs); | |
477 | ||
478 | // rollback | |
85aaf69f | 479 | for _ in 0..pushed { |
1a4d82fc JJ |
480 | let (lint, lvlsrc) = self.level_stack.pop().unwrap(); |
481 | self.lints.set_level(lint, lvlsrc); | |
482 | } | |
483 | } | |
484 | ||
485 | fn visit_ids<F>(&mut self, f: F) where | |
486 | F: FnOnce(&mut ast_util::IdVisitor<Context>) | |
487 | { | |
488 | let mut v = ast_util::IdVisitor { | |
489 | operation: self, | |
490 | pass_through_items: false, | |
491 | visited_outermost: false, | |
492 | }; | |
493 | f(&mut v); | |
494 | } | |
495 | } | |
496 | ||
497 | impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> { | |
498 | fn visit_item(&mut self, it: &ast::Item) { | |
c34b1796 | 499 | self.with_lint_attrs(&it.attrs, |cx| { |
1a4d82fc JJ |
500 | run_lints!(cx, check_item, it); |
501 | cx.visit_ids(|v| v.visit_item(it)); | |
502 | visit::walk_item(cx, it); | |
503 | }) | |
504 | } | |
505 | ||
506 | fn visit_foreign_item(&mut self, it: &ast::ForeignItem) { | |
c34b1796 | 507 | self.with_lint_attrs(&it.attrs, |cx| { |
1a4d82fc JJ |
508 | run_lints!(cx, check_foreign_item, it); |
509 | visit::walk_foreign_item(cx, it); | |
510 | }) | |
511 | } | |
512 | ||
1a4d82fc JJ |
513 | fn visit_pat(&mut self, p: &ast::Pat) { |
514 | run_lints!(self, check_pat, p); | |
515 | visit::walk_pat(self, p); | |
516 | } | |
517 | ||
518 | fn visit_expr(&mut self, e: &ast::Expr) { | |
519 | run_lints!(self, check_expr, e); | |
520 | visit::walk_expr(self, e); | |
521 | } | |
522 | ||
523 | fn visit_stmt(&mut self, s: &ast::Stmt) { | |
524 | run_lints!(self, check_stmt, s); | |
525 | visit::walk_stmt(self, s); | |
526 | } | |
527 | ||
528 | fn visit_fn(&mut self, fk: FnKind<'v>, decl: &'v ast::FnDecl, | |
529 | body: &'v ast::Block, span: Span, id: ast::NodeId) { | |
c34b1796 AL |
530 | run_lints!(self, check_fn, fk, decl, body, span, id); |
531 | visit::walk_fn(self, fk, decl, body, span); | |
1a4d82fc JJ |
532 | } |
533 | ||
534 | fn visit_struct_def(&mut self, | |
535 | s: &ast::StructDef, | |
536 | ident: ast::Ident, | |
537 | g: &ast::Generics, | |
538 | id: ast::NodeId) { | |
539 | run_lints!(self, check_struct_def, s, ident, g, id); | |
540 | visit::walk_struct_def(self, s); | |
541 | run_lints!(self, check_struct_def_post, s, ident, g, id); | |
542 | } | |
543 | ||
544 | fn visit_struct_field(&mut self, s: &ast::StructField) { | |
c34b1796 | 545 | self.with_lint_attrs(&s.node.attrs, |cx| { |
1a4d82fc JJ |
546 | run_lints!(cx, check_struct_field, s); |
547 | visit::walk_struct_field(cx, s); | |
548 | }) | |
549 | } | |
550 | ||
551 | fn visit_variant(&mut self, v: &ast::Variant, g: &ast::Generics) { | |
c34b1796 | 552 | self.with_lint_attrs(&v.node.attrs, |cx| { |
1a4d82fc JJ |
553 | run_lints!(cx, check_variant, v, g); |
554 | visit::walk_variant(cx, v, g); | |
555 | run_lints!(cx, check_variant_post, v, g); | |
556 | }) | |
557 | } | |
558 | ||
1a4d82fc JJ |
559 | fn visit_ty(&mut self, t: &ast::Ty) { |
560 | run_lints!(self, check_ty, t); | |
c34b1796 | 561 | visit::walk_ty(self, t); |
1a4d82fc JJ |
562 | } |
563 | ||
564 | fn visit_ident(&mut self, sp: Span, id: ast::Ident) { | |
565 | run_lints!(self, check_ident, sp, id); | |
566 | } | |
567 | ||
568 | fn visit_mod(&mut self, m: &ast::Mod, s: Span, n: ast::NodeId) { | |
569 | run_lints!(self, check_mod, m, s, n); | |
570 | visit::walk_mod(self, m); | |
571 | } | |
572 | ||
573 | fn visit_local(&mut self, l: &ast::Local) { | |
574 | run_lints!(self, check_local, l); | |
575 | visit::walk_local(self, l); | |
576 | } | |
577 | ||
578 | fn visit_block(&mut self, b: &ast::Block) { | |
579 | run_lints!(self, check_block, b); | |
580 | visit::walk_block(self, b); | |
581 | } | |
582 | ||
583 | fn visit_arm(&mut self, a: &ast::Arm) { | |
584 | run_lints!(self, check_arm, a); | |
585 | visit::walk_arm(self, a); | |
586 | } | |
587 | ||
588 | fn visit_decl(&mut self, d: &ast::Decl) { | |
589 | run_lints!(self, check_decl, d); | |
590 | visit::walk_decl(self, d); | |
591 | } | |
592 | ||
593 | fn visit_expr_post(&mut self, e: &ast::Expr) { | |
594 | run_lints!(self, check_expr_post, e); | |
595 | } | |
596 | ||
597 | fn visit_generics(&mut self, g: &ast::Generics) { | |
598 | run_lints!(self, check_generics, g); | |
599 | visit::walk_generics(self, g); | |
600 | } | |
601 | ||
c34b1796 AL |
602 | fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) { |
603 | self.with_lint_attrs(&trait_item.attrs, |cx| { | |
604 | run_lints!(cx, check_trait_item, trait_item); | |
605 | cx.visit_ids(|v| v.visit_trait_item(trait_item)); | |
606 | visit::walk_trait_item(cx, trait_item); | |
607 | }); | |
608 | } | |
609 | ||
610 | fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) { | |
611 | self.with_lint_attrs(&impl_item.attrs, |cx| { | |
612 | run_lints!(cx, check_impl_item, impl_item); | |
613 | cx.visit_ids(|v| v.visit_impl_item(impl_item)); | |
614 | visit::walk_impl_item(cx, impl_item); | |
615 | }); | |
1a4d82fc JJ |
616 | } |
617 | ||
618 | fn visit_opt_lifetime_ref(&mut self, sp: Span, lt: &Option<ast::Lifetime>) { | |
619 | run_lints!(self, check_opt_lifetime_ref, sp, lt); | |
620 | } | |
621 | ||
622 | fn visit_lifetime_ref(&mut self, lt: &ast::Lifetime) { | |
623 | run_lints!(self, check_lifetime_ref, lt); | |
624 | } | |
625 | ||
626 | fn visit_lifetime_def(&mut self, lt: &ast::LifetimeDef) { | |
627 | run_lints!(self, check_lifetime_def, lt); | |
628 | } | |
629 | ||
630 | fn visit_explicit_self(&mut self, es: &ast::ExplicitSelf) { | |
631 | run_lints!(self, check_explicit_self, es); | |
632 | visit::walk_explicit_self(self, es); | |
633 | } | |
634 | ||
635 | fn visit_mac(&mut self, mac: &ast::Mac) { | |
636 | run_lints!(self, check_mac, mac); | |
637 | visit::walk_mac(self, mac); | |
638 | } | |
639 | ||
640 | fn visit_path(&mut self, p: &ast::Path, id: ast::NodeId) { | |
641 | run_lints!(self, check_path, p, id); | |
642 | visit::walk_path(self, p); | |
643 | } | |
644 | ||
645 | fn visit_attribute(&mut self, attr: &ast::Attribute) { | |
646 | run_lints!(self, check_attribute, attr); | |
647 | } | |
648 | } | |
649 | ||
650 | // Output any lints that were previously added to the session. | |
651 | impl<'a, 'tcx> IdVisitingOperation for Context<'a, 'tcx> { | |
652 | fn visit_id(&mut self, id: ast::NodeId) { | |
653 | match self.tcx.sess.lints.borrow_mut().remove(&id) { | |
654 | None => {} | |
655 | Some(lints) => { | |
85aaf69f SL |
656 | for (lint_id, span, msg) in lints { |
657 | self.span_lint(lint_id.lint, span, &msg[..]) | |
1a4d82fc JJ |
658 | } |
659 | } | |
660 | } | |
661 | } | |
662 | } | |
663 | ||
664 | // This lint pass is defined here because it touches parts of the `Context` | |
665 | // that we don't want to expose. It records the lint level at certain AST | |
666 | // nodes, so that the variant size difference check in trans can call | |
667 | // `raw_emit_lint`. | |
668 | ||
c34b1796 | 669 | pub struct GatherNodeLevels; |
1a4d82fc JJ |
670 | |
671 | impl LintPass for GatherNodeLevels { | |
672 | fn get_lints(&self) -> LintArray { | |
673 | lint_array!() | |
674 | } | |
675 | ||
676 | fn check_item(&mut self, cx: &Context, it: &ast::Item) { | |
677 | match it.node { | |
678 | ast::ItemEnum(..) => { | |
679 | let lint_id = LintId::of(builtin::VARIANT_SIZE_DIFFERENCES); | |
680 | let lvlsrc = cx.lints.get_level_source(lint_id); | |
681 | match lvlsrc { | |
682 | (lvl, _) if lvl != Allow => { | |
683 | cx.node_levels.borrow_mut() | |
684 | .insert((it.id, lint_id), lvlsrc); | |
685 | }, | |
686 | _ => { } | |
687 | } | |
688 | }, | |
689 | _ => { } | |
690 | } | |
691 | } | |
692 | } | |
693 | ||
694 | /// Perform lint checking on a crate. | |
695 | /// | |
696 | /// Consumes the `lint_store` field of the `Session`. | |
697 | pub fn check_crate(tcx: &ty::ctxt, | |
698 | exported_items: &ExportedItems) { | |
699 | ||
1a4d82fc JJ |
700 | let krate = tcx.map.krate(); |
701 | let mut cx = Context::new(tcx, krate, exported_items); | |
702 | ||
703 | // Visit the whole crate. | |
c34b1796 | 704 | cx.with_lint_attrs(&krate.attrs, |cx| { |
1a4d82fc JJ |
705 | cx.visit_id(ast::CRATE_NODE_ID); |
706 | cx.visit_ids(|v| { | |
707 | v.visited_outermost = true; | |
708 | visit::walk_crate(v, krate); | |
709 | }); | |
710 | ||
711 | // since the root module isn't visited as an item (because it isn't an | |
712 | // item), warn for it here. | |
713 | run_lints!(cx, check_crate, krate); | |
714 | ||
715 | visit::walk_crate(cx, krate); | |
716 | }); | |
717 | ||
718 | // If we missed any lints added to the session, then there's a bug somewhere | |
719 | // in the iteration code. | |
62682a34 | 720 | for (id, v) in tcx.sess.lints.borrow().iter() { |
85aaf69f | 721 | for &(lint, span, ref msg) in v { |
1a4d82fc | 722 | tcx.sess.span_bug(span, |
85aaf69f SL |
723 | &format!("unprocessed lint {} at {}: {}", |
724 | lint.as_str(), tcx.map.node_to_string(*id), *msg)) | |
1a4d82fc JJ |
725 | } |
726 | } | |
727 | ||
1a4d82fc JJ |
728 | *tcx.node_lint_levels.borrow_mut() = cx.node_levels.into_inner(); |
729 | } |