]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_lint/src/context.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / compiler / rustc_lint / src / context.rs
1 //! Implementation of lint checking.
2 //!
3 //! The lint checking is mostly consolidated into one pass which runs
4 //! after all other analyses. Throughout compilation, lint warnings
5 //! can be added via the `add_lint` method on the Session structure. This
6 //! requires a span and an ID of the node that the lint is being added to. The
7 //! lint isn't actually emitted at that time because it is unknown what the
8 //! actual lint level at that location is.
9 //!
10 //! To actually emit lint warnings/errors, a separate pass is used.
11 //! A context keeps track of the current state of all lint levels.
12 //! Upon entering a node of the ast which can modify the lint settings, the
13 //! previous lint state is pushed onto a stack and the ast is then recursed
14 //! upon. As the ast is traversed, this keeps track of the current lint level
15 //! for all lint attributes.
16
17 use self::TargetLint::*;
18
19 use crate::levels::{is_known_lint_tool, LintLevelsBuilder};
20 use crate::passes::{EarlyLintPassObject, LateLintPassObject};
21 use rustc_ast as ast;
22 use rustc_data_structures::fx::FxHashMap;
23 use rustc_data_structures::sync;
24 use rustc_errors::{
25 add_elided_lifetime_in_path_suggestion, struct_span_err, Applicability, SuggestionStyle,
26 };
27 use rustc_hir as hir;
28 use rustc_hir::def::Res;
29 use rustc_hir::def_id::{CrateNum, DefId};
30 use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
31 use rustc_middle::lint::LintDiagnosticBuilder;
32 use rustc_middle::middle::privacy::AccessLevels;
33 use rustc_middle::middle::stability;
34 use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
35 use rustc_middle::ty::print::with_no_trimmed_paths;
36 use rustc_middle::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt};
37 use rustc_serialize::json::Json;
38 use rustc_session::lint::{BuiltinLintDiagnostics, ExternDepSpec};
39 use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
40 use rustc_session::Session;
41 use rustc_session::SessionLintStore;
42 use rustc_span::lev_distance::find_best_match_for_name;
43 use rustc_span::{symbol::Symbol, MultiSpan, Span, DUMMY_SP};
44 use rustc_target::abi::{self, LayoutOf};
45 use tracing::debug;
46
47 use std::cell::Cell;
48 use std::iter;
49 use std::slice;
50
51 /// Information about the registered lints.
52 ///
53 /// This is basically the subset of `Context` that we can
54 /// build early in the compile pipeline.
55 pub struct LintStore {
56 /// Registered lints.
57 lints: Vec<&'static Lint>,
58
59 /// Constructor functions for each variety of lint pass.
60 ///
61 /// These should only be called once, but since we want to avoid locks or
62 /// interior mutability, we don't enforce this (and lints should, in theory,
63 /// be compatible with being constructed more than once, though not
64 /// necessarily in a sane manner. This is safe though.)
65 pub pre_expansion_passes: Vec<Box<dyn Fn() -> EarlyLintPassObject + sync::Send + sync::Sync>>,
66 pub early_passes: Vec<Box<dyn Fn() -> EarlyLintPassObject + sync::Send + sync::Sync>>,
67 pub late_passes: Vec<Box<dyn Fn() -> LateLintPassObject + sync::Send + sync::Sync>>,
68 /// This is unique in that we construct them per-module, so not once.
69 pub late_module_passes: Vec<Box<dyn Fn() -> LateLintPassObject + sync::Send + sync::Sync>>,
70
71 /// Lints indexed by name.
72 by_name: FxHashMap<String, TargetLint>,
73
74 /// Map of registered lint groups to what lints they expand to.
75 lint_groups: FxHashMap<&'static str, LintGroup>,
76 }
77
78 impl SessionLintStore for LintStore {
79 fn name_to_lint(&self, lint_name: &str) -> LintId {
80 let lints = self
81 .find_lints(lint_name)
82 .unwrap_or_else(|_| panic!("Failed to find lint with name `{}`", lint_name));
83
84 if let &[lint] = lints.as_slice() {
85 return lint;
86 } else {
87 panic!("Found mutliple lints with name `{}`: {:?}", lint_name, lints);
88 }
89 }
90 }
91
92 /// The target of the `by_name` map, which accounts for renaming/deprecation.
93 #[derive(Debug)]
94 enum TargetLint {
95 /// A direct lint target
96 Id(LintId),
97
98 /// Temporary renaming, used for easing migration pain; see #16545
99 Renamed(String, LintId),
100
101 /// Lint with this name existed previously, but has been removed/deprecated.
102 /// The string argument is the reason for removal.
103 Removed(String),
104
105 /// A lint name that should give no warnings and have no effect.
106 ///
107 /// This is used by rustc to avoid warning about old rustdoc lints before rustdoc registers them as tool lints.
108 Ignored,
109 }
110
111 pub enum FindLintError {
112 NotFound,
113 Removed,
114 }
115
116 struct LintAlias {
117 name: &'static str,
118 /// Whether deprecation warnings should be suppressed for this alias.
119 silent: bool,
120 }
121
122 struct LintGroup {
123 lint_ids: Vec<LintId>,
124 from_plugin: bool,
125 depr: Option<LintAlias>,
126 }
127
128 pub enum CheckLintNameResult<'a> {
129 Ok(&'a [LintId]),
130 /// Lint doesn't exist. Potentially contains a suggestion for a correct lint name.
131 NoLint(Option<Symbol>),
132 /// The lint refers to a tool that has not been registered.
133 NoTool,
134 /// The lint is either renamed or removed. This is the warning
135 /// message, and an optional new name (`None` if removed).
136 Warning(String, Option<String>),
137 /// The lint is from a tool. If the Option is None, then either
138 /// the lint does not exist in the tool or the code was not
139 /// compiled with the tool and therefore the lint was never
140 /// added to the `LintStore`. Otherwise the `LintId` will be
141 /// returned as if it where a rustc lint.
142 Tool(Result<&'a [LintId], (Option<&'a [LintId]>, String)>),
143 }
144
145 impl LintStore {
146 pub fn new() -> LintStore {
147 LintStore {
148 lints: vec![],
149 pre_expansion_passes: vec![],
150 early_passes: vec![],
151 late_passes: vec![],
152 late_module_passes: vec![],
153 by_name: Default::default(),
154 lint_groups: Default::default(),
155 }
156 }
157
158 pub fn get_lints<'t>(&'t self) -> &'t [&'static Lint] {
159 &self.lints
160 }
161
162 pub fn get_lint_groups<'t>(&'t self) -> Vec<(&'static str, Vec<LintId>, bool)> {
163 self.lint_groups
164 .iter()
165 .filter(|(_, LintGroup { depr, .. })| {
166 // Don't display deprecated lint groups.
167 depr.is_none()
168 })
169 .map(|(k, LintGroup { lint_ids, from_plugin, .. })| {
170 (*k, lint_ids.clone(), *from_plugin)
171 })
172 .collect()
173 }
174
175 pub fn register_early_pass(
176 &mut self,
177 pass: impl Fn() -> EarlyLintPassObject + 'static + sync::Send + sync::Sync,
178 ) {
179 self.early_passes.push(Box::new(pass));
180 }
181
182 /// Used by clippy.
183 pub fn register_pre_expansion_pass(
184 &mut self,
185 pass: impl Fn() -> EarlyLintPassObject + 'static + sync::Send + sync::Sync,
186 ) {
187 self.pre_expansion_passes.push(Box::new(pass));
188 }
189
190 pub fn register_late_pass(
191 &mut self,
192 pass: impl Fn() -> LateLintPassObject + 'static + sync::Send + sync::Sync,
193 ) {
194 self.late_passes.push(Box::new(pass));
195 }
196
197 pub fn register_late_mod_pass(
198 &mut self,
199 pass: impl Fn() -> LateLintPassObject + 'static + sync::Send + sync::Sync,
200 ) {
201 self.late_module_passes.push(Box::new(pass));
202 }
203
204 // Helper method for register_early/late_pass
205 pub fn register_lints(&mut self, lints: &[&'static Lint]) {
206 for lint in lints {
207 self.lints.push(lint);
208
209 let id = LintId::of(lint);
210 if self.by_name.insert(lint.name_lower(), Id(id)).is_some() {
211 bug!("duplicate specification of lint {}", lint.name_lower())
212 }
213
214 if let Some(FutureIncompatibleInfo { reason, .. }) = lint.future_incompatible {
215 if let Some(edition) = reason.edition() {
216 self.lint_groups
217 .entry(edition.lint_name())
218 .or_insert(LintGroup {
219 lint_ids: vec![],
220 from_plugin: lint.is_plugin,
221 depr: None,
222 })
223 .lint_ids
224 .push(id);
225 } else {
226 // Lints belonging to the `future_incompatible` lint group are lints where a
227 // future version of rustc will cause existing code to stop compiling.
228 // Lints tied to an edition don't count because they are opt-in.
229 self.lint_groups
230 .entry("future_incompatible")
231 .or_insert(LintGroup {
232 lint_ids: vec![],
233 from_plugin: lint.is_plugin,
234 depr: None,
235 })
236 .lint_ids
237 .push(id);
238 }
239 }
240 }
241 }
242
243 pub fn register_group_alias(&mut self, lint_name: &'static str, alias: &'static str) {
244 self.lint_groups.insert(
245 alias,
246 LintGroup {
247 lint_ids: vec![],
248 from_plugin: false,
249 depr: Some(LintAlias { name: lint_name, silent: true }),
250 },
251 );
252 }
253
254 pub fn register_group(
255 &mut self,
256 from_plugin: bool,
257 name: &'static str,
258 deprecated_name: Option<&'static str>,
259 to: Vec<LintId>,
260 ) {
261 let new = self
262 .lint_groups
263 .insert(name, LintGroup { lint_ids: to, from_plugin, depr: None })
264 .is_none();
265 if let Some(deprecated) = deprecated_name {
266 self.lint_groups.insert(
267 deprecated,
268 LintGroup {
269 lint_ids: vec![],
270 from_plugin,
271 depr: Some(LintAlias { name, silent: false }),
272 },
273 );
274 }
275
276 if !new {
277 bug!("duplicate specification of lint group {}", name);
278 }
279 }
280
281 /// This lint should give no warning and have no effect.
282 ///
283 /// This is used by rustc to avoid warning about old rustdoc lints before rustdoc registers them as tool lints.
284 #[track_caller]
285 pub fn register_ignored(&mut self, name: &str) {
286 if self.by_name.insert(name.to_string(), Ignored).is_some() {
287 bug!("duplicate specification of lint {}", name);
288 }
289 }
290
291 /// This lint has been renamed; warn about using the new name and apply the lint.
292 #[track_caller]
293 pub fn register_renamed(&mut self, old_name: &str, new_name: &str) {
294 let target = match self.by_name.get(new_name) {
295 Some(&Id(lint_id)) => lint_id,
296 _ => bug!("invalid lint renaming of {} to {}", old_name, new_name),
297 };
298 self.by_name.insert(old_name.to_string(), Renamed(new_name.to_string(), target));
299 }
300
301 pub fn register_removed(&mut self, name: &str, reason: &str) {
302 self.by_name.insert(name.into(), Removed(reason.into()));
303 }
304
305 pub fn find_lints(&self, mut lint_name: &str) -> Result<Vec<LintId>, FindLintError> {
306 match self.by_name.get(lint_name) {
307 Some(&Id(lint_id)) => Ok(vec![lint_id]),
308 Some(&Renamed(_, lint_id)) => Ok(vec![lint_id]),
309 Some(&Removed(_)) => Err(FindLintError::Removed),
310 Some(&Ignored) => Ok(vec![]),
311 None => loop {
312 return match self.lint_groups.get(lint_name) {
313 Some(LintGroup { lint_ids, depr, .. }) => {
314 if let Some(LintAlias { name, .. }) = depr {
315 lint_name = name;
316 continue;
317 }
318 Ok(lint_ids.clone())
319 }
320 None => Err(FindLintError::Removed),
321 };
322 },
323 }
324 }
325
326 /// Checks the validity of lint names derived from the command line.
327 pub fn check_lint_name_cmdline(
328 &self,
329 sess: &Session,
330 lint_name: &str,
331 level: Level,
332 crate_attrs: &[ast::Attribute],
333 ) {
334 let (tool_name, lint_name_only) = parse_lint_and_tool_name(lint_name);
335 if lint_name_only == crate::WARNINGS.name_lower() && level == Level::ForceWarn {
336 return struct_span_err!(
337 sess,
338 DUMMY_SP,
339 E0602,
340 "`{}` lint group is not supported with ´--force-warn´",
341 crate::WARNINGS.name_lower()
342 )
343 .emit();
344 }
345 let db = match self.check_lint_name(sess, lint_name_only, tool_name, crate_attrs) {
346 CheckLintNameResult::Ok(_) => None,
347 CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)),
348 CheckLintNameResult::NoLint(suggestion) => {
349 let mut err =
350 struct_span_err!(sess, DUMMY_SP, E0602, "unknown lint: `{}`", lint_name);
351
352 if let Some(suggestion) = suggestion {
353 err.help(&format!("did you mean: `{}`", suggestion));
354 }
355
356 Some(err)
357 }
358 CheckLintNameResult::Tool(result) => match result {
359 Err((Some(_), new_name)) => Some(sess.struct_warn(&format!(
360 "lint name `{}` is deprecated \
361 and does not have an effect anymore. \
362 Use: {}",
363 lint_name, new_name
364 ))),
365 _ => None,
366 },
367 CheckLintNameResult::NoTool => Some(struct_span_err!(
368 sess,
369 DUMMY_SP,
370 E0602,
371 "unknown lint tool: `{}`",
372 tool_name.unwrap()
373 )),
374 };
375
376 if let Some(mut db) = db {
377 let msg = format!(
378 "requested on the command line with `{} {}`",
379 match level {
380 Level::Allow => "-A",
381 Level::Warn => "-W",
382 Level::ForceWarn => "--force-warn",
383 Level::Deny => "-D",
384 Level::Forbid => "-F",
385 },
386 lint_name
387 );
388 db.note(&msg);
389 db.emit();
390 }
391 }
392
393 /// True if this symbol represents a lint group name.
394 pub fn is_lint_group(&self, lint_name: Symbol) -> bool {
395 debug!(
396 "is_lint_group(lint_name={:?}, lint_groups={:?})",
397 lint_name,
398 self.lint_groups.keys().collect::<Vec<_>>()
399 );
400 let lint_name_str = &*lint_name.as_str();
401 self.lint_groups.contains_key(&lint_name_str) || {
402 let warnings_name_str = crate::WARNINGS.name_lower();
403 lint_name_str == &*warnings_name_str
404 }
405 }
406
407 /// Checks the name of a lint for its existence, and whether it was
408 /// renamed or removed. Generates a DiagnosticBuilder containing a
409 /// warning for renamed and removed lints. This is over both lint
410 /// names from attributes and those passed on the command line. Since
411 /// it emits non-fatal warnings and there are *two* lint passes that
412 /// inspect attributes, this is only run from the late pass to avoid
413 /// printing duplicate warnings.
414 pub fn check_lint_name(
415 &self,
416 sess: &Session,
417 lint_name: &str,
418 tool_name: Option<Symbol>,
419 crate_attrs: &[ast::Attribute],
420 ) -> CheckLintNameResult<'_> {
421 if let Some(tool_name) = tool_name {
422 if !is_known_lint_tool(tool_name, sess, crate_attrs) {
423 return CheckLintNameResult::NoTool;
424 }
425 }
426
427 let complete_name = if let Some(tool_name) = tool_name {
428 format!("{}::{}", tool_name, lint_name)
429 } else {
430 lint_name.to_string()
431 };
432 // If the lint was scoped with `tool::` check if the tool lint exists
433 if let Some(tool_name) = tool_name {
434 match self.by_name.get(&complete_name) {
435 None => match self.lint_groups.get(&*complete_name) {
436 // If the lint isn't registered, there are two possibilities:
437 None => {
438 // 1. The tool is currently running, so this lint really doesn't exist.
439 // FIXME: should this handle tools that never register a lint, like rustfmt?
440 tracing::debug!("lints={:?}", self.by_name.keys().collect::<Vec<_>>());
441 let tool_prefix = format!("{}::", tool_name);
442 return if self.by_name.keys().any(|lint| lint.starts_with(&tool_prefix)) {
443 self.no_lint_suggestion(&complete_name)
444 } else {
445 // 2. The tool isn't currently running, so no lints will be registered.
446 // To avoid giving a false positive, ignore all unknown lints.
447 CheckLintNameResult::Tool(Err((None, String::new())))
448 };
449 }
450 Some(LintGroup { lint_ids, .. }) => {
451 return CheckLintNameResult::Tool(Ok(&lint_ids));
452 }
453 },
454 Some(&Id(ref id)) => return CheckLintNameResult::Tool(Ok(slice::from_ref(id))),
455 // If the lint was registered as removed or renamed by the lint tool, we don't need
456 // to treat tool_lints and rustc lints different and can use the code below.
457 _ => {}
458 }
459 }
460 match self.by_name.get(&complete_name) {
461 Some(&Renamed(ref new_name, _)) => CheckLintNameResult::Warning(
462 format!("lint `{}` has been renamed to `{}`", complete_name, new_name),
463 Some(new_name.to_owned()),
464 ),
465 Some(&Removed(ref reason)) => CheckLintNameResult::Warning(
466 format!("lint `{}` has been removed: {}", complete_name, reason),
467 None,
468 ),
469 None => match self.lint_groups.get(&*complete_name) {
470 // If neither the lint, nor the lint group exists check if there is a `clippy::`
471 // variant of this lint
472 None => self.check_tool_name_for_backwards_compat(&complete_name, "clippy"),
473 Some(LintGroup { lint_ids, depr, .. }) => {
474 // Check if the lint group name is deprecated
475 if let Some(LintAlias { name, silent }) = depr {
476 let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
477 return if *silent {
478 CheckLintNameResult::Ok(&lint_ids)
479 } else {
480 CheckLintNameResult::Tool(Err((Some(&lint_ids), (*name).to_string())))
481 };
482 }
483 CheckLintNameResult::Ok(&lint_ids)
484 }
485 },
486 Some(&Id(ref id)) => CheckLintNameResult::Ok(slice::from_ref(id)),
487 Some(&Ignored) => CheckLintNameResult::Ok(&[]),
488 }
489 }
490
491 fn no_lint_suggestion(&self, lint_name: &str) -> CheckLintNameResult<'_> {
492 let name_lower = lint_name.to_lowercase();
493
494 if lint_name.chars().any(char::is_uppercase) && self.find_lints(&name_lower).is_ok() {
495 // First check if the lint name is (partly) in upper case instead of lower case...
496 return CheckLintNameResult::NoLint(Some(Symbol::intern(&name_lower)));
497 }
498 // ...if not, search for lints with a similar name
499 let groups = self.lint_groups.keys().copied().map(Symbol::intern);
500 let lints = self.lints.iter().map(|l| Symbol::intern(&l.name_lower()));
501 let names: Vec<Symbol> = groups.chain(lints).collect();
502 let suggestion = find_best_match_for_name(&names, Symbol::intern(&name_lower), None);
503 CheckLintNameResult::NoLint(suggestion)
504 }
505
506 fn check_tool_name_for_backwards_compat(
507 &self,
508 lint_name: &str,
509 tool_name: &str,
510 ) -> CheckLintNameResult<'_> {
511 let complete_name = format!("{}::{}", tool_name, lint_name);
512 match self.by_name.get(&complete_name) {
513 None => match self.lint_groups.get(&*complete_name) {
514 // Now we are sure, that this lint exists nowhere
515 None => self.no_lint_suggestion(lint_name),
516 Some(LintGroup { lint_ids, depr, .. }) => {
517 // Reaching this would be weird, but let's cover this case anyway
518 if let Some(LintAlias { name, silent }) = depr {
519 let LintGroup { lint_ids, .. } = self.lint_groups.get(name).unwrap();
520 return if *silent {
521 CheckLintNameResult::Tool(Err((Some(&lint_ids), complete_name)))
522 } else {
523 CheckLintNameResult::Tool(Err((Some(&lint_ids), (*name).to_string())))
524 };
525 }
526 CheckLintNameResult::Tool(Err((Some(&lint_ids), complete_name)))
527 }
528 },
529 Some(&Id(ref id)) => {
530 CheckLintNameResult::Tool(Err((Some(slice::from_ref(id)), complete_name)))
531 }
532 Some(other) => {
533 tracing::debug!("got renamed lint {:?}", other);
534 CheckLintNameResult::NoLint(None)
535 }
536 }
537 }
538 }
539
540 /// Context for lint checking after type checking.
541 pub struct LateContext<'tcx> {
542 /// Type context we're checking in.
543 pub tcx: TyCtxt<'tcx>,
544
545 /// Current body, or `None` if outside a body.
546 pub enclosing_body: Option<hir::BodyId>,
547
548 /// Type-checking results for the current body. Access using the `typeck_results`
549 /// and `maybe_typeck_results` methods, which handle querying the typeck results on demand.
550 // FIXME(eddyb) move all the code accessing internal fields like this,
551 // to this module, to avoid exposing it to lint logic.
552 pub(super) cached_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
553
554 /// Parameter environment for the item we are in.
555 pub param_env: ty::ParamEnv<'tcx>,
556
557 /// Items accessible from the crate being checked.
558 pub access_levels: &'tcx AccessLevels,
559
560 /// The store of registered lints and the lint levels.
561 pub lint_store: &'tcx LintStore,
562
563 pub last_node_with_lint_attrs: hir::HirId,
564
565 /// Generic type parameters in scope for the item we are in.
566 pub generics: Option<&'tcx hir::Generics<'tcx>>,
567
568 /// We are only looking at one module
569 pub only_module: bool,
570 }
571
572 /// Context for lint checking of the AST, after expansion, before lowering to
573 /// HIR.
574 pub struct EarlyContext<'a> {
575 /// Type context we're checking in.
576 pub sess: &'a Session,
577
578 /// The crate being checked.
579 pub krate: &'a ast::Crate,
580
581 pub builder: LintLevelsBuilder<'a>,
582
583 /// The store of registered lints and the lint levels.
584 pub lint_store: &'a LintStore,
585
586 pub buffered: LintBuffer,
587 }
588
589 pub trait LintPassObject: Sized {}
590
591 impl LintPassObject for EarlyLintPassObject {}
592
593 impl LintPassObject for LateLintPassObject {}
594
595 pub trait LintContext: Sized {
596 type PassObject: LintPassObject;
597
598 fn sess(&self) -> &Session;
599 fn lints(&self) -> &LintStore;
600
601 fn lookup_with_diagnostics(
602 &self,
603 lint: &'static Lint,
604 span: Option<impl Into<MultiSpan>>,
605 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
606 diagnostic: BuiltinLintDiagnostics,
607 ) {
608 self.lookup(lint, span, |lint| {
609 // We first generate a blank diagnostic.
610 let mut db = lint.build("");
611
612 // Now, set up surrounding context.
613 let sess = self.sess();
614 match diagnostic {
615 BuiltinLintDiagnostics::Normal => (),
616 BuiltinLintDiagnostics::BareTraitObject(span, is_global) => {
617 let (sugg, app) = match sess.source_map().span_to_snippet(span) {
618 Ok(s) if is_global => {
619 (format!("dyn ({})", s), Applicability::MachineApplicable)
620 }
621 Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
622 Err(_) => ("dyn <type>".to_string(), Applicability::HasPlaceholders),
623 };
624 db.span_suggestion(span, "use `dyn`", sugg, app);
625 }
626 BuiltinLintDiagnostics::AbsPathWithModule(span) => {
627 let (sugg, app) = match sess.source_map().span_to_snippet(span) {
628 Ok(ref s) => {
629 // FIXME(Manishearth) ideally the emitting code
630 // can tell us whether or not this is global
631 let opt_colon =
632 if s.trim_start().starts_with("::") { "" } else { "::" };
633
634 (format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
635 }
636 Err(_) => ("crate::<path>".to_string(), Applicability::HasPlaceholders),
637 };
638 db.span_suggestion(span, "use `crate`", sugg, app);
639 }
640 BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(span) => {
641 db.span_label(
642 span,
643 "names from parent modules are not accessible without an explicit import",
644 );
645 }
646 BuiltinLintDiagnostics::MacroExpandedMacroExportsAccessedByAbsolutePaths(
647 span_def,
648 ) => {
649 db.span_note(span_def, "the macro is defined here");
650 }
651 BuiltinLintDiagnostics::ElidedLifetimesInPaths(
652 n,
653 path_span,
654 incl_angl_brckt,
655 insertion_span,
656 anon_lts,
657 ) => {
658 add_elided_lifetime_in_path_suggestion(
659 sess.source_map(),
660 &mut db,
661 n,
662 path_span,
663 incl_angl_brckt,
664 insertion_span,
665 anon_lts,
666 );
667 }
668 BuiltinLintDiagnostics::UnknownCrateTypes(span, note, sugg) => {
669 db.span_suggestion(span, &note, sugg, Applicability::MaybeIncorrect);
670 }
671 BuiltinLintDiagnostics::UnusedImports(message, replaces) => {
672 if !replaces.is_empty() {
673 db.tool_only_multipart_suggestion(
674 &message,
675 replaces,
676 Applicability::MachineApplicable,
677 );
678 }
679 }
680 BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
681 for (span, is_imported) in spans {
682 let introduced = if is_imported { "imported" } else { "defined" };
683 db.span_label(
684 span,
685 format!("the item `{}` is already {} here", ident, introduced),
686 );
687 }
688 }
689 BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span) => {
690 stability::deprecation_suggestion(&mut db, "macro", suggestion, span)
691 }
692 BuiltinLintDiagnostics::UnusedDocComment(span) => {
693 db.span_label(span, "rustdoc does not generate documentation for macro invocations");
694 db.help("to document an item produced by a macro, \
695 the macro must produce the documentation as part of its expansion");
696 }
697 BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident) => {
698 db.span_suggestion(span, "remove `mut` from the parameter", ident.to_string(), Applicability::MachineApplicable);
699 }
700 BuiltinLintDiagnostics::MissingAbi(span, default_abi) => {
701 db.span_label(span, "ABI should be specified here");
702 db.help(&format!("the default ABI is {}", default_abi.name()));
703 }
704 BuiltinLintDiagnostics::LegacyDeriveHelpers(span) => {
705 db.span_label(span, "the attribute is introduced here");
706 }
707 BuiltinLintDiagnostics::ExternDepSpec(krate, loc) => {
708 let json = match loc {
709 ExternDepSpec::Json(json) => {
710 db.help(&format!("remove unnecessary dependency `{}`", krate));
711 json
712 }
713 ExternDepSpec::Raw(raw) => {
714 db.help(&format!("remove unnecessary dependency `{}` at `{}`", krate, raw));
715 db.span_suggestion_with_style(
716 DUMMY_SP,
717 "raw extern location",
718 raw.clone(),
719 Applicability::Unspecified,
720 SuggestionStyle::CompletelyHidden,
721 );
722 Json::String(raw)
723 }
724 };
725 db.tool_only_suggestion_with_metadata(
726 "json extern location",
727 Applicability::Unspecified,
728 json
729 );
730 }
731 BuiltinLintDiagnostics::ProcMacroBackCompat(note) => {
732 db.note(&note);
733 }
734 BuiltinLintDiagnostics::OrPatternsBackCompat(span,suggestion) => {
735 db.span_suggestion(span, "use pat_param to preserve semantics", suggestion, Applicability::MachineApplicable);
736 }
737 BuiltinLintDiagnostics::ReservedPrefix(span) => {
738 db.span_label(span, "unknown prefix");
739 db.span_suggestion_verbose(
740 span.shrink_to_hi(),
741 "insert whitespace here to avoid this being parsed as a prefix in Rust 2021",
742 " ".into(),
743 Applicability::MachineApplicable,
744 );
745 }
746 BuiltinLintDiagnostics::UnusedBuiltinAttribute {
747 attr_name,
748 macro_name,
749 invoc_span
750 } => {
751 db.span_note(
752 invoc_span,
753 &format!("the built-in attribute `{attr_name}` will be ignored, since it's applied to the macro invocation `{macro_name}`")
754 );
755 }
756 BuiltinLintDiagnostics::TrailingMacro(is_trailing, name) => {
757 if is_trailing {
758 db.note("macro invocations at the end of a block are treated as expressions");
759 db.note(&format!("to ignore the value produced by the macro, add a semicolon after the invocation of `{name}`"));
760 }
761 }
762 BuiltinLintDiagnostics::BreakWithLabelAndLoop(span) => {
763 db.multipart_suggestion(
764 "wrap this expression in parentheses",
765 vec![(span.shrink_to_lo(), "(".to_string()),
766 (span.shrink_to_hi(), ")".to_string())],
767 Applicability::MachineApplicable
768 );
769 }
770 BuiltinLintDiagnostics::NamedAsmLabel(help) => {
771 db.help(&help);
772 db.note("see the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information");
773 }
774 }
775 // Rewrap `db`, and pass control to the user.
776 decorate(LintDiagnosticBuilder::new(db));
777 });
778 }
779
780 // FIXME: These methods should not take an Into<MultiSpan> -- instead, callers should need to
781 // set the span in their `decorate` function (preferably using set_span).
782 fn lookup<S: Into<MultiSpan>>(
783 &self,
784 lint: &'static Lint,
785 span: Option<S>,
786 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
787 );
788
789 fn struct_span_lint<S: Into<MultiSpan>>(
790 &self,
791 lint: &'static Lint,
792 span: S,
793 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
794 ) {
795 self.lookup(lint, Some(span), decorate);
796 }
797 /// Emit a lint at the appropriate level, with no associated span.
798 fn lint(&self, lint: &'static Lint, decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>)) {
799 self.lookup(lint, None as Option<Span>, decorate);
800 }
801 }
802
803 impl<'a> EarlyContext<'a> {
804 pub fn new(
805 sess: &'a Session,
806 lint_store: &'a LintStore,
807 krate: &'a ast::Crate,
808 buffered: LintBuffer,
809 warn_about_weird_lints: bool,
810 ) -> EarlyContext<'a> {
811 EarlyContext {
812 sess,
813 krate,
814 lint_store,
815 builder: LintLevelsBuilder::new(sess, warn_about_weird_lints, lint_store, &krate.attrs),
816 buffered,
817 }
818 }
819 }
820
821 impl LintContext for LateContext<'_> {
822 type PassObject = LateLintPassObject;
823
824 /// Gets the overall compiler `Session` object.
825 fn sess(&self) -> &Session {
826 &self.tcx.sess
827 }
828
829 fn lints(&self) -> &LintStore {
830 &*self.lint_store
831 }
832
833 fn lookup<S: Into<MultiSpan>>(
834 &self,
835 lint: &'static Lint,
836 span: Option<S>,
837 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
838 ) {
839 let hir_id = self.last_node_with_lint_attrs;
840
841 match span {
842 Some(s) => self.tcx.struct_span_lint_hir(lint, hir_id, s, decorate),
843 None => self.tcx.struct_lint_node(lint, hir_id, decorate),
844 }
845 }
846 }
847
848 impl LintContext for EarlyContext<'_> {
849 type PassObject = EarlyLintPassObject;
850
851 /// Gets the overall compiler `Session` object.
852 fn sess(&self) -> &Session {
853 &self.sess
854 }
855
856 fn lints(&self) -> &LintStore {
857 &*self.lint_store
858 }
859
860 fn lookup<S: Into<MultiSpan>>(
861 &self,
862 lint: &'static Lint,
863 span: Option<S>,
864 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a>),
865 ) {
866 self.builder.struct_lint(lint, span.map(|s| s.into()), decorate)
867 }
868 }
869
870 impl<'tcx> LateContext<'tcx> {
871 /// Gets the type-checking results for the current body,
872 /// or `None` if outside a body.
873 pub fn maybe_typeck_results(&self) -> Option<&'tcx ty::TypeckResults<'tcx>> {
874 self.cached_typeck_results.get().or_else(|| {
875 self.enclosing_body.map(|body| {
876 let typeck_results = self.tcx.typeck_body(body);
877 self.cached_typeck_results.set(Some(typeck_results));
878 typeck_results
879 })
880 })
881 }
882
883 /// Gets the type-checking results for the current body.
884 /// As this will ICE if called outside bodies, only call when working with
885 /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
886 #[track_caller]
887 pub fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
888 self.maybe_typeck_results().expect("`LateContext::typeck_results` called outside of body")
889 }
890
891 /// Returns the final resolution of a `QPath`, or `Res::Err` if unavailable.
892 /// Unlike `.typeck_results().qpath_res(qpath, id)`, this can be used even outside
893 /// bodies (e.g. for paths in `hir::Ty`), without any risk of ICE-ing.
894 pub fn qpath_res(&self, qpath: &hir::QPath<'_>, id: hir::HirId) -> Res {
895 match *qpath {
896 hir::QPath::Resolved(_, ref path) => path.res,
897 hir::QPath::TypeRelative(..) | hir::QPath::LangItem(..) => self
898 .maybe_typeck_results()
899 .filter(|typeck_results| typeck_results.hir_owner == id.owner)
900 .or_else(|| {
901 if self.tcx.has_typeck_results(id.owner.to_def_id()) {
902 Some(self.tcx.typeck(id.owner))
903 } else {
904 None
905 }
906 })
907 .and_then(|typeck_results| typeck_results.type_dependent_def(id))
908 .map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)),
909 }
910 }
911
912 /// Check if a `DefId`'s path matches the given absolute type path usage.
913 ///
914 /// Anonymous scopes such as `extern` imports are matched with `kw::Empty`;
915 /// inherent `impl` blocks are matched with the name of the type.
916 ///
917 /// Instead of using this method, it is often preferable to instead use
918 /// `rustc_diagnostic_item` or a `lang_item`. This is less prone to errors
919 /// as paths get invalidated if the target definition moves.
920 ///
921 /// # Examples
922 ///
923 /// ```rust,ignore (no context or def id available)
924 /// if cx.match_def_path(def_id, &[sym::core, sym::option, sym::Option]) {
925 /// // The given `def_id` is that of an `Option` type
926 /// }
927 /// ```
928 ///
929 /// Used by clippy, but should be replaced by diagnostic items eventually.
930 pub fn match_def_path(&self, def_id: DefId, path: &[Symbol]) -> bool {
931 let names = self.get_def_path(def_id);
932
933 names.len() == path.len() && iter::zip(names, path).all(|(a, &b)| a == b)
934 }
935
936 /// Gets the absolute path of `def_id` as a vector of `Symbol`.
937 ///
938 /// # Examples
939 ///
940 /// ```rust,ignore (no context or def id available)
941 /// let def_path = cx.get_def_path(def_id);
942 /// if let &[sym::core, sym::option, sym::Option] = &def_path[..] {
943 /// // The given `def_id` is that of an `Option` type
944 /// }
945 /// ```
946 pub fn get_def_path(&self, def_id: DefId) -> Vec<Symbol> {
947 pub struct AbsolutePathPrinter<'tcx> {
948 pub tcx: TyCtxt<'tcx>,
949 }
950
951 impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
952 type Error = !;
953
954 type Path = Vec<Symbol>;
955 type Region = ();
956 type Type = ();
957 type DynExistential = ();
958 type Const = ();
959
960 fn tcx(&self) -> TyCtxt<'tcx> {
961 self.tcx
962 }
963
964 fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
965 Ok(())
966 }
967
968 fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
969 Ok(())
970 }
971
972 fn print_dyn_existential(
973 self,
974 _predicates: &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
975 ) -> Result<Self::DynExistential, Self::Error> {
976 Ok(())
977 }
978
979 fn print_const(self, _ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
980 Ok(())
981 }
982
983 fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
984 Ok(vec![self.tcx.crate_name(cnum)])
985 }
986
987 fn path_qualified(
988 self,
989 self_ty: Ty<'tcx>,
990 trait_ref: Option<ty::TraitRef<'tcx>>,
991 ) -> Result<Self::Path, Self::Error> {
992 if trait_ref.is_none() {
993 if let ty::Adt(def, substs) = self_ty.kind() {
994 return self.print_def_path(def.did, substs);
995 }
996 }
997
998 // This shouldn't ever be needed, but just in case:
999 with_no_trimmed_paths(|| {
1000 Ok(vec![match trait_ref {
1001 Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)),
1002 None => Symbol::intern(&format!("<{}>", self_ty)),
1003 }])
1004 })
1005 }
1006
1007 fn path_append_impl(
1008 self,
1009 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
1010 _disambiguated_data: &DisambiguatedDefPathData,
1011 self_ty: Ty<'tcx>,
1012 trait_ref: Option<ty::TraitRef<'tcx>>,
1013 ) -> Result<Self::Path, Self::Error> {
1014 let mut path = print_prefix(self)?;
1015
1016 // This shouldn't ever be needed, but just in case:
1017 path.push(match trait_ref {
1018 Some(trait_ref) => with_no_trimmed_paths(|| {
1019 Symbol::intern(&format!(
1020 "<impl {} for {}>",
1021 trait_ref.print_only_trait_path(),
1022 self_ty
1023 ))
1024 }),
1025 None => {
1026 with_no_trimmed_paths(|| Symbol::intern(&format!("<impl {}>", self_ty)))
1027 }
1028 });
1029
1030 Ok(path)
1031 }
1032
1033 fn path_append(
1034 self,
1035 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
1036 disambiguated_data: &DisambiguatedDefPathData,
1037 ) -> Result<Self::Path, Self::Error> {
1038 let mut path = print_prefix(self)?;
1039
1040 // Skip `::{{constructor}}` on tuple/unit structs.
1041 if let DefPathData::Ctor = disambiguated_data.data {
1042 return Ok(path);
1043 }
1044
1045 path.push(Symbol::intern(&disambiguated_data.data.to_string()));
1046 Ok(path)
1047 }
1048
1049 fn path_generic_args(
1050 self,
1051 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
1052 _args: &[GenericArg<'tcx>],
1053 ) -> Result<Self::Path, Self::Error> {
1054 print_prefix(self)
1055 }
1056 }
1057
1058 AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap()
1059 }
1060 }
1061
1062 impl<'tcx> abi::HasDataLayout for LateContext<'tcx> {
1063 #[inline]
1064 fn data_layout(&self) -> &abi::TargetDataLayout {
1065 &self.tcx.data_layout
1066 }
1067 }
1068
1069 impl<'tcx> ty::layout::HasTyCtxt<'tcx> for LateContext<'tcx> {
1070 #[inline]
1071 fn tcx(&self) -> TyCtxt<'tcx> {
1072 self.tcx
1073 }
1074 }
1075
1076 impl<'tcx> ty::layout::HasParamEnv<'tcx> for LateContext<'tcx> {
1077 #[inline]
1078 fn param_env(&self) -> ty::ParamEnv<'tcx> {
1079 self.param_env
1080 }
1081 }
1082
1083 impl<'tcx> LayoutOf<'tcx> for LateContext<'tcx> {
1084 type Ty = Ty<'tcx>;
1085 type TyAndLayout = Result<TyAndLayout<'tcx>, LayoutError<'tcx>>;
1086
1087 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
1088 self.tcx.layout_of(self.param_env.and(ty))
1089 }
1090 }
1091
1092 pub fn parse_lint_and_tool_name(lint_name: &str) -> (Option<Symbol>, &str) {
1093 match lint_name.split_once("::") {
1094 Some((tool_name, lint_name)) => {
1095 let tool_name = Symbol::intern(tool_name);
1096
1097 (Some(tool_name), lint_name)
1098 }
1099 None => (None, lint_name),
1100 }
1101 }