From c6581325ac38ce3f96cb7a0ea0aad35feed173c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Horv=C3=A1th?= Date: Fri, 24 May 2013 10:27:31 +0200 Subject: [PATCH 1/2] Warnings for missing documentations. --- src/librustc/middle/lint.rs | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index d156457ca8865..50d127869e572 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -82,6 +82,9 @@ pub enum lint { dead_assignment, unused_mut, unnecessary_allocation, + + missing_struct_doc, + missing_trait_doc, } pub fn level_to_str(lv: level) -> &'static str { @@ -252,6 +255,20 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[ desc: "detects unnecessary allocations that can be eliminated", default: warn }), + + ("missing_struct_doc", + LintSpec { + lint: missing_struct_doc, + desc: "detects missing documentation for structs", + default: allow + }), + + ("missing_trait_doc", + LintSpec { + lint: missing_trait_doc, + desc: "detects missing documentation for traits", + default: allow + }), ]; /* @@ -952,6 +969,58 @@ fn lint_unnecessary_allocations(cx: @mut Context) -> visit::vt<()> { }) } +fn lint_missing_struct_doc(cx: @mut Context) -> visit::vt<()> { + visit::mk_simple_visitor(@visit::SimpleVisitor { + visit_struct_field: |field| { + let mut has_doc = false; + for field.node.attrs.each |attr| { + if attr.node.is_sugared_doc { + has_doc = true; + break; + } + } + if !has_doc { + cx.span_lint(missing_struct_doc, field.span, "missing documentation \ + for a field."); + } + }, + .. *visit::default_simple_visitor() + }) +} + +fn lint_missing_trait_doc(cx: @mut Context) -> visit::vt<()> { + visit::mk_simple_visitor(@visit::SimpleVisitor { + visit_trait_method: |method| { + let mut has_doc = false; + let span = match copy *method { + ast::required(m) => { + for m.attrs.each |attr| { + if attr.node.is_sugared_doc { + has_doc = true; + break; + } + } + m.span + }, + ast::provided(m) => { + for m.attrs.each |attr| { + if attr.node.is_sugared_doc { + has_doc = true; + break; + } + } + m.span + } + }; + if !has_doc { + cx.span_lint(missing_trait_doc, span, "missing documentation \ + for a method."); + } + }, + .. *visit::default_simple_visitor() + }) +} + pub fn check_crate(tcx: ty::ctxt, crate: @ast::crate) { let cx = @mut Context { dict: @get_lint_dict(), @@ -980,6 +1049,8 @@ pub fn check_crate(tcx: ty::ctxt, crate: @ast::crate) { cx.add_lint(lint_unused_mut(cx)); cx.add_lint(lint_session(cx)); cx.add_lint(lint_unnecessary_allocations(cx)); + cx.add_lint(lint_missing_struct_doc(cx)); + cx.add_lint(lint_missing_trait_doc(cx)); // type inference doesn't like this being declared below, we need to tell it // what the type of this first function is... From 3d61931fcac13aadc88ece7e48567f7ff503fba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Horv=C3=A1th?= Date: Fri, 24 May 2013 14:13:41 +0200 Subject: [PATCH 2/2] Only trigger missing documentation warnings to public functions and fields. --- src/librustc/middle/lint.rs | 39 ++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index 50d127869e572..a49dfc97f7152 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -972,16 +972,23 @@ fn lint_unnecessary_allocations(cx: @mut Context) -> visit::vt<()> { fn lint_missing_struct_doc(cx: @mut Context) -> visit::vt<()> { visit::mk_simple_visitor(@visit::SimpleVisitor { visit_struct_field: |field| { - let mut has_doc = false; - for field.node.attrs.each |attr| { - if attr.node.is_sugared_doc { - has_doc = true; - break; + let relevant = match field.node.kind { + ast::named_field(_, vis) => vis != ast::private, + ast::unnamed_field => false, + }; + + if relevant { + let mut has_doc = false; + for field.node.attrs.each |attr| { + if attr.node.is_sugared_doc { + has_doc = true; + break; + } + } + if !has_doc { + cx.span_lint(missing_struct_doc, field.span, "missing documentation \ + for a field."); } - } - if !has_doc { - cx.span_lint(missing_struct_doc, field.span, "missing documentation \ - for a field."); } }, .. *visit::default_simple_visitor() @@ -1003,10 +1010,14 @@ fn lint_missing_trait_doc(cx: @mut Context) -> visit::vt<()> { m.span }, ast::provided(m) => { - for m.attrs.each |attr| { - if attr.node.is_sugared_doc { - has_doc = true; - break; + if m.vis == ast::private { + has_doc = true; + } else { + for m.attrs.each |attr| { + if attr.node.is_sugared_doc { + has_doc = true; + break; + } } } m.span @@ -1014,7 +1025,7 @@ fn lint_missing_trait_doc(cx: @mut Context) -> visit::vt<()> { }; if !has_doc { cx.span_lint(missing_trait_doc, span, "missing documentation \ - for a method."); + for a method."); } }, .. *visit::default_simple_visitor()