diff --git a/src/lib.rs b/src/lib.rs index da83b285a21..370b7eb6c4e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ #[macro_use] extern crate derive_new; +#[cfg(test)] #[macro_use] extern crate lazy_static; #[macro_use] diff --git a/src/modules.rs b/src/modules.rs index b4d2f1fc822..cadcd30a3d2 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -21,10 +21,6 @@ mod visitor; type FileModMap<'ast> = BTreeMap>; -lazy_static! { - static ref CFG_IF: Symbol = Symbol::intern("cfg_if"); -} - /// Represents module with its inner attributes. #[derive(Debug, Clone)] pub(crate) struct Module<'a> { @@ -480,7 +476,7 @@ fn is_cfg_if(item: &ast::Item) -> bool { match item.kind { ast::ItemKind::MacCall(ref mac) => { if let Some(first_segment) = mac.path.segments.first() { - if first_segment.ident.name == *CFG_IF { + if first_segment.ident.name == Symbol::intern("cfg_if") { return true; } } diff --git a/src/test/mod.rs b/src/test/mod.rs index 9b3ca717152..142de28f330 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -372,6 +372,44 @@ fn self_tests() { ); } +#[test] +fn format_files_find_new_files_via_cfg_if() { + init_log(); + run_test_with(&TestSetting::default(), || { + // To repro issue-4656, it is necessary that these files are parsed + // as a part of the same session (hence this separate test runner). + let files = vec![ + Path::new("tests/source/issue-4656/lib2.rs"), + Path::new("tests/source/issue-4656/lib.rs"), + ]; + + let config = Config::default(); + let mut session = Session::::new(config, None); + + let mut write_result = HashMap::new(); + for file in files { + assert!(file.exists()); + let result = session.format(Input::File(file.into())).unwrap(); + assert!(!session.has_formatting_errors()); + assert!(!result.has_warnings()); + let mut source_file = SourceFile::new(); + mem::swap(&mut session.source_file, &mut source_file); + + for (filename, text) in source_file { + if let FileName::Real(ref filename) = filename { + write_result.insert(filename.to_owned(), text); + } + } + } + assert_eq!( + 3, + write_result.len(), + "Should have uncovered an extra file (format_me_please.rs) via lib.rs" + ); + assert!(handle_result(write_result, None).is_ok()); + }); +} + #[test] fn stdin_formatting_smoke_test() { init_log(); diff --git a/tests/source/issue-4656/format_me_please.rs b/tests/source/issue-4656/format_me_please.rs new file mode 100644 index 00000000000..7de75301649 --- /dev/null +++ b/tests/source/issue-4656/format_me_please.rs @@ -0,0 +1,2 @@ + +pub fn hello( ) { } diff --git a/tests/source/issue-4656/lib.rs b/tests/source/issue-4656/lib.rs new file mode 100644 index 00000000000..5dac91b8aab --- /dev/null +++ b/tests/source/issue-4656/lib.rs @@ -0,0 +1,7 @@ +extern crate cfg_if; + +cfg_if::cfg_if! { + if #[cfg(target_family = "unix")] { + mod format_me_please; + } +} diff --git a/tests/source/issue-4656/lib2.rs b/tests/source/issue-4656/lib2.rs new file mode 100644 index 00000000000..b17fffc58e1 --- /dev/null +++ b/tests/source/issue-4656/lib2.rs @@ -0,0 +1,3 @@ +its_a_macro! { + // Contents +} diff --git a/tests/target/issue-4656/format_me_please.rs b/tests/target/issue-4656/format_me_please.rs new file mode 100644 index 00000000000..421e195a2fb --- /dev/null +++ b/tests/target/issue-4656/format_me_please.rs @@ -0,0 +1 @@ +pub fn hello() {} diff --git a/tests/target/issue-4656/lib.rs b/tests/target/issue-4656/lib.rs new file mode 100644 index 00000000000..5dac91b8aab --- /dev/null +++ b/tests/target/issue-4656/lib.rs @@ -0,0 +1,7 @@ +extern crate cfg_if; + +cfg_if::cfg_if! { + if #[cfg(target_family = "unix")] { + mod format_me_please; + } +} diff --git a/tests/target/issue-4656/lib2.rs b/tests/target/issue-4656/lib2.rs new file mode 100644 index 00000000000..b17fffc58e1 --- /dev/null +++ b/tests/target/issue-4656/lib2.rs @@ -0,0 +1,3 @@ +its_a_macro! { + // Contents +}