Skip to content

Commit 5d04ee8

Browse files
committed
auto merge of #6715 : Xazax-hun/rust/incoming, r=graydon
Preliminary implementation for: #6275 This is my first (non hello world) rust code, so it may not be idiomatic.
2 parents b0f3686 + 3d61931 commit 5d04ee8

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/librustc/middle/lint.rs

+82
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
/*
@@ -931,6 +948,69 @@ fn lint_unnecessary_allocations(cx: @mut Context) -> visit::vt<()> {
931948
})
932949
}
933950

951+
fn lint_missing_struct_doc(cx: @mut Context) -> visit::vt<()> {
952+
visit::mk_simple_visitor(@visit::SimpleVisitor {
953+
visit_struct_field: |field| {
954+
let relevant = match field.node.kind {
955+
ast::named_field(_, vis) => vis != ast::private,
956+
ast::unnamed_field => false,
957+
};
958+
959+
if relevant {
960+
let mut has_doc = false;
961+
for field.node.attrs.each |attr| {
962+
if attr.node.is_sugared_doc {
963+
has_doc = true;
964+
break;
965+
}
966+
}
967+
if !has_doc {
968+
cx.span_lint(missing_struct_doc, field.span, "missing documentation \
969+
for a field.");
970+
}
971+
}
972+
},
973+
.. *visit::default_simple_visitor()
974+
})
975+
}
976+
977+
fn lint_missing_trait_doc(cx: @mut Context) -> visit::vt<()> {
978+
visit::mk_simple_visitor(@visit::SimpleVisitor {
979+
visit_trait_method: |method| {
980+
let mut has_doc = false;
981+
let span = match copy *method {
982+
ast::required(m) => {
983+
for m.attrs.each |attr| {
984+
if attr.node.is_sugared_doc {
985+
has_doc = true;
986+
break;
987+
}
988+
}
989+
m.span
990+
},
991+
ast::provided(m) => {
992+
if m.vis == ast::private {
993+
has_doc = true;
994+
} else {
995+
for m.attrs.each |attr| {
996+
if attr.node.is_sugared_doc {
997+
has_doc = true;
998+
break;
999+
}
1000+
}
1001+
}
1002+
m.span
1003+
}
1004+
};
1005+
if !has_doc {
1006+
cx.span_lint(missing_trait_doc, span, "missing documentation \
1007+
for a method.");
1008+
}
1009+
},
1010+
.. *visit::default_simple_visitor()
1011+
})
1012+
}
1013+
9341014
pub fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
9351015
let cx = @mut Context {
9361016
dict: @get_lint_dict(),
@@ -959,6 +1039,8 @@ pub fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
9591039
cx.add_lint(lint_unused_mut(cx));
9601040
cx.add_lint(lint_session(cx));
9611041
cx.add_lint(lint_unnecessary_allocations(cx));
1042+
cx.add_lint(lint_missing_struct_doc(cx));
1043+
cx.add_lint(lint_missing_trait_doc(cx));
9621044

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

0 commit comments

Comments
 (0)