Skip to content

Commit a129548

Browse files
committed
Unify macro name detection for cfg_{if|matach}!
Specifically, both cfg_if! and cfg_match! visitors now gate using the same is_macro_name predicate, which checks the last path segment ident. cfg_if! previously tested the *first* path segment, meaning that the visitor would walk `cfg_if!` and `cfg_if::cfg_if!`, but would also walk `cfg_if::other!` and wouldn't walk `::cfg_if::cfg_if!`. cfg_match! instead chose to check the *last* path segment, so its visitor would walk any macro invoked as `cfg_match!` independent of the leading path.
1 parent 924b834 commit a129548

File tree

2 files changed

+17
-58
lines changed

2 files changed

+17
-58
lines changed

src/modules.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -590,30 +590,23 @@ fn find_path_value(attrs: &[ast::Attribute]) -> Option<Symbol> {
590590
attrs.iter().flat_map(path_value).next()
591591
}
592592

593+
fn is_macro_name(mac: &ast::MacCall, name: &str) -> bool {
594+
mac.path
595+
.segments
596+
.last()
597+
.map_or(false, |segment| segment.ident.name == Symbol::intern(name))
598+
}
599+
593600
fn is_cfg_if(item: &ast::Item) -> bool {
594601
match item.kind {
595-
ast::ItemKind::MacCall(ref mac) => {
596-
if let Some(first_segment) = mac.path.segments.first() {
597-
if first_segment.ident.name == Symbol::intern("cfg_if") {
598-
return true;
599-
}
600-
}
601-
false
602-
}
602+
ast::ItemKind::MacCall(ref mac) => is_macro_name(mac, "cfg_if"),
603603
_ => false,
604604
}
605605
}
606606

607607
fn is_cfg_match(item: &ast::Item) -> bool {
608608
match item.kind {
609-
ast::ItemKind::MacCall(ref mac) => {
610-
if let Some(last_segment) = mac.path.segments.last() {
611-
if last_segment.ident.name == Symbol::intern("cfg_match") {
612-
return true;
613-
}
614-
}
615-
false
616-
}
609+
ast::ItemKind::MacCall(ref mac) => is_macro_name(mac, "cfg_match"),
617610
_ => false,
618611
}
619612
}

src/modules/visitor.rs

+8-42
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use rustc_ast::ast;
22
use rustc_ast::visit::Visitor;
3-
use rustc_span::{Symbol, sym};
3+
use rustc_span::sym;
44
use tracing::debug;
55

6+
use super::is_macro_name;
67
use crate::attr::MetaVisitor;
78
use crate::parse::macros::cfg_if::parse_cfg_if;
89
use crate::parse::macros::cfg_match::parse_cfg_match;
@@ -42,27 +43,9 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CfgIfVisitor<'a> {
4243

4344
impl<'a, 'ast: 'a> CfgIfVisitor<'a> {
4445
fn visit_mac_inner(&mut self, mac: &'ast ast::MacCall) -> Result<(), &'static str> {
45-
// Support both:
46-
// ```
47-
// extern crate cfg_if;
48-
// cfg_if::cfg_if! {..}
49-
// ```
50-
// And:
51-
// ```
52-
// #[macro_use]
53-
// extern crate cfg_if;
54-
// cfg_if! {..}
55-
// ```
56-
match mac.path.segments.first() {
57-
Some(first_segment) => {
58-
if first_segment.ident.name != Symbol::intern("cfg_if") {
59-
return Err("Expected cfg_if");
60-
}
61-
}
62-
None => {
63-
return Err("Expected cfg_if");
64-
}
65-
};
46+
if !is_macro_name(mac, "cfg_if") {
47+
return Err("Expected cfg_if");
48+
}
6649

6750
let items = parse_cfg_if(self.psess, mac)?;
6851
self.mods
@@ -102,26 +85,9 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CfgMatchVisitor<'a> {
10285

10386
impl<'a, 'ast: 'a> CfgMatchVisitor<'a> {
10487
fn visit_mac_inner(&mut self, mac: &'ast ast::MacCall) -> Result<(), &'static str> {
105-
// Support both:
106-
// ```
107-
// std::cfg_match! {..}
108-
// core::cfg_match! {..}
109-
// ```
110-
// And:
111-
// ```
112-
// use std::cfg_match;
113-
// cfg_match! {..}
114-
// ```
115-
match mac.path.segments.last() {
116-
Some(last_segment) => {
117-
if last_segment.ident.name != Symbol::intern("cfg_match") {
118-
return Err("Expected cfg_match");
119-
}
120-
}
121-
None => {
122-
return Err("Expected cfg_match");
123-
}
124-
};
88+
if !is_macro_name(mac, "cfg_match") {
89+
return Err("Expected cfg_match");
90+
}
12591

12692
let items = parse_cfg_match(self.psess, mac)?;
12793
self.mods

0 commit comments

Comments
 (0)