rustc_middle/
lint.rs

1use std::cmp;
2
3use rustc_data_structures::fx::FxIndexMap;
4use rustc_data_structures::sorted_map::SortedMap;
5use rustc_errors::{Diag, MultiSpan};
6use rustc_hir::{HirId, ItemLocalId};
7use rustc_lint_defs::EditionFcw;
8use rustc_macros::{Decodable, Encodable, HashStable};
9use rustc_session::Session;
10use rustc_session::lint::builtin::{self, FORBIDDEN_LINT_GROUPS};
11use rustc_session::lint::{FutureIncompatibilityReason, Level, Lint, LintExpectationId, LintId};
12use rustc_span::{DUMMY_SP, Span, Symbol, kw};
13use tracing::instrument;
14
15use crate::ty::TyCtxt;
16
17/// How a lint level was set.
18#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, HashStable, Debug)]
19pub enum LintLevelSource {
20    /// Lint is at the default level as declared in rustc.
21    Default,
22
23    /// Lint level was set by an attribute.
24    Node {
25        name: Symbol,
26        span: Span,
27        /// RFC 2383 reason
28        reason: Option<Symbol>,
29    },
30
31    /// Lint level was set by a command-line flag.
32    /// The provided `Level` is the level specified on the command line.
33    /// (The actual level may be lower due to `--cap-lints`.)
34    CommandLine(Symbol, Level),
35}
36
37impl LintLevelSource {
38    pub fn name(&self) -> Symbol {
39        match *self {
40            LintLevelSource::Default => kw::Default,
41            LintLevelSource::Node { name, .. } => name,
42            LintLevelSource::CommandLine(name, _) => name,
43        }
44    }
45
46    pub fn span(&self) -> Span {
47        match *self {
48            LintLevelSource::Default => DUMMY_SP,
49            LintLevelSource::Node { span, .. } => span,
50            LintLevelSource::CommandLine(_, _) => DUMMY_SP,
51        }
52    }
53}
54
55/// Convenience helper for moving things around together that frequently are paired
56#[derive(Copy, Clone, Debug, HashStable, Encodable, Decodable)]
57pub struct LevelAndSource {
58    pub level: Level,
59    pub lint_id: Option<LintExpectationId>,
60    pub src: LintLevelSource,
61}
62
63/// Return type for the `shallow_lint_levels_on` query.
64///
65/// This map represents the set of allowed lints and allowance levels given
66/// by the attributes for *a single HirId*.
67#[derive(Default, Debug, HashStable)]
68pub struct ShallowLintLevelMap {
69    pub expectations: Vec<(LintExpectationId, LintExpectation)>,
70    pub specs: SortedMap<ItemLocalId, FxIndexMap<LintId, LevelAndSource>>,
71}
72
73/// From an initial level and source, verify the effect of special annotations:
74/// `warnings` lint level and lint caps.
75///
76/// The return of this function is suitable for diagnostics.
77pub fn reveal_actual_level(
78    level: Option<(Level, Option<LintExpectationId>)>,
79    src: &mut LintLevelSource,
80    sess: &Session,
81    lint: LintId,
82    probe_for_lint_level: impl FnOnce(
83        LintId,
84    )
85        -> (Option<(Level, Option<LintExpectationId>)>, LintLevelSource),
86) -> (Level, Option<LintExpectationId>) {
87    // If `level` is none then we actually assume the default level for this lint.
88    let (mut level, mut lint_id) =
89        level.unwrap_or_else(|| (lint.lint.default_level(sess.edition()), None));
90
91    // If we're about to issue a warning, check at the last minute for any
92    // directives against the warnings "lint". If, for example, there's an
93    // `allow(warnings)` in scope then we want to respect that instead.
94    //
95    // We exempt `FORBIDDEN_LINT_GROUPS` from this because it specifically
96    // triggers in cases (like #80988) where you have `forbid(warnings)`,
97    // and so if we turned that into an error, it'd defeat the purpose of the
98    // future compatibility warning.
99    if level == Level::Warn && lint != LintId::of(FORBIDDEN_LINT_GROUPS) {
100        let (warnings_level, warnings_src) = probe_for_lint_level(LintId::of(builtin::WARNINGS));
101        if let Some((configured_warning_level, configured_lint_id)) = warnings_level {
102            if configured_warning_level != Level::Warn {
103                level = configured_warning_level;
104                lint_id = configured_lint_id;
105                *src = warnings_src;
106            }
107        }
108    }
109
110    // Ensure that we never exceed the `--cap-lints` argument unless the source is a --force-warn
111    level = if let LintLevelSource::CommandLine(_, Level::ForceWarn) = src {
112        level
113    } else {
114        cmp::min(level, sess.opts.lint_cap.unwrap_or(Level::Forbid))
115    };
116
117    if let Some(driver_level) = sess.driver_lint_caps.get(&lint) {
118        // Ensure that we never exceed driver level.
119        level = cmp::min(*driver_level, level);
120    }
121
122    (level, lint_id)
123}
124
125impl ShallowLintLevelMap {
126    /// Perform a deep probe in the HIR tree looking for the actual level for the lint.
127    /// This lint level is not usable for diagnostics, it needs to be corrected by
128    /// `reveal_actual_level` beforehand.
129    #[instrument(level = "trace", skip(self, tcx), ret)]
130    fn probe_for_lint_level(
131        &self,
132        tcx: TyCtxt<'_>,
133        id: LintId,
134        start: HirId,
135    ) -> (Option<(Level, Option<LintExpectationId>)>, LintLevelSource) {
136        if let Some(map) = self.specs.get(&start.local_id)
137            && let Some(&LevelAndSource { level, lint_id, src }) = map.get(&id)
138        {
139            return (Some((level, lint_id)), src);
140        }
141
142        let mut owner = start.owner;
143        let mut specs = &self.specs;
144
145        for parent in tcx.hir_parent_id_iter(start) {
146            if parent.owner != owner {
147                owner = parent.owner;
148                specs = &tcx.shallow_lint_levels_on(owner).specs;
149            }
150            if let Some(map) = specs.get(&parent.local_id)
151                && let Some(&LevelAndSource { level, lint_id, src }) = map.get(&id)
152            {
153                return (Some((level, lint_id)), src);
154            }
155        }
156
157        (None, LintLevelSource::Default)
158    }
159
160    /// Fetch and return the user-visible lint level for the given lint at the given HirId.
161    #[instrument(level = "trace", skip(self, tcx), ret)]
162    pub fn lint_level_id_at_node(
163        &self,
164        tcx: TyCtxt<'_>,
165        lint: LintId,
166        cur: HirId,
167    ) -> LevelAndSource {
168        let (level, mut src) = self.probe_for_lint_level(tcx, lint, cur);
169        let (level, lint_id) = reveal_actual_level(level, &mut src, tcx.sess, lint, |lint| {
170            self.probe_for_lint_level(tcx, lint, cur)
171        });
172        LevelAndSource { level, lint_id, src }
173    }
174}
175
176impl TyCtxt<'_> {
177    /// Fetch and return the user-visible lint level for the given lint at the given HirId.
178    pub fn lint_level_at_node(self, lint: &'static Lint, id: HirId) -> LevelAndSource {
179        self.shallow_lint_levels_on(id.owner).lint_level_id_at_node(self, LintId::of(lint), id)
180    }
181}
182
183/// This struct represents a lint expectation and holds all required information
184/// to emit the `unfulfilled_lint_expectations` lint if it is unfulfilled after
185/// the `LateLintPass` has completed.
186#[derive(Clone, Debug, Encodable, Decodable, HashStable)]
187pub struct LintExpectation {
188    /// The reason for this expectation that can optionally be added as part of
189    /// the attribute. It will be displayed as part of the lint message.
190    pub reason: Option<Symbol>,
191    /// The [`Span`] of the attribute that this expectation originated from.
192    pub emission_span: Span,
193    /// Lint messages for the `unfulfilled_lint_expectations` lint will be
194    /// adjusted to include an additional note. Therefore, we have to track if
195    /// the expectation is for the lint.
196    pub is_unfulfilled_lint_expectations: bool,
197    /// This will hold the name of the tool that this lint belongs to. For
198    /// the lint `clippy::some_lint` the tool would be `clippy`, the same
199    /// goes for `rustdoc`. This will be `None` for rustc lints
200    pub lint_tool: Option<Symbol>,
201}
202
203impl LintExpectation {
204    pub fn new(
205        reason: Option<Symbol>,
206        emission_span: Span,
207        is_unfulfilled_lint_expectations: bool,
208        lint_tool: Option<Symbol>,
209    ) -> Self {
210        Self { reason, emission_span, is_unfulfilled_lint_expectations, lint_tool }
211    }
212}
213
214fn explain_lint_level_source(
215    sess: &Session,
216    lint: &'static Lint,
217    level: Level,
218    src: LintLevelSource,
219    err: &mut Diag<'_, ()>,
220) {
221    // Find the name of the lint group that contains the given lint.
222    // Assumes the lint only belongs to one group.
223    let lint_group_name = |lint| {
224        let lint_groups_iter = sess.lint_groups_iter();
225        let lint_id = LintId::of(lint);
226        lint_groups_iter
227            .filter(|lint_group| !lint_group.is_externally_loaded)
228            .find(|lint_group| {
229                lint_group
230                    .lints
231                    .iter()
232                    .find(|lint_group_lint| **lint_group_lint == lint_id)
233                    .is_some()
234            })
235            .map(|lint_group| lint_group.name)
236    };
237    let name = lint.name_lower();
238    if let Level::Allow = level {
239        // Do not point at `#[allow(compat_lint)]` as the reason for a compatibility lint
240        // triggering. (#121009)
241        return;
242    }
243    match src {
244        LintLevelSource::Default => {
245            let level_str = level.as_str();
246            match lint_group_name(lint) {
247                Some(group_name) => {
248                    err.note_once(format!("`#[{level_str}({name})]` (part of `#[{level_str}({group_name})]`) on by default"));
249                }
250                None => {
251                    err.note_once(format!("`#[{level_str}({name})]` on by default"));
252                }
253            }
254        }
255        LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
256            let flag = orig_level.to_cmd_flag();
257            let hyphen_case_lint_name = name.replace('_', "-");
258            if lint_flag_val.as_str() == name {
259                err.note_once(format!(
260                    "requested on the command line with `{flag} {hyphen_case_lint_name}`"
261                ));
262            } else {
263                let hyphen_case_flag_val = lint_flag_val.as_str().replace('_', "-");
264                err.note_once(format!(
265                    "`{flag} {hyphen_case_lint_name}` implied by `{flag} {hyphen_case_flag_val}`"
266                ));
267                if matches!(orig_level, Level::Warn | Level::Deny) {
268                    err.help_once(format!(
269                        "to override `{flag} {hyphen_case_flag_val}` add `#[allow({name})]`"
270                    ));
271                }
272            }
273        }
274        LintLevelSource::Node { name: lint_attr_name, span, reason, .. } => {
275            if let Some(rationale) = reason {
276                err.note(rationale.to_string());
277            }
278            err.span_note_once(span, "the lint level is defined here");
279            if lint_attr_name.as_str() != name {
280                let level_str = level.as_str();
281                err.note_once(format!(
282                    "`#[{level_str}({name})]` implied by `#[{level_str}({lint_attr_name})]`"
283                ));
284            }
285        }
286    }
287}
288
289/// The innermost function for emitting lints.
290///
291/// If you are looking to implement a lint, look for higher level functions,
292/// for example:
293/// - [`TyCtxt::emit_node_span_lint`]
294/// - [`TyCtxt::node_span_lint`]
295/// - [`TyCtxt::emit_node_lint`]
296/// - [`TyCtxt::node_lint`]
297/// - `LintContext::opt_span_lint`
298///
299/// ## `decorate`
300///
301/// It is not intended to call `emit`/`cancel` on the `Diag` passed in the `decorate` callback.
302#[track_caller]
303pub fn lint_level(
304    sess: &Session,
305    lint: &'static Lint,
306    level: LevelAndSource,
307    span: Option<MultiSpan>,
308    decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
309) {
310    // Avoid codegen bloat from monomorphization by immediately doing dyn dispatch of `decorate` to
311    // the "real" work.
312    #[track_caller]
313    fn lint_level_impl(
314        sess: &Session,
315        lint: &'static Lint,
316        level: LevelAndSource,
317        span: Option<MultiSpan>,
318        decorate: Box<dyn '_ + for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>)>,
319    ) {
320        let LevelAndSource { level, lint_id, src } = level;
321
322        // Check for future incompatibility lints and issue a stronger warning.
323        let future_incompatible = lint.future_incompatible;
324
325        let has_future_breakage = future_incompatible.map_or(
326            // Default allow lints trigger too often for testing.
327            sess.opts.unstable_opts.future_incompat_test && lint.default_level != Level::Allow,
328            |incompat| incompat.report_in_deps,
329        );
330
331        // Convert lint level to error level.
332        let err_level = match level {
333            Level::Allow => {
334                if has_future_breakage {
335                    rustc_errors::Level::Allow
336                } else {
337                    return;
338                }
339            }
340            Level::Expect => {
341                // This case is special as we actually allow the lint itself in this context, but
342                // we can't return early like in the case for `Level::Allow` because we still
343                // need the lint diagnostic to be emitted to `rustc_error::DiagCtxtInner`.
344                //
345                // We can also not mark the lint expectation as fulfilled here right away, as it
346                // can still be cancelled in the decorate function. All of this means that we simply
347                // create a `Diag` and continue as we would for warnings.
348                rustc_errors::Level::Expect
349            }
350            Level::ForceWarn => rustc_errors::Level::ForceWarning,
351            Level::Warn => rustc_errors::Level::Warning,
352            Level::Deny | Level::Forbid => rustc_errors::Level::Error,
353        };
354        let mut err = Diag::new(sess.dcx(), err_level, "");
355        if let Some(span) = span {
356            err.span(span);
357        }
358        if let Some(lint_id) = lint_id {
359            err.lint_id(lint_id);
360        }
361
362        // If this code originates in a foreign macro, aka something that this crate
363        // did not itself author, then it's likely that there's nothing this crate
364        // can do about it. We probably want to skip the lint entirely.
365        if err.span.primary_spans().iter().any(|s| s.in_external_macro(sess.source_map())) {
366            // Any suggestions made here are likely to be incorrect, so anything we
367            // emit shouldn't be automatically fixed by rustfix.
368            err.disable_suggestions();
369
370            // If this is a future incompatible that is not an edition fixing lint
371            // it'll become a hard error, so we have to emit *something*. Also,
372            // if this lint occurs in the expansion of a macro from an external crate,
373            // allow individual lints to opt-out from being reported.
374            let incompatible = future_incompatible.is_some_and(|f| f.reason.edition().is_none());
375
376            if !incompatible && !lint.report_in_external_macro {
377                err.cancel();
378
379                // Don't continue further, since we don't want to have
380                // `diag_span_note_once` called for a diagnostic that isn't emitted.
381                return;
382            }
383        }
384
385        err.is_lint(lint.name_lower(), has_future_breakage);
386
387        // Lint diagnostics that are covered by the expect level will not be emitted outside
388        // the compiler. It is therefore not necessary to add any information for the user.
389        // This will therefore directly call the decorate function which will in turn emit
390        // the diagnostic.
391        if let Level::Expect = level {
392            decorate(&mut err);
393            err.emit();
394            return;
395        }
396
397        if let Some(future_incompatible) = future_incompatible {
398            let explanation = match future_incompatible.reason {
399                FutureIncompatibilityReason::FutureReleaseError(_) => {
400                    "this was previously accepted by the compiler but is being phased out; \
401                         it will become a hard error in a future release!"
402                        .to_owned()
403                }
404                FutureIncompatibilityReason::FutureReleaseSemanticsChange(_) => {
405                    "this will change its meaning in a future release!".to_owned()
406                }
407                FutureIncompatibilityReason::EditionError(EditionFcw { edition, .. }) => {
408                    let current_edition = sess.edition();
409                    format!(
410                        "this is accepted in the current edition (Rust {current_edition}) but is a hard error in Rust {edition}!"
411                    )
412                }
413                FutureIncompatibilityReason::EditionSemanticsChange(EditionFcw {
414                    edition, ..
415                }) => {
416                    format!("this changes meaning in Rust {edition}")
417                }
418                FutureIncompatibilityReason::EditionAndFutureReleaseError(EditionFcw {
419                    edition,
420                    ..
421                }) => {
422                    format!(
423                        "this was previously accepted by the compiler but is being phased out; \
424                         it will become a hard error in Rust {edition} and in a future release in all editions!"
425                    )
426                }
427                FutureIncompatibilityReason::EditionAndFutureReleaseSemanticsChange(
428                    EditionFcw { edition, .. },
429                ) => {
430                    format!(
431                        "this changes meaning in Rust {edition} and in a future release in all editions!"
432                    )
433                }
434                FutureIncompatibilityReason::Custom(reason, _) => reason.to_owned(),
435                FutureIncompatibilityReason::Unreachable => unreachable!(),
436            };
437
438            if future_incompatible.explain_reason {
439                err.warn(explanation);
440            }
441
442            let citation =
443                format!("for more information, see {}", future_incompatible.reason.reference());
444            err.note(citation);
445        }
446
447        // Finally, run `decorate`. `decorate` can call `trimmed_path_str` (directly or indirectly),
448        // so we need to make sure when we do call `decorate` that the diagnostic is eventually
449        // emitted or we'll get a `must_produce_diag` ICE.
450        //
451        // When is a diagnostic *eventually* emitted? Well, that is determined by 2 factors:
452        // 1. If the corresponding `rustc_errors::Level` is beyond warning, i.e. `ForceWarning(_)`
453        //    or `Error`, then the diagnostic will be emitted regardless of CLI options.
454        // 2. If the corresponding `rustc_errors::Level` is warning, then that can be affected by
455        //    `-A warnings` or `--cap-lints=xxx` on the command line. In which case, the diagnostic
456        //    will be emitted if `can_emit_warnings` is true.
457        let skip = err_level == rustc_errors::Level::Warning && !sess.dcx().can_emit_warnings();
458
459        if !skip {
460            decorate(&mut err);
461        }
462
463        explain_lint_level_source(sess, lint, level, src, &mut err);
464        err.emit()
465    }
466    lint_level_impl(sess, lint, level, span, Box::new(decorate))
467}