Skip to content

Commit 4730953

Browse files
committed
Neaten up fold_crate.
1 parent 8909f70 commit 4730953

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

src/libsyntax/fold.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -980,38 +980,29 @@ pub fn noop_fold_mod<T: Folder>(Mod {inner, items, inline}: Mod, folder: &mut T)
980980

981981
pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, span}: Crate,
982982
folder: &mut T) -> Crate {
983-
let mut items = folder.fold_item(P(Item {
983+
let item = P(Item {
984984
ident: keywords::Invalid.ident(),
985985
attrs,
986986
id: DUMMY_NODE_ID,
987987
vis: respan(span.shrink_to_lo(), VisibilityKind::Public),
988988
span,
989989
node: ItemKind::Mod(module),
990990
tokens: None,
991-
})).into_iter();
992-
993-
let (module, attrs, span) = match items.next() {
994-
Some(item) => {
995-
assert!(items.next().is_none(),
996-
"a crate cannot expand to more than one item");
997-
item.and_then(|Item { attrs, span, node, .. }| {
998-
match node {
999-
ItemKind::Mod(m) => (m, attrs, span),
1000-
_ => panic!("fold converted a module to not a module"),
1001-
}
1002-
})
991+
});
992+
let items = folder.fold_item(item);
993+
994+
let len = items.len();
995+
if len == 0 {
996+
let module = Mod { inner: span, items: vec![], inline: true };
997+
Crate { module, attrs: vec![], span }
998+
} else if len == 1 {
999+
let Item { attrs, span, node, .. } = items.into_iter().next().unwrap().into_inner();
1000+
match node {
1001+
ItemKind::Mod(module) => Crate { module, attrs, span },
1002+
_ => panic!("fold converted a module to not a module"),
10031003
}
1004-
None => (Mod {
1005-
inner: span,
1006-
items: vec![],
1007-
inline: true,
1008-
}, vec![], span)
1009-
};
1010-
1011-
Crate {
1012-
module,
1013-
attrs,
1014-
span,
1004+
} else {
1005+
panic!("a crate cannot expand to more than one item");
10151006
}
10161007
}
10171008

0 commit comments

Comments
 (0)