Skip to content

Commit d7a2837

Browse files
committed
rustdoc: Prune undocumented impls
1 parent fee9037 commit d7a2837

File tree

1 file changed

+88
-18
lines changed

1 file changed

+88
-18
lines changed

src/rustdoc/prune_undoc_pass.rs

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ fn run(
2323
fold_const: fold_const,
2424
fold_enum: fold_enum,
2525
fold_res: fold_res,
26-
fold_iface: fold_iface
26+
fold_iface: fold_iface,
27+
fold_impl: fold_impl
2728
with *fold::default_seq_fold(ctxt)
2829
});
2930
fold.fold_crate(fold, doc)
@@ -84,6 +85,14 @@ fn fold_mod(
8485
none
8586
}
8687
}
88+
doc::impltag(impldoc) {
89+
let doc = fold.fold_impl(fold, impldoc);
90+
if fold.ctxt.have_docs {
91+
some(doc::impltag(doc))
92+
} else {
93+
none
94+
}
95+
}
8796
_ { some(itemtag) }
8897
}
8998
}
@@ -286,28 +295,35 @@ fn fold_iface(
286295
) -> doc::ifacedoc {
287296
let doc = fold::default_seq_fold_iface(fold, doc);
288297
let doc = {
289-
methods: vec::map(doc.methods) {|doc|
290-
{
291-
args: prune_args(doc.args),
292-
return: prune_return(doc.return)
293-
with doc
294-
}
295-
}
298+
methods: prune_methods(doc.methods)
296299
with doc
297300
};
298-
let methods_have_docs = vec::foldl(false, doc.methods) {|accum, doc|
301+
fold.ctxt.have_docs =
302+
doc.brief != none
303+
|| doc.desc != none
304+
|| methods_have_docs(doc.methods);
305+
ret doc;
306+
}
307+
308+
fn prune_methods(docs: [doc::methoddoc]) -> [doc::methoddoc] {
309+
vec::map(docs) {|doc|
310+
{
311+
args: prune_args(doc.args),
312+
return: prune_return(doc.return)
313+
with doc
314+
}
315+
}
316+
}
317+
318+
fn methods_have_docs(docs: [doc::methoddoc]) -> bool {
319+
vec::foldl(false, docs) {|accum, doc|
299320
accum
300321
|| doc.brief != none
301322
|| doc.desc != none
302323
|| vec::is_not_empty(doc.args)
303324
|| doc.return.desc != none
304325
|| doc.failure != none
305-
};
306-
fold.ctxt.have_docs =
307-
doc.brief != none
308-
|| doc.desc != none
309-
|| methods_have_docs;
310-
ret doc;
326+
}
311327
}
312328

313329
#[test]
@@ -329,23 +345,77 @@ fn should_not_elide_ifaces_with_documented_methods() {
329345
}
330346

331347
#[test]
332-
fn should_not_elide_undocumented_methods() {
348+
fn should_not_elide_undocumented_iface_methods() {
333349
let doc = test::mk_doc("#[doc = \"hey\"] iface i { fn a(); }");
334350
assert vec::is_not_empty(doc.topmod.ifaces()[0].methods);
335351
}
336352

337353
#[test]
338-
fn should_elide_undocumented_method_args() {
354+
fn should_elide_undocumented_iface_method_args() {
339355
let doc = test::mk_doc("#[doc = \"hey\"] iface i { fn a(); }");
340356
assert vec::is_empty(doc.topmod.ifaces()[0].methods[0].args);
341357
}
342358

343359
#[test]
344-
fn should_elide_undocumented_method_return_values() {
360+
fn should_elide_undocumented_iface_method_return_values() {
345361
let doc = test::mk_doc("#[doc = \"hey\"] iface i { fn a() -> int; }");
346362
assert doc.topmod.ifaces()[0].methods[0].return.ty == none;
347363
}
348364

365+
fn fold_impl(
366+
fold: fold::fold<ctxt>,
367+
doc: doc::impldoc
368+
) -> doc::impldoc {
369+
let doc = fold::default_seq_fold_impl(fold, doc);
370+
let doc = {
371+
methods: prune_methods(doc.methods)
372+
with doc
373+
};
374+
fold.ctxt.have_docs =
375+
doc.brief != none
376+
|| doc.desc != none
377+
|| methods_have_docs(doc.methods);
378+
ret doc;
379+
}
380+
381+
#[test]
382+
fn should_elide_undocumented_impls() {
383+
let doc = test::mk_doc("impl i for int { fn a() { } }");
384+
assert vec::is_empty(doc.topmod.impls());
385+
}
386+
387+
#[test]
388+
fn should_not_elide_documented_impls() {
389+
let doc = test::mk_doc("#[doc = \"hey\"] impl i for int { fn a() { } }");
390+
assert vec::is_not_empty(doc.topmod.impls());
391+
}
392+
393+
#[test]
394+
fn should_not_elide_impls_with_documented_methods() {
395+
let doc = test::mk_doc("impl i for int { #[doc = \"hey\"] fn a() { } }");
396+
assert vec::is_not_empty(doc.topmod.impls());
397+
}
398+
399+
#[test]
400+
fn should_not_elide_undocumented_impl_methods() {
401+
let doc = test::mk_doc("#[doc = \"hey\"] impl i for int { fn a() { } }");
402+
assert vec::is_not_empty(doc.topmod.impls()[0].methods);
403+
}
404+
405+
#[test]
406+
fn should_elide_undocumented_impl_method_args() {
407+
let doc = test::mk_doc(
408+
"#[doc = \"hey\"] impl i for int { fn a(b: bool) { } }");
409+
assert vec::is_empty(doc.topmod.impls()[0].methods[0].args);
410+
}
411+
412+
#[test]
413+
fn should_elide_undocumented_impl_method_return_values() {
414+
let doc = test::mk_doc(
415+
"#[doc = \"hey\"] impl i for int { fn a() -> int { } }");
416+
assert doc.topmod.impls()[0].methods[0].return.ty == none;
417+
}
418+
349419
#[cfg(test)]
350420
mod test {
351421
fn mk_doc(source: str) -> doc::cratedoc {

0 commit comments

Comments
 (0)