Skip to content

Commit 1518fcc

Browse files
Move misleading_cfg_in_build_script directly into attribute parsing
1 parent d6ed6a9 commit 1518fcc

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ pub fn parse_cfg_entry<S: Stage>(
8888
cx: &mut AcceptContext<'_, '_, S>,
8989
item: &MetaItemOrLitParser,
9090
) -> Result<CfgEntry, ErrorGuaranteed> {
91+
let entry = parse_cfg_entry_inner(cx, item)?;
92+
let mut spans = Vec::new();
93+
let total = misleading_cfgs(&entry, &mut spans);
94+
if !spans.is_empty() {
95+
cx.emit_lint(
96+
UNEXPECTED_CFGS,
97+
AttributeLintKind::UnexpectedCfgName((name, name_span), value),
98+
span,
99+
);
100+
}
101+
Ok(entry)
102+
}
103+
104+
fn parse_cfg_entry_inner<S: Stage>(
105+
cx: &mut AcceptContext<'_, '_, S>,
106+
item: &MetaItemOrLitParser,
107+
) -> Result<CfgEntry, ErrorGuaranteed> {
108+
91109
Ok(match item {
92110
MetaItemOrLitParser::MetaItemParser(meta) => match meta.args() {
93111
ArgParser::List(list) => match meta.path().word_sym() {

compiler/rustc_lint/src/misleading_cfg_in_build_script.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ const ERROR_MESSAGE: &str = "target-based cfg should be avoided in build scripts
203203

204204
impl EarlyLintPass for MisleadingCfgInBuildScript {
205205
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
206-
let mut spans = Vec::new();
207-
let mut has_unknown = false;
208206
match attr.name() {
209207
Some(sym::cfg) if let Some(meta) = attr.meta() => {
210208
get_invalid_cfg_attrs(&meta, &mut spans, &mut has_unknown);

compiler/rustc_session/src/parse.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_feature::{GateIssue, UnstableFeatures, find_feature_issue};
1818
use rustc_span::edition::Edition;
1919
use rustc_span::hygiene::ExpnId;
2020
use rustc_span::source_map::{FilePathMapping, SourceMap};
21-
use rustc_span::{Span, Symbol, sym};
21+
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
2222

2323
use crate::Session;
2424
use crate::config::{Cfg, CheckCfg};
@@ -379,3 +379,30 @@ impl ParseSess {
379379
self.dcx.handle()
380380
}
381381
}
382+
383+
pub fn parse_cfg(parser: &mut rustc_parse::Parser<'_>, sess: &rustc_session::Session<'_>, span: Span, tts: TokenStream) -> Result<CfgEntry, ErrorGuaranteed> {
384+
let meta = MetaItemOrLitParser::parse_single(
385+
&mut parser,
386+
ShouldEmit::Nothing,
387+
AllowExprMetavar::Yes,
388+
)
389+
.map_err(|_| diag.emit())?;
390+
AttributeParser::parse_single_args(
391+
sess,
392+
span,
393+
span,
394+
AttrStyle::Inner,
395+
AttrPath { segments: vec![sym::cfg].into_boxed_slice(), span },
396+
None,
397+
ParsedDescription::Macro,
398+
span,
399+
cx.current_expansion.lint_node_id,
400+
// Doesn't matter what the target actually is here.
401+
Target::Crate,
402+
Some(cx.ecfg.features),
403+
ShouldEmit::Nothing,
404+
&meta,
405+
parse_cfg_entry,
406+
&CFG_TEMPLATE,
407+
)
408+
}

0 commit comments

Comments
 (0)