Skip to content

turn errors from loading external docs into a proper lint #46567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,54 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnsafeCode {
}
}

declare_lint! {
EXTERNAL_DOC_ERROR,
Warn,
"errors that occur when loading external documentation"
}

pub struct ExternalDocError;

impl LintPass for ExternalDocError {
fn get_lints(&self) -> LintArray {
lint_array!(EXTERNAL_DOC_ERROR)
}
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExternalDocError {
fn check_attribute(&mut self, cx: &LateContext, attr: &ast::Attribute) {
//in libsyntax, failures to read a file transform a #[doc(include = "filename")] into a
//#[doc(include(file="filename", error="error message"))], so we need to pick that up here

if !attr.check_name("doc") {
return;
}

if let Some(list) = attr.meta_item_list() {
for it in list {
if !it.check_name("include") {
continue;
}

if let Some(inner_list) = it.meta_item_list() {
let mut error: Option<String> = None;

for it in inner_list {
if it.check_name("error") {
error = it.value_str().map(|s| s.to_string());
break;
}
}

if let Some(error) = error {
cx.span_lint(EXTERNAL_DOC_ERROR, attr.span(), &error);
}
}
}
}
}
}

declare_lint! {
MISSING_DOCS,
Allow,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
MutableTransmutes,
UnionsWithDropFields,
UnreachablePub,
ExternalDocError,
);

add_builtin_with_new!(sess,
Expand Down
44 changes: 29 additions & 15 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,17 +666,18 @@ impl Attributes {
/// `#[doc(include="file")]`, and returns the filename and contents of the file as loaded from
/// its expansion.
fn extract_include(mi: &ast::MetaItem)
-> Option<(String, String)>
-> Result<Option<(String, String)>, String>
{
mi.meta_item_list().and_then(|list| {
mi.meta_item_list().map_or(Ok(None), |list| {
for meta in list {
if meta.check_name("include") {
// the actual compiled `#[doc(include="filename")]` gets expanded to
// `#[doc(include(file="filename", contents="file contents")]` so we need to
// look for that instead
return meta.meta_item_list().and_then(|list| {
return meta.meta_item_list().map_or(Ok(None), |list| {
let mut filename: Option<String> = None;
let mut contents: Option<String> = None;
let mut error: Option<String> = None;

for it in list {
if it.check_name("file") {
Expand All @@ -687,19 +688,25 @@ impl Attributes {
if let Some(docs) = it.value_str() {
contents = Some(docs.to_string());
}
} else if it.check_name("error") {
if let Some(err) = it.value_str() {
error = Some(err.to_string());
}
}
}

if let (Some(filename), Some(contents)) = (filename, contents) {
Some((filename, contents))
if let Some(error) = error {
Err(error)
} else if let (Some(filename), Some(contents)) = (filename, contents) {
Ok(Some((filename, contents)))
} else {
None
Ok(None)
}
});
}
}

None
Ok(None)
})
}

Expand Down Expand Up @@ -751,14 +758,21 @@ impl Attributes {
Err(e) => diagnostic.span_err(e.span, e.msg),
}
return None;
} else if let Some((filename, contents)) = Attributes::extract_include(&mi)
{
let line = doc_line;
doc_line += contents.lines().count();
doc_strings.push(DocFragment::Include(line,
attr.span,
filename,
contents));
} else {
match Attributes::extract_include(&mi) {
Ok(Some((filename, contents))) => {
let line = doc_line;
doc_line += contents.lines().count();
doc_strings.push(DocFragment::Include(line,
attr.span,
filename,
contents));
}
Err(e) => {
diagnostic.span_err(attr.span(), &e);
}
Ok(None) => {}
}
}
}
}
Expand Down
55 changes: 33 additions & 22 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,37 +1101,48 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {

let mut buf = vec![];
let filename = self.cx.root_path.join(file.to_string());
let mut include_info = vec![
dummy_spanned(ast::NestedMetaItemKind::MetaItem(
attr::mk_name_value_item_str("file".into(),
file))),
];

match File::open(&filename).and_then(|mut f| f.read_to_end(&mut buf)) {
Ok(..) => {}
Err(e) => {
self.cx.span_warn(at.span,
&format!("couldn't read {}: {}",
filename.display(),
e));
}
}

match String::from_utf8(buf) {
Ok(src) => {
let include_info = vec![
dummy_spanned(ast::NestedMetaItemKind::MetaItem(
attr::mk_name_value_item_str("file".into(),
file))),
include_info.push(
dummy_spanned(ast::NestedMetaItemKind::MetaItem(
attr::mk_name_value_item_str("contents".into(),
(&*src).into()))),
];
attr::mk_name_value_item_str("error".into(),
(&*format!("couldn't read {}: {}",
filename.display(),
e)).into()))));

items.push(dummy_spanned(ast::NestedMetaItemKind::MetaItem(
attr::mk_list_item("include".into(), include_info))));
//make sure the buffer is empty so we can properly skip the next match
buf.clear();
}
Err(_) => {
self.cx.span_warn(at.span,
&format!("{} wasn't a utf-8 file",
filename.display()));
}

if !buf.is_empty() {
match String::from_utf8(buf) {
Ok(src) => {
include_info.push(
dummy_spanned(ast::NestedMetaItemKind::MetaItem(
attr::mk_name_value_item_str("contents".into(),
(&*src).into()))));
}
Err(_) => {
include_info.push(
dummy_spanned(ast::NestedMetaItemKind::MetaItem(
attr::mk_name_value_item_str(
"error".into(),
(&*format!("{} wasn't a utf-8 file",
filename.display())).into()))));
}
}
}

items.push(dummy_spanned(ast::NestedMetaItemKind::MetaItem(
attr::mk_list_item("include".into(), include_info))));
} else {
items.push(noop_fold_meta_list_item(it, self));
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/compile-fail/lint-external-doc-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(external_doc)]
#![deny(external_doc_error)]

#[doc(include = "not-a-file.md")] //~ ERROR: couldn't read
pub struct SomeStruct;

fn main() {}