Skip to content

Commit c658132

Browse files
committed
Warnings for missing documentations.
1 parent ed9a793 commit c658132

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/librustc/middle/lint.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ pub enum lint {
8282
dead_assignment,
8383
unused_mut,
8484
unnecessary_allocation,
85+
86+
missing_struct_doc,
87+
missing_trait_doc,
8588
}
8689

8790
pub fn level_to_str(lv: level) -> &'static str {
@@ -252,6 +255,20 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
252255
desc: "detects unnecessary allocations that can be eliminated",
253256
default: warn
254257
}),
258+
259+
("missing_struct_doc",
260+
LintSpec {
261+
lint: missing_struct_doc,
262+
desc: "detects missing documentation for structs",
263+
default: allow
264+
}),
265+
266+
("missing_trait_doc",
267+
LintSpec {
268+
lint: missing_trait_doc,
269+
desc: "detects missing documentation for traits",
270+
default: allow
271+
}),
255272
];
256273

257274
/*
@@ -952,6 +969,58 @@ fn lint_unnecessary_allocations(cx: @mut Context) -> visit::vt<()> {
952969
})
953970
}
954971

972+
fn lint_missing_struct_doc(cx: @mut Context) -> visit::vt<()> {
973+
visit::mk_simple_visitor(@visit::SimpleVisitor {
974+
visit_struct_field: |field| {
975+
let mut has_doc = false;
976+
for field.node.attrs.each |attr| {
977+
if attr.node.is_sugared_doc {
978+
has_doc = true;
979+
break;
980+
}
981+
}
982+
if !has_doc {
983+
cx.span_lint(missing_struct_doc, field.span, "missing documentation \
984+
for a field.");
985+
}
986+
},
987+
.. *visit::default_simple_visitor()
988+
})
989+
}
990+
991+
fn lint_missing_trait_doc(cx: @mut Context) -> visit::vt<()> {
992+
visit::mk_simple_visitor(@visit::SimpleVisitor {
993+
visit_trait_method: |method| {
994+
let mut has_doc = false;
995+
let span = match copy *method {
996+
ast::required(m) => {
997+
for m.attrs.each |attr| {
998+
if attr.node.is_sugared_doc {
999+
has_doc = true;
1000+
break;
1001+
}
1002+
}
1003+
m.span
1004+
},
1005+
ast::provided(m) => {
1006+
for m.attrs.each |attr| {
1007+
if attr.node.is_sugared_doc {
1008+
has_doc = true;
1009+
break;
1010+
}
1011+
}
1012+
m.span
1013+
}
1014+
};
1015+
if !has_doc {
1016+
cx.span_lint(missing_trait_doc, span, "missing documentation \
1017+
for a method.");
1018+
}
1019+
},
1020+
.. *visit::default_simple_visitor()
1021+
})
1022+
}
1023+
9551024
pub fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
9561025
let cx = @mut Context {
9571026
dict: @get_lint_dict(),
@@ -980,6 +1049,8 @@ pub fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
9801049
cx.add_lint(lint_unused_mut(cx));
9811050
cx.add_lint(lint_session(cx));
9821051
cx.add_lint(lint_unnecessary_allocations(cx));
1052+
cx.add_lint(lint_missing_struct_doc(cx));
1053+
cx.add_lint(lint_missing_trait_doc(cx));
9831054

9841055
// type inference doesn't like this being declared below, we need to tell it
9851056
// what the type of this first function is...

0 commit comments

Comments
 (0)