3 use rustc_data_structures::fx::FxHashMap;
4 use rustc_errors::{Diagnostic, DiagnosticId, LintDiagnosticBuilder, MultiSpan};
6 use rustc_session::lint::{
7 builtin::{self, FORBIDDEN_LINT_GROUPS},
8 FutureIncompatibilityReason, Level, Lint, LintId,
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};
15 use crate::ty::TyCtxt;
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.
24 /// Lint level was set by an attribute.
29 reason: Option<Symbol>,
30 /// The lint tool. (e.g. rustdoc, clippy)
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),
40 impl LintLevelSource {
41 pub fn name(&self) -> Symbol {
43 LintLevelSource::Default => symbol::kw::Default,
44 LintLevelSource::Node { name, .. } => name,
45 LintLevelSource::CommandLine(name, _) => name,
49 pub fn span(&self) -> Span {
51 LintLevelSource::Default => DUMMY_SP,
52 LintLevelSource::Node { span, .. } => span,
53 LintLevelSource::CommandLine(_, _) => DUMMY_SP,
58 /// A tuple of a lint level and its source.
59 pub type LevelAndSource = (Level, LintLevelSource);
61 /// Return type for the `shallow_lint_levels_on` query.
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>,
70 /// From an initial level and source, verify the effect of special annotations:
71 /// `warnings` lint level and lint caps.
73 /// The return of this function is suitable for diagnostics.
74 pub fn reveal_actual_level(
76 src: &mut LintLevelSource,
79 probe_for_lint_level: impl FnOnce(LintId) -> (Option<Level>, LintLevelSource),
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()));
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.
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;
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 {
106 cmp::min(level, sess.opts.lint_cap.unwrap_or(Level::Forbid))
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);
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(
126 ) -> (Option<Level>, LintLevelSource) {
127 if let Some(&(level, src)) = self.specs.get(&id) {
128 return (Some(level), src);
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);
137 (None, LintLevelSource::Default)
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(
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)
151 debug!(?id, ?level, ?src);
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)
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)
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>,
193 impl LintExpectation {
195 reason: Option<Symbol>,
197 is_unfulfilled_lint_expectations: bool,
198 lint_tool: Option<Symbol>,
200 Self { reason, emission_span, is_unfulfilled_lint_expectations, lint_tool }
204 pub fn explain_lint_level_source(
207 src: LintLevelSource,
208 err: &mut Diagnostic,
210 let name = lint.name_lower();
212 LintLevelSource::Default => {
213 err.note_once(&format!("`#[{}({})]` on by default", level.as_str(), name));
215 LintLevelSource::CommandLine(lint_flag_val, orig_level) => {
216 let flag = match orig_level {
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")
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
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
240 LintLevelSource::Node { name: lint_attr_name, span, reason, .. } => {
241 if let Some(rationale) = reason {
242 err.note(rationale.as_str());
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
256 pub fn struct_lint_level<'s, 'd>(
260 src: LintLevelSource,
261 span: Option<MultiSpan>,
262 decorate: impl for<'a> FnOnce(LintDiagnosticBuilder<'a, ()>) + 'd,
264 // Avoid codegen bloat from monomorphization by immediately doing dyn dispatch of `decorate` to
266 fn struct_lint_level_impl<'s, 'd>(
270 src: LintLevelSource,
271 span: Option<MultiSpan>,
272 decorate: Box<dyn for<'b> FnOnce(LintDiagnosticBuilder<'b, ()>) + 'd>,
274 // Check for future incompatibility lints and issue a stronger warning.
275 let future_incompatible = lint.future_incompatible;
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,
281 matches!(incompat.reason, FutureIncompatibilityReason::FutureReleaseErrorReportNow)
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, "")
291 sess.struct_allow("")
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`.
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)
307 (Level::ForceWarn(Some(expect_id)), Some(span)) => {
308 sess.struct_span_warn_with_expectation(span, "", expect_id)
310 (Level::ForceWarn(Some(expect_id)), None) => {
311 sess.struct_warn_with_expectation("", expect_id)
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);
320 (Level::Deny | Level::Forbid, None) => sess.diagnostic().struct_err_lint(""),
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();
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 {
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.
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
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));
356 explain_lint_level_source(lint, level, src, &mut err);
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 });
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!"
370 FutureIncompatibilityReason::FutureReleaseSemanticsChange => {
371 "this will change its meaning in a future release!".to_owned()
373 FutureIncompatibilityReason::EditionError(edition) => {
374 let current_edition = sess.edition();
376 "this is accepted in the current edition (Rust {}) but is a hard error in Rust {}!",
377 current_edition, edition
380 FutureIncompatibilityReason::EditionSemanticsChange(edition) => {
381 format!("this changes meaning in Rust {}", edition)
383 FutureIncompatibilityReason::Custom(reason) => reason.to_owned(),
386 if future_incompatible.explain_reason {
387 err.warn(&explanation);
389 if !future_incompatible.reference.is_empty() {
391 format!("for more information, see {}", future_incompatible.reference);
396 // Finally, run `decorate`. This function is also responsible for emitting the diagnostic.
397 decorate(LintDiagnosticBuilder::new(err));
399 struct_lint_level_impl(sess, lint, level, src, span, Box::new(decorate))
402 /// Returns whether `span` originates in a foreign crate's external macro.
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 {
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)
417 ExpnKind::Macro { .. } => true, // definitely a plugin