]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/lint.rs
Move some code and add comments.
[rust.git] / compiler / rustc_middle / src / lint.rs
1 use std::cmp;
2
3 use rustc_data_structures::fx::FxHashMap;
4 use rustc_errors::{Diagnostic, DiagnosticId, LintDiagnosticBuilder, MultiSpan};
5 use rustc_hir::HirId;
6 use rustc_session::lint::{
7     builtin::{self, FORBIDDEN_LINT_GROUPS},
8     FutureIncompatibilityReason, Level, Lint, LintId,
9 };
10 use rustc_session::Session;
11 use rustc_span::hygiene::MacroKind;
12 use rustc_span::source_map::{DesugaringKind, ExpnKind};
13 use rustc_span::{symbol, Span, Symbol, DUMMY_SP};
14
15 use crate::ty::TyCtxt;
16
17 /// How a lint level was set.
18 #[derive(Clone, Copy, PartialEq, Eq, HashStable, Debug)]
19 pub enum LintLevelSource {
20     /// Lint is at the default level as declared
21     /// in rustc or a plugin.
22     Default,
23
24     /// Lint level was set by an attribute.
25     Node {
26         name: Symbol,
27         span: Span,
28         /// RFC 2383 reason
29         reason: Option<Symbol>,
30         /// The lint tool. (e.g. rustdoc, clippy)
31         tool: Option<Symbol>,
32     },
33
34     /// Lint level was set by a command-line flag.
35     /// The provided `Level` is the level specified on the command line.
36     /// (The actual level may be lower due to `--cap-lints`.)
37     CommandLine(Symbol, Level),
38 }
39
40 impl LintLevelSource {
41     pub fn name(&self) -> Symbol {
42         match *self {
43             LintLevelSource::Default => symbol::kw::Default,
44             LintLevelSource::Node { name, .. } => name,
45             LintLevelSource::CommandLine(name, _) => name,
46         }
47     }
48
49     pub fn span(&self) -> Span {
50         match *self {
51             LintLevelSource::Default => DUMMY_SP,
52             LintLevelSource::Node { span, .. } => span,
53             LintLevelSource::CommandLine(_, _) => DUMMY_SP,
54         }
55     }
56 }
57
58 /// A tuple of a lint level and its source.
59 pub type LevelAndSource = (Level, LintLevelSource);
60
61 /// Return type for the `shallow_lint_levels_on` query.
62 ///
63 /// This map represents the set of allowed lints and allowance levels given
64 /// by the attributes for *a single HirId*.
65 #[derive(Default, Debug, HashStable)]
66 pub struct ShallowLintLevelMap {
67     pub specs: FxHashMap<LintId, LevelAndSource>,
68 }
69
70 /// From an initial level and source, verify the effect of special annotations:
71 /// `warnings` lint level and lint caps.
72 ///
73 /// The return of this function is suitable for diagnostics.
74 pub fn reveal_actual_level(
75     level: Option<Level>,
76     src: &mut LintLevelSource,
77     sess: &Session,
78     lint: LintId,
79     probe_for_lint_level: impl FnOnce(LintId) -> (Option<Level>, LintLevelSource),
80 ) -> Level {
81     // If `level` is none then we actually assume the default level for this lint.
82     let mut level = level.unwrap_or_else(|| lint.lint.default_level(sess.edition()));
83
84     // If we're about to issue a warning, check at the last minute for any
85     // directives against the warnings "lint". If, for example, there's an
86     // `allow(warnings)` in scope then we want to respect that instead.
87     //
88     // We exempt `FORBIDDEN_LINT_GROUPS` from this because it specifically
89     // triggers in cases (like #80988) where you have `forbid(warnings)`,
90     // and so if we turned that into an error, it'd defeat the purpose of the
91     // future compatibility warning.
92     if level == Level::Warn && lint != LintId::of(FORBIDDEN_LINT_GROUPS) {
93         let (warnings_level, warnings_src) = probe_for_lint_level(LintId::of(builtin::WARNINGS));
94         if let Some(configured_warning_level) = warnings_level {
95             if configured_warning_level != Level::Warn {
96                 level = configured_warning_level;
97                 *src = warnings_src;
98             }
99         }
100     }
101
102     // Ensure that we never exceed the `--cap-lints` argument unless the source is a --force-warn
103     level = if let LintLevelSource::CommandLine(_, Level::ForceWarn(_)) = src {
104         level
105     } else {
106         cmp::min(level, sess.opts.lint_cap.unwrap_or(Level::Forbid))
107     };
108
109     if let Some(driver_level) = sess.driver_lint_caps.get(&lint) {
110         // Ensure that we never exceed driver level.
111         level = cmp::min(*driver_level, level);
112     }
113
114     level
115 }
116
117 impl ShallowLintLevelMap {
118     /// Perform a deep probe in the HIR tree looking for the actual level for the lint.
119     /// This lint level is not usable for diagnostics, it needs to be corrected by
120     /// `reveal_actual_level` beforehand.
121     fn probe_for_lint_level(
122         &self,
123         tcx: TyCtxt<'_>,
124         id: LintId,
125         start: HirId,
126     ) -> (Option<Level>, LintLevelSource) {
127         if let Some(&(level, src)) = self.specs.get(&id) {
128             return (Some(level), src);
129         }
130
131         for (parent, _) in tcx.hir().parent_iter(start) {
132             let specs = tcx.shallow_lint_levels_on(parent);
133             if let Some(&(level, src)) = specs.specs.get(&id) {
134                 return (Some(level), src);
135             }
136         }
137         (None, LintLevelSource::Default)
138     }
139
140     /// Fetch and return the user-visible lint level for the given lint at the given HirId.
141     pub fn lint_level_id_at_node(
142         &self,
143         tcx: TyCtxt<'_>,
144         lint: LintId,
145         id: HirId,
146     ) -> (Level, LintLevelSource) {
147         let (level, mut src) = self.probe_for_lint_level(tcx, lint, id);
148         let level = reveal_actual_level(level, &mut src, tcx.sess, lint, |lint| {
149             self.probe_for_lint_level(tcx, lint, id)
150         });
151         debug!(?id, ?level, ?src);
152         (level, src)
153     }
154 }
155
156 impl TyCtxt<'_> {
157     /// Fetch and return the user-visible lint level for the given lint at the given HirId.
158     pub fn lint_level_at_node(self, lint: &'static Lint, id: HirId) -> (Level, LintLevelSource) {
159         self.shallow_lint_levels_on(id).lint_level_id_at_node(self, LintId::of(lint), id)
160     }
161
162     /// Walks upwards from `id` to find a node which might change lint levels with attributes.
163     /// It stops at `bound` and just returns it if reached.
164     pub fn maybe_lint_level_root_bounded(self, mut id: HirId, bound: HirId) -> HirId {
165         let hir = self.hir();
166         while id != bound && self.shallow_lint_levels_on(id).specs.is_empty() {
167             id = hir.get_parent_node(id)
168         }
169         id
170     }
171 }
172
173 /// This struct represents a lint expectation and holds all required information
174 /// to emit the `unfulfilled_lint_expectations` lint if it is unfulfilled after
175 /// the `LateLintPass` has completed.
176 #[derive(Clone, Debug, HashStable)]
177 pub struct LintExpectation {
178     /// The reason for this expectation that can optionally be added as part of
179     /// the attribute. It will be displayed as part of the lint message.
180     pub reason: Option<Symbol>,
181     /// The [`Span`] of the attribute that this expectation originated from.
182     pub emission_span: Span,
183     /// Lint messages for the `unfulfilled_lint_expectations` lint will be
184     /// adjusted to include an additional note. Therefore, we have to track if
185     /// the expectation is for the lint.
186     pub is_unfulfilled_lint_expectations: bool,
187     /// This will hold the name of the tool that this lint belongs to. For
188     /// the lint `clippy::some_lint` the tool would be `clippy`, the same
189     /// goes for `rustdoc`. This will be `None` for rustc lints
190     pub lint_tool: Option<Symbol>,
191 }
192
193 impl LintExpectation {
194     pub fn new(
195         reason: Option<Symbol>,
196         emission_span: Span,
197         is_unfulfilled_lint_expectations: bool,
198         lint_tool: Option<Symbol>,
199     ) -> Self {
200         Self { reason, emission_span, is_unfulfilled_lint_expectations, lint_tool }
201     }
202 }
203
204 pub fn explain_lint_level_source(
205     lint: &'static Lint,
206     level: Level,
207     src: LintLevelSource,
208     err: &mut Diagnostic,
209 ) {
210     let name = lint.name_lower();
211     match src {
212         LintLevelSource::Default => {
213             err.note_once(&format!("`#[{}({})]` on by default", level.as_str(), name));
214         }
215         LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
216             let flag = match orig_level {
217                 Level::Warn => "-W",
218                 Level::Deny => "-D",
219                 Level::Forbid => "-F",
220                 Level::Allow => "-A",
221                 Level::ForceWarn(_) => "--force-warn",
222                 Level::Expect(_) => {
223                     unreachable!("the expect level does not have a commandline flag")
224                 }
225             };
226             let hyphen_case_lint_name = name.replace('_', "-");
227             if lint_flag_val.as_str() == name {
228                 err.note_once(&format!(
229                     "requested on the command line with `{} {}`",
230                     flag, hyphen_case_lint_name
231                 ));
232             } else {
233                 let hyphen_case_flag_val = lint_flag_val.as_str().replace('_', "-");
234                 err.note_once(&format!(
235                     "`{} {}` implied by `{} {}`",
236                     flag, hyphen_case_lint_name, flag, hyphen_case_flag_val
237                 ));
238             }
239         }
240         LintLevelSource::Node { name: lint_attr_name, span, reason, .. } => {
241             if let Some(rationale) = reason {
242                 err.note(rationale.as_str());
243             }
244             err.span_note_once(span, "the lint level is defined here");
245             if lint_attr_name.as_str() != name {
246                 let level_str = level.as_str();
247                 err.note_once(&format!(
248                     "`#[{}({})]` implied by `#[{}({})]`",
249                     level_str, name, level_str, lint_attr_name
250                 ));
251             }
252         }
253     }
254 }
255
256 pub fn struct_lint_level<'s, 'd>(
257     sess: &'s Session,
258     lint: &'static Lint,
259     level: Level,
260     src: LintLevelSource,
261     span: Option<MultiSpan>,
262     decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a, ()>) + 'd,
263 ) {
264     // Avoid codegen bloat from monomorphization by immediately doing dyn dispatch of `decorate` to
265     // the "real" work.
266     fn struct_lint_level_impl<'s, 'd>(
267         sess: &'s Session,
268         lint: &'static Lint,
269         level: Level,
270         src: LintLevelSource,
271         span: Option<MultiSpan>,
272         decorate: Box<dyn for<'b> FnOnce(LintDiagnosticBuilder<'b, ()>) + 'd>,
273     ) {
274         // Check for future incompatibility lints and issue a stronger warning.
275         let future_incompatible = lint.future_incompatible;
276
277         let has_future_breakage = future_incompatible.map_or(
278             // Default allow lints trigger too often for testing.
279             sess.opts.unstable_opts.future_incompat_test && lint.default_level != Level::Allow,
280             |incompat| {
281                 matches!(incompat.reason, FutureIncompatibilityReason::FutureReleaseErrorReportNow)
282             },
283         );
284
285         let mut err = match (level, span) {
286             (Level::Allow, span) => {
287                 if has_future_breakage {
288                     if let Some(span) = span {
289                         sess.struct_span_allow(span, "")
290                     } else {
291                         sess.struct_allow("")
292                     }
293                 } else {
294                     return;
295                 }
296             }
297             (Level::Expect(expect_id), _) => {
298                 // This case is special as we actually allow the lint itself in this context, but
299                 // we can't return early like in the case for `Level::Allow` because we still
300                 // need the lint diagnostic to be emitted to `rustc_error::HandlerInner`.
301                 //
302                 // We can also not mark the lint expectation as fulfilled here right away, as it
303                 // can still be cancelled in the decorate function. All of this means that we simply
304                 // create a `DiagnosticBuilder` and continue as we would for warnings.
305                 sess.struct_expect("", expect_id)
306             }
307             (Level::ForceWarn(Some(expect_id)), Some(span)) => {
308                 sess.struct_span_warn_with_expectation(span, "", expect_id)
309             }
310             (Level::ForceWarn(Some(expect_id)), None) => {
311                 sess.struct_warn_with_expectation("", expect_id)
312             }
313             (Level::Warn | Level::ForceWarn(None), Some(span)) => sess.struct_span_warn(span, ""),
314             (Level::Warn | Level::ForceWarn(None), None) => sess.struct_warn(""),
315             (Level::Deny | Level::Forbid, Some(span)) => {
316                 let mut builder = sess.diagnostic().struct_err_lint("");
317                 builder.set_span(span);
318                 builder
319             }
320             (Level::Deny | Level::Forbid, None) => sess.diagnostic().struct_err_lint(""),
321         };
322
323         // If this code originates in a foreign macro, aka something that this crate
324         // did not itself author, then it's likely that there's nothing this crate
325         // can do about it. We probably want to skip the lint entirely.
326         if err.span.primary_spans().iter().any(|s| in_external_macro(sess, *s)) {
327             // Any suggestions made here are likely to be incorrect, so anything we
328             // emit shouldn't be automatically fixed by rustfix.
329             err.disable_suggestions();
330
331             // If this is a future incompatible that is not an edition fixing lint
332             // it'll become a hard error, so we have to emit *something*. Also,
333             // if this lint occurs in the expansion of a macro from an external crate,
334             // allow individual lints to opt-out from being reported.
335             let not_future_incompatible =
336                 future_incompatible.map(|f| f.reason.edition().is_some()).unwrap_or(true);
337             if not_future_incompatible && !lint.report_in_external_macro {
338                 err.cancel();
339                 // Don't continue further, since we don't want to have
340                 // `diag_span_note_once` called for a diagnostic that isn't emitted.
341                 return;
342             }
343         }
344
345         // Lint diagnostics that are covered by the expect level will not be emitted outside
346         // the compiler. It is therefore not necessary to add any information for the user.
347         // This will therefore directly call the decorate function which will in turn emit
348         // the `Diagnostic`.
349         if let Level::Expect(_) = level {
350             let name = lint.name_lower();
351             err.code(DiagnosticId::Lint { name, has_future_breakage, is_force_warn: false });
352             decorate(LintDiagnosticBuilder::new(err));
353             return;
354         }
355
356         explain_lint_level_source(lint, level, src, &mut err);
357
358         let name = lint.name_lower();
359         let is_force_warn = matches!(level, Level::ForceWarn(_));
360         err.code(DiagnosticId::Lint { name, has_future_breakage, is_force_warn });
361
362         if let Some(future_incompatible) = future_incompatible {
363             let explanation = match future_incompatible.reason {
364                 FutureIncompatibilityReason::FutureReleaseError
365                 | FutureIncompatibilityReason::FutureReleaseErrorReportNow => {
366                     "this was previously accepted by the compiler but is being phased out; \
367                          it will become a hard error in a future release!"
368                         .to_owned()
369                 }
370                 FutureIncompatibilityReason::FutureReleaseSemanticsChange => {
371                     "this will change its meaning in a future release!".to_owned()
372                 }
373                 FutureIncompatibilityReason::EditionError(edition) => {
374                     let current_edition = sess.edition();
375                     format!(
376                         "this is accepted in the current edition (Rust {}) but is a hard error in Rust {}!",
377                         current_edition, edition
378                     )
379                 }
380                 FutureIncompatibilityReason::EditionSemanticsChange(edition) => {
381                     format!("this changes meaning in Rust {}", edition)
382                 }
383                 FutureIncompatibilityReason::Custom(reason) => reason.to_owned(),
384             };
385
386             if future_incompatible.explain_reason {
387                 err.warn(&explanation);
388             }
389             if !future_incompatible.reference.is_empty() {
390                 let citation =
391                     format!("for more information, see {}", future_incompatible.reference);
392                 err.note(&citation);
393             }
394         }
395
396         // Finally, run `decorate`. This function is also responsible for emitting the diagnostic.
397         decorate(LintDiagnosticBuilder::new(err));
398     }
399     struct_lint_level_impl(sess, lint, level, src, span, Box::new(decorate))
400 }
401
402 /// Returns whether `span` originates in a foreign crate's external macro.
403 ///
404 /// This is used to test whether a lint should not even begin to figure out whether it should
405 /// be reported on the current node.
406 pub fn in_external_macro(sess: &Session, span: Span) -> bool {
407     let expn_data = span.ctxt().outer_expn_data();
408     match expn_data.kind {
409         ExpnKind::Inlined
410         | ExpnKind::Root
411         | ExpnKind::Desugaring(DesugaringKind::ForLoop | DesugaringKind::WhileLoop) => false,
412         ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
413         ExpnKind::Macro(MacroKind::Bang, _) => {
414             // Dummy span for the `def_site` means it's an external macro.
415             expn_data.def_site.is_dummy() || sess.source_map().is_imported(expn_data.def_site)
416         }
417         ExpnKind::Macro { .. } => true, // definitely a plugin
418     }
419 }