Repository: rust-lang/rust
File: compiler/rustc_middle/src/lint.rs (Rust)

Code before:
            Level::Deny | Level::Forbid => rustc_errors::Level::Error,
        };
        // Finally, run `decorate`. `decorate` can call `trimmed_path_str` (directly or indirectly),
        // so we need to make sure when we do call `decorate` that the diagnostic is eventually
        // emitted or we'll get a `must_produce_diag` ICE.
        //
        // When is a diagnostic *eventually* emitted? Well, that is determined by 2 factors:
        // 1. If the corresponding `rustc_errors::Level` is beyond warning, i.e. `ForceWarning(_)`
        //    or `Error`, then the diagnostic will be emitted regardless of CLI options.
        // 2. If the corresponding `rustc_errors::Level` is warning, then that can be affected by
        //    `-A warnings` or `--cap-lints=xxx` on the command line. In which case, the diagnostic
        //    will be emitted if `can_emit_warnings` is true.
        let skip = err_level == rustc_errors::Level::Warning && !sess.dcx().can_emit_warnings();

        let disable_suggestions = if let Some(ref span) = span
            // If this code originates in a foreign macro, aka something that this crate
            // did not itself author, then it's likely that there's nothing this crate
            // can do about it. We probably want to skip the lint entirely.
            && span.primary_spans().iter().any(|s| s.in_external_macro(sess.source_map()))
        {
            true
        } else {
            false
        };

        let mut err: Diag<'_, ()> = if !skip {
            decorate(sess.dcx(), err_level)
        } else {
            Diag::new(sess.dcx(), err_level, "")
        };
        if let Some(span) = span
            && err.span.primary_span().is_none()
        {
            // We can't use `err.span()` because it overwrites the labels, so we need to do it manually.
            for primary in span.primary_spans() {
                err.span.push_primary_span(*primary);
            }
            for (label_span, label) in span.span_labels_raw() {
                err.span.push_span_diag(*label_span, label.clone());
            }
        }
        if let Some(lint_id) = lint_id {
            err.lint_id(lint_id);
        }

        if disable_suggestions {
            // Any suggestions made here are likely to be incorrect, so anything we
            // emit shouldn't be automatically fixed by rustfix.
            err.disable_suggestions();

            // If this is a future incompatible that is not an edition fixing lint

Review comment (bug):
Why was this code moved?
The move doesn't look incorrect and is probably nice for performance, just wondering if there is a concrete reason

Code after:
            Level::Deny | Level::Forbid => rustc_errors::Level::Error,
        };
        // Finally, run `decorate`. `decorate` can call `trimmed_path_str` (directly or indirectly),
        // so we need to make sure when we do call `decorate` that the diagnostic is eventually
        // emitted or we'll get a `must_produce_diag` ICE.
        //
        // When is a diagnostic *eventually* emitted? Well, that is determined by 2 factors:
        // 1. If the corresponding `rustc_errors::Level` is beyond warning, i.e. `ForceWarning(_)`
        //    or `Error`, then the diagnostic will be emitted regardless of CLI options.
        // 2. If the corresponding `rustc_errors::Level` is warning, then that can be affected by
        //    `-A warnings` or `--cap-lints=xxx` on the command line. In which case, the diagnostic
        //    will be emitted if `can_emit_warnings` is true.
        let skip = err_level == rustc_errors::Level::Warning && !sess.dcx().can_emit_warnings();

        let disable_suggestions = if let Some(ref span) = span
            // If this code originates in a foreign macro, aka something that this crate
            // did not itself author, then it's likely that there's nothing this crate
            // can do about it. We probably want to skip the lint entirely.
            && span.primary_spans().iter().any(|s| s.in_external_macro(sess.source_map()))
        {
            true
        } else {
            false
        };

        if disable_suggestions {
            // If this is a future incompatible that is not an edition fixing lint
            // it'll become a hard error, so we have to emit *something*. Also,
            // if this lint occurs in the expansion of a macro from an external crate,
            // allow individual lints to opt-out from being reported.
            let incompatible = future_incompatible.is_some_and(|f| f.reason.edition().is_none());

            if !incompatible && !lint.report_in_external_macro {
                // Don't continue further, since we don't want to have
                // `diag_span_note_once` called for a diagnostic that isn't emitted.
                return;
            }
        }

        let mut err: Diag<'_, ()> = if !skip {
            decorate(sess.dcx(), err_level)
        } else {
            Diag::new(sess.dcx(), err_level, "")
        };
        if let Some(span) = span
            && err.span.primary_span().is_none()
        {
            // We can't use `err.span()` because it overwrites the labels, so we need to do it manually.
            for primary in span.primary_spans() {
                err.span.push_primary_span(*primary);
            }