|
1 |
| -use rustc_errors::{DiagArgValue, LintEmitter}; |
| 1 | +use rustc_errors::{AttributeLintDecorator, DeferredAttributeLintDecorator, DiagArgValue}; |
2 | 2 | use rustc_hir::lints::{AttributeLint, AttributeLintKind};
|
3 | 3 | use rustc_hir::{HirId, Target};
|
| 4 | +use rustc_session::lint::Lint; |
4 | 5 | use rustc_span::sym;
|
5 | 6 |
|
6 | 7 | use crate::session_diagnostics;
|
7 | 8 |
|
8 |
| -pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<HirId>, lint_emitter: L) { |
9 |
| - let AttributeLint { id, span, kind } = lint; |
| 9 | +pub(crate) fn lint_name(kind: &AttributeLintKind) -> &'static Lint { |
| 10 | + use rustc_session::lint::builtin::*; |
| 11 | + match kind { |
| 12 | + AttributeLintKind::UnusedDuplicate { .. } => UNUSED_ATTRIBUTES, |
| 13 | + AttributeLintKind::IllFormedAttributeInput { .. } => ILL_FORMED_ATTRIBUTE_INPUT, |
| 14 | + AttributeLintKind::EmptyAttribute { .. } => UNUSED_ATTRIBUTES, |
| 15 | + AttributeLintKind::InvalidTarget { name, target, .. } => { |
| 16 | + // This check is here because `deprecated` had its own lint group and removing this would be a breaking change |
| 17 | + if *name == sym::deprecated |
| 18 | + && ![Target::Closure, Target::Expression, Target::Statement, Target::Arm] |
| 19 | + .contains(target) |
| 20 | + { |
| 21 | + rustc_session::lint::builtin::USELESS_DEPRECATED |
| 22 | + } else { |
| 23 | + rustc_session::lint::builtin::UNUSED_ATTRIBUTES |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
10 | 28 |
|
| 29 | +pub fn decorate_attribute_lint_kind<L: AttributeLintDecorator>( |
| 30 | + kind: &AttributeLintKind, |
| 31 | + lint_emitter: L, |
| 32 | +) { |
11 | 33 | match kind {
|
12 |
| - &AttributeLintKind::UnusedDuplicate { this, other, warning } => lint_emitter |
13 |
| - .emit_node_span_lint( |
14 |
| - rustc_session::lint::builtin::UNUSED_ATTRIBUTES, |
15 |
| - *id, |
16 |
| - *span, |
17 |
| - session_diagnostics::UnusedDuplicate { this, other, warning }, |
18 |
| - ), |
| 34 | + &AttributeLintKind::UnusedDuplicate { this, other, warning } => { |
| 35 | + lint_emitter.decorate(session_diagnostics::UnusedDuplicate { this, other, warning }) |
| 36 | + } |
19 | 37 | AttributeLintKind::IllFormedAttributeInput { suggestions } => {
|
20 |
| - lint_emitter.emit_node_span_lint( |
21 |
| - rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT, |
22 |
| - *id, |
23 |
| - *span, |
24 |
| - session_diagnostics::IllFormedAttributeInput { |
25 |
| - num_suggestions: suggestions.len(), |
26 |
| - suggestions: DiagArgValue::StrListSepByAnd( |
27 |
| - suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(), |
28 |
| - ), |
29 |
| - }, |
30 |
| - ); |
| 38 | + lint_emitter.decorate(session_diagnostics::IllFormedAttributeInput { |
| 39 | + num_suggestions: suggestions.len(), |
| 40 | + suggestions: DiagArgValue::StrListSepByAnd( |
| 41 | + suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(), |
| 42 | + ), |
| 43 | + }); |
31 | 44 | }
|
32 |
| - AttributeLintKind::EmptyAttribute { first_span } => lint_emitter.emit_node_span_lint( |
33 |
| - rustc_session::lint::builtin::UNUSED_ATTRIBUTES, |
34 |
| - *id, |
35 |
| - *first_span, |
36 |
| - session_diagnostics::EmptyAttributeList { attr_span: *first_span }, |
37 |
| - ), |
38 |
| - &AttributeLintKind::InvalidTarget { name, target, ref applied, only } => lint_emitter |
39 |
| - .emit_node_span_lint( |
40 |
| - // This check is here because `deprecated` had its own lint group and removing this would be a breaking change |
41 |
| - if name == sym::deprecated |
42 |
| - && ![Target::Closure, Target::Expression, Target::Statement, Target::Arm] |
43 |
| - .contains(&target) |
44 |
| - { |
45 |
| - rustc_session::lint::builtin::USELESS_DEPRECATED |
46 |
| - } else { |
47 |
| - rustc_session::lint::builtin::UNUSED_ATTRIBUTES |
48 |
| - }, |
49 |
| - *id, |
50 |
| - *span, |
51 |
| - session_diagnostics::InvalidTargetLint { |
52 |
| - name, |
53 |
| - target: target.plural_name(), |
54 |
| - applied: applied.clone(), |
55 |
| - only, |
56 |
| - attr_span: *span, |
57 |
| - }, |
58 |
| - ), |
| 45 | + |
| 46 | + AttributeLintKind::EmptyAttribute { first_span } => lint_emitter |
| 47 | + .decorate(session_diagnostics::EmptyAttributeList { attr_span: *first_span }), |
| 48 | + &AttributeLintKind::InvalidTarget { name, target, ref applied, only, span } => lint_emitter |
| 49 | + .decorate(session_diagnostics::InvalidTargetLint { |
| 50 | + name, |
| 51 | + target: target.plural_name(), |
| 52 | + applied: applied.clone(), |
| 53 | + only, |
| 54 | + attr_span: span, |
| 55 | + }), |
59 | 56 | }
|
60 | 57 | }
|
| 58 | + |
| 59 | +pub fn emit_attribute_lint<L: DeferredAttributeLintDecorator<ID = HirId>>( |
| 60 | + lint: &AttributeLint<HirId>, |
| 61 | + lint_emitter: L, |
| 62 | +) { |
| 63 | + let AttributeLint { id, span, kind } = lint; |
| 64 | + |
| 65 | + let lint_name = lint_name(&lint.kind); |
| 66 | + |
| 67 | + let emit = lint_emitter.prepare(lint_name, *id, *span); |
| 68 | + decorate_attribute_lint_kind(kind, emit); |
| 69 | +} |
0 commit comments