Skip to content

Commit eb93d1b

Browse files
Improve code readability
1 parent 9c46173 commit eb93d1b

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/librustdoc/html/render/write_shared.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub(super) fn write_shared(
137137
Ok((ret, krates))
138138
}
139139

140-
/// Read a file and return all lines that match the <code>"{crate}":{data},\</code> format,
140+
/// Read a file and return all lines that match the <code>"{crate}":{data},\ </code> format,
141141
/// and return a tuple `(Vec<DataString>, Vec<CrateNameString>)`.
142142
///
143143
/// This forms the payload of files that look like this:

src/librustdoc/visit_ast.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
187187
ret
188188
}
189189

190+
#[inline]
191+
fn add_to_current_mod(
192+
&mut self,
193+
item: &'tcx hir::Item<'_>,
194+
renamed: Option<Symbol>,
195+
parent_id: Option<hir::HirId>,
196+
) {
197+
self.modules.last_mut().unwrap().items.push((item, renamed, parent_id))
198+
}
199+
190200
fn visit_item_inner(
191201
&mut self,
192202
item: &'tcx hir::Item<'_>,
@@ -247,7 +257,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
247257
}
248258
}
249259

250-
self.modules.last_mut().unwrap().items.push((item, renamed, parent_id));
260+
self.add_to_current_mod(item, renamed, parent_id);
251261
}
252262
}
253263
hir::ItemKind::Macro(ref macro_def, _) => {
@@ -267,7 +277,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
267277
let nonexported = !self.cx.tcx.has_attr(def_id, sym::macro_export);
268278

269279
if is_macro_2_0 || nonexported || self.inlining {
270-
self.modules.last_mut().unwrap().items.push((item, renamed, None));
280+
self.add_to_current_mod(item, renamed, None);
271281
}
272282
}
273283
hir::ItemKind::Mod(ref m) => {
@@ -283,20 +293,20 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
283293
| hir::ItemKind::Static(..)
284294
| hir::ItemKind::Trait(..)
285295
| hir::ItemKind::TraitAlias(..) => {
286-
self.modules.last_mut().unwrap().items.push((item, renamed, parent_id))
296+
self.add_to_current_mod(item, renamed, parent_id);
287297
}
288298
hir::ItemKind::Const(..) => {
289299
// Underscore constants do not correspond to a nameable item and
290300
// so are never useful in documentation.
291301
if name != kw::Underscore {
292-
self.modules.last_mut().unwrap().items.push((item, renamed, parent_id));
302+
self.add_to_current_mod(item, renamed, parent_id);
293303
}
294304
}
295305
hir::ItemKind::Impl(impl_) => {
296306
// Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
297307
// them up regardless of where they're located.
298308
if !self.inlining && impl_.of_trait.is_none() {
299-
self.modules.last_mut().unwrap().items.push((item, None, None));
309+
self.add_to_current_mod(item, None, None);
300310
}
301311
}
302312
}
@@ -333,15 +343,13 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
333343
// macro in the same module.
334344
let mut inserted = FxHashSet::default();
335345
for export in self.cx.tcx.module_reexports(CRATE_DEF_ID).unwrap_or(&[]) {
336-
if let Res::Def(DefKind::Macro(_), def_id) = export.res {
337-
if let Some(local_def_id) = def_id.as_local() {
338-
if self.cx.tcx.has_attr(def_id, sym::macro_export) {
339-
if inserted.insert(def_id) {
340-
let item = self.cx.tcx.hir().expect_item(local_def_id);
341-
top_level_module.items.push((item, None, None));
342-
}
343-
}
344-
}
346+
if let Res::Def(DefKind::Macro(_), def_id) = export.res &&
347+
let Some(local_def_id) = def_id.as_local() &&
348+
self.cx.tcx.has_attr(def_id, sym::macro_export) &&
349+
inserted.insert(def_id)
350+
{
351+
let item = self.cx.tcx.hir().expect_item(local_def_id);
352+
top_level_module.items.push((item, None, None));
345353
}
346354
}
347355

0 commit comments

Comments
 (0)