Skip to content

Commit 4a5d887

Browse files
committed
Allow doc(hidden) and --test to disable doc linting
1 parent af995ce commit 4a5d887

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/librustc/middle/lint.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ struct Context {
297297
// Just a simple flag if we're currently recursing into a trait
298298
// implementation. This is only used by the lint_missing_doc() pass
299299
in_trait_impl: bool,
300+
// Another flag for doc lint emissions. Does some parent of the current node
301+
// have the doc(hidden) attribute? Treating this as allow(missing_doc) would
302+
// play badly with forbid(missing_doc) when it shouldn't.
303+
doc_hidden: bool,
300304
// When recursing into an attributed node of the ast which modifies lint
301305
// levels, this stack keeps track of the previous lint levels of whatever
302306
// was modified.
@@ -422,9 +426,30 @@ impl Context {
422426
}
423427
}
424428

429+
// detect doc(hidden)
430+
let mut doc_hidden = false;
431+
for attr::find_attrs_by_name(attrs, "doc").each |attr| {
432+
match attr::get_meta_item_list(attr.node.value) {
433+
Some(s) => {
434+
if attr::find_meta_items_by_name(s, "hidden").len() > 0 {
435+
doc_hidden = true;
436+
}
437+
}
438+
None => {}
439+
}
440+
}
441+
if doc_hidden && !self.doc_hidden {
442+
self.doc_hidden = true;
443+
} else {
444+
doc_hidden = false;
445+
}
446+
425447
f();
426448

427449
// rollback
450+
if doc_hidden && self.doc_hidden {
451+
self.doc_hidden = false;
452+
}
428453
for pushed.times {
429454
let (lint, lvl, src) = self.lint_stack.pop();
430455
self.set_level(lint, lvl, src);
@@ -980,9 +1005,16 @@ fn lint_unnecessary_allocations() -> visit::vt<@mut Context> {
9801005
fn lint_missing_doc() -> visit::vt<@mut Context> {
9811006
fn check_attrs(cx: @mut Context, attrs: &[ast::attribute],
9821007
sp: span, msg: &str) {
983-
if !attrs.any(|a| a.node.is_sugared_doc) {
984-
cx.span_lint(missing_doc, sp, msg);
985-
}
1008+
// If we're building a test harness, then warning about documentation is
1009+
// probably not really relevant right now
1010+
if cx.tcx.sess.opts.test { return }
1011+
// If we have doc(hidden), nothing to do
1012+
if cx.doc_hidden { return }
1013+
// If we're documented, nothing to do
1014+
if attrs.any(|a| a.node.is_sugared_doc) { return }
1015+
1016+
// otherwise, warn!
1017+
cx.span_lint(missing_doc, sp, msg);
9861018
}
9871019

9881020
visit::mk_vt(@visit::Visitor {
@@ -1067,6 +1099,7 @@ pub fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
10671099
lint_stack: ~[],
10681100
visitors: ~[],
10691101
in_trait_impl: false,
1102+
doc_hidden: false,
10701103
};
10711104

10721105
// Install defaults.

src/test/compile-fail/lint-missing-doc.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,17 @@ impl F for Foo {
6969
fn b(&self) {}
7070
}
7171

72+
// It sure is nice if doc(hidden) implies allow(missing_doc), and that it
73+
// applies recursively
74+
#[doc(hidden)]
75+
mod a {
76+
pub fn baz() {}
77+
pub mod b {
78+
pub fn baz() {}
79+
}
80+
}
81+
82+
#[doc(hidden)]
83+
pub fn baz() {}
84+
7285
fn main() {}

0 commit comments

Comments
 (0)