Skip to content

Commit 5934944

Browse files
tests: add tests to cover cfg_if mods
1 parent 19e8746 commit 5934944

File tree

14 files changed

+92
-3
lines changed

14 files changed

+92
-3
lines changed

src/modules/visitor.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,16 @@ impl<'a, 'ast: 'a> CfgIfVisitor<'a> {
5454
// extern crate cfg_if;
5555
// cfg_if! {..}
5656
// ```
57-
if let Some(first_segment) = mac.node.path.segments.first() {
58-
if first_segment.ident.name != Symbol::intern("cfg_if") {
57+
match mac.node.path.segments.first() {
58+
Some(first_segment) => {
59+
if first_segment.ident.name != Symbol::intern("cfg_if") {
60+
return Err("Expected cfg_if");
61+
}
62+
}
63+
None => {
5964
return Err("Expected cfg_if");
6065
}
61-
}
66+
};
6267

6368
let mut parser = stream_to_parser_with_base_dir(
6469
self.parse_sess,

src/test/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const SKIP_FILE_WHITE_LIST: &[&str] = &[
3030
// These files and directory are a part of modules defined inside `cfg_if!`.
3131
"cfg_if/mod.rs",
3232
"cfg_if/detect",
33+
"issue-3253/foo.rs",
34+
"issue-3253/bar.rs",
35+
"issue-3253/paths",
3336
// These files and directory are a part of modules defined inside `cfg_attr(..)`.
3437
"cfg_mod/dir",
3538
"cfg_mod/bar.rs",

tests/source/issue-3253/bar.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Empty
2+
fn empty() {
3+
4+
}

tests/source/issue-3253/foo.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub fn hello( )
2+
{
3+
println!("Hello World!");
4+
5+
}
6+

tests/source/issue-3253/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[macro_use]
2+
extern crate cfg_if;
3+
4+
cfg_if! {
5+
if #[cfg(target_family = "unix")] {
6+
mod foo;
7+
#[path = "paths/bar_foo.rs"]
8+
mod bar_foo;
9+
} else {
10+
mod bar;
11+
#[path = "paths/foo_bar.rs"]
12+
mod foo_bar;
13+
}
14+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn foo_decl_item(x: &mut i32) {
2+
x = 3;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This module is not imported in the cfg_if macro in lib.rs so it is ignored
2+
// while the foo and bar mods are formatted.
3+
// Check the corresponding file in tests/target/issue-3253/paths/excluded.rs
4+
trait CoolerTypes { fn dummy(&self) {
5+
}
6+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
fn Foo<T>() where T: Bar {
4+
}

tests/target/issue-3253/bar.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Empty
2+
fn empty() {}

tests/target/issue-3253/foo.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn hello() {
2+
println!("Hello World!");
3+
}

tests/target/issue-3253/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[macro_use]
2+
extern crate cfg_if;
3+
4+
cfg_if! {
5+
if #[cfg(target_family = "unix")] {
6+
mod foo;
7+
#[path = "paths/bar_foo.rs"]
8+
mod bar_foo;
9+
} else {
10+
mod bar;
11+
#[path = "paths/foo_bar.rs"]
12+
mod foo_bar;
13+
}
14+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn foo_decl_item(x: &mut i32) {
2+
x = 3;
3+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This module is not imported in the cfg_if macro in lib.rs so it is ignored
2+
// while the foo and bar mods are formatted.
3+
// Check the corresponding file in tests/source/issue-3253/paths/excluded.rs
4+
5+
6+
7+
8+
fn Foo<T>() where T: Bar {
9+
}
10+
11+
12+
13+
trait CoolerTypes { fn dummy(&self) {
14+
}
15+
}
16+
17+
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn Foo<T>()
2+
where
3+
T: Bar,
4+
{
5+
}

0 commit comments

Comments
 (0)