Skip to content

Commit 397dd46

Browse files
Remove duplication in target checking
1 parent 706f7be commit 397dd46

1 file changed

Lines changed: 23 additions & 42 deletions

File tree

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22

33
use rustc_ast::AttrStyle;
4-
use rustc_errors::{DiagArgValue, Diagnostic, MultiSpan, StashKey};
4+
use rustc_errors::{DiagArgValue, MultiSpan, StashKey};
55
use rustc_feature::Features;
66
use rustc_hir::attrs::AttributeKind;
77
use rustc_hir::{AttrItem, Attribute, MethodKind, Target};
@@ -121,15 +121,26 @@ impl<'sess> AttributeParser<'sess> {
121121
.emit();
122122
}
123123

124-
match allowed_targets.is_allowed(cx.target) {
125-
AllowedResult::Allowed => {}
126-
AllowedResult::Warn => {
127-
let allowed_targets = allowed_targets.allowed_targets();
128-
let (applied, only) =
129-
allowed_targets_applied(allowed_targets, cx.target, cx.features);
130-
let name = cx.attr_path.clone();
124+
let result = allowed_targets.is_allowed(cx.target);
125+
if matches!(result, AllowedResult::Allowed) {
126+
return;
127+
}
131128

132-
let lint = if name.segments[0] == sym::deprecated
129+
let allowed_targets = allowed_targets.allowed_targets();
130+
let (applied, only) = allowed_targets_applied(allowed_targets, cx.target, cx.features);
131+
let diag = InvalidTarget {
132+
span: cx.attr_span.clone(),
133+
name: cx.attr_path.clone(),
134+
target: cx.target.plural_name(),
135+
only: if only { "only " } else { "" },
136+
applied: DiagArgValue::StrListSepByAnd(applied.into_iter().map(Cow::Owned).collect()),
137+
previously_accepted: matches!(result, AllowedResult::Warn),
138+
};
139+
140+
match result {
141+
AllowedResult::Allowed => unreachable!("Should have early returned above"),
142+
AllowedResult::Warn => {
143+
let lint = if cx.attr_path.segments[0] == sym::deprecated
133144
&& ![
134145
Target::Closure,
135146
Target::Expression,
@@ -144,41 +155,11 @@ impl<'sess> AttributeParser<'sess> {
144155
rustc_session::lint::builtin::UNUSED_ATTRIBUTES
145156
};
146157

147-
let attr_span = cx.attr_span;
148-
let target = cx.target;
149-
cx.emit_lint_with_sess(
150-
lint,
151-
move |dcx, level, _| {
152-
InvalidTarget {
153-
name,
154-
target: target.plural_name(),
155-
only: if only { "only " } else { "" },
156-
applied: DiagArgValue::StrListSepByAnd(
157-
applied.iter().map(|i| Cow::Owned(i.to_string())).collect(),
158-
),
159-
span: attr_span,
160-
previously_accepted: true,
161-
}
162-
.into_diag(dcx, level)
163-
},
164-
attr_span,
165-
);
158+
let attr_span = cx.attr_span.clone();
159+
cx.emit_lint(lint, diag, attr_span);
166160
}
167161
AllowedResult::Error => {
168-
let allowed_targets = allowed_targets.allowed_targets();
169-
let (applied, only) =
170-
allowed_targets_applied(allowed_targets, cx.target, cx.features);
171-
let name = cx.attr_path.clone();
172-
cx.dcx().emit_err(InvalidTarget {
173-
span: cx.attr_span.clone(),
174-
name,
175-
target: cx.target.plural_name(),
176-
only: if only { "only " } else { "" },
177-
applied: DiagArgValue::StrListSepByAnd(
178-
applied.into_iter().map(Cow::Owned).collect(),
179-
),
180-
previously_accepted: false,
181-
});
162+
cx.dcx().emit_err(diag);
182163
}
183164
}
184165
}

0 commit comments

Comments
 (0)