Skip to content

Commit

Permalink
Add lint for intra link resolution failure
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jun 9, 2018
1 parent 40f20b5 commit b000cf0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/librustc/lint/builtin.rs
Expand Up @@ -298,6 +298,12 @@ declare_lint! {
"detects duplicate macro exports"
}

declare_lint! {
pub INTRA_LINK_RESOLUTION_FAILURE,
Warn,
"warn about documentation intra links resolution failure"
}

/// Does nothing as a lint pass, but registers some `Lint`s
/// which are used by other parts of the compiler.
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -351,6 +357,7 @@ impl LintPass for HardwiredLints {
UNSTABLE_NAME_COLLISIONS,
DUPLICATE_ASSOCIATED_TYPE_BINDINGS,
DUPLICATE_MACRO_EXPORTS,
INTRA_LINK_RESOLUTION_FAILURE,
)
}
}
Expand Down
20 changes: 15 additions & 5 deletions src/librustdoc/clean/mod.rs
Expand Up @@ -21,7 +21,7 @@ pub use self::Visibility::{Public, Inherited};

use syntax;
use rustc_target::spec::abi::Abi;
use syntax::ast::{self, AttrStyle, Ident};
use syntax::ast::{self, AttrStyle, NodeId, Ident};
use syntax::attr;
use syntax::codemap::{dummy_spanned, Spanned};
use syntax::feature_gate::UnstableFeatures;
Expand All @@ -46,9 +46,10 @@ use rustc::middle::stability;
use rustc::util::nodemap::{FxHashMap, FxHashSet};
use rustc_typeck::hir_ty_to_ty;
use rustc::infer::region_constraints::{RegionConstraintData, Constraint};
use rustc::lint as lint;

use std::collections::hash_map::Entry;
use std::fmt;

use std::default::Default;
use std::{mem, slice, vec};
use std::iter::{FromIterator, once};
Expand Down Expand Up @@ -1283,10 +1284,16 @@ fn resolution_failure(
link_range.end + code_dox_len,
);

diag = cx.sess().struct_span_warn(sp, &msg);
diag = cx.tcx.struct_span_lint_node(lint::builtin::INTRA_LINK_RESOLUTION_FAILURE,
NodeId::new(0),
sp,
&msg);
diag.span_label(sp, "cannot be resolved, ignoring");
} else {
diag = cx.sess().struct_span_warn(sp, &msg);
diag = cx.tcx.struct_span_lint_node(lint::builtin::INTRA_LINK_RESOLUTION_FAILURE,
NodeId::new(0),
sp,
&msg);

let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1);
let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
Expand All @@ -1303,7 +1310,10 @@ fn resolution_failure(
}
diag
} else {
cx.sess().struct_span_warn(sp, &msg)
cx.tcx.struct_span_lint_node(lint::builtin::INTRA_LINK_RESOLUTION_FAILURE,
NodeId::new(0),
sp,
&msg)
};
diag.emit();
}
Expand Down
28 changes: 24 additions & 4 deletions src/librustdoc/core.rs
Expand Up @@ -17,7 +17,7 @@ use rustc::middle::cstore::CrateStore;
use rustc::middle::privacy::AccessLevels;
use rustc::ty::{self, TyCtxt, AllArenas};
use rustc::hir::map as hir_map;
use rustc::lint;
use rustc::lint::{self, LintPass};
use rustc::session::config::ErrorOutputType;
use rustc::util::nodemap::{FxHashMap, FxHashSet};
use rustc_resolve as resolve;
Expand Down Expand Up @@ -187,16 +187,36 @@ pub fn run_core(search_paths: SearchPaths,
_ => None
};

let warning_lint = lint::builtin::WARNINGS.name_lower();
let intra_link_resolution_failure_name = lint::builtin::INTRA_LINK_RESOLUTION_FAILURE.name;
let warnings_lint_name = lint::builtin::WARNINGS.name;
let lints = lint::builtin::HardwiredLints.get_lints()
.iter()
.filter_map(|lint| {
if lint.name == warnings_lint_name {
None
} else {
let level = if lint.name == intra_link_resolution_failure_name {
lint::Warn
} else {
lint::Allow
};
Some((lint.name_lower(), level))
}
})
.collect::<Vec<_>>();

let host_triple = TargetTriple::from_triple(config::host_triple());
// plays with error output here!
let sessopts = config::Options {
maybe_sysroot,
search_paths,
crate_types: vec![config::CrateTypeRlib],
lint_opts: if !allow_warnings { vec![(warning_lint, lint::Allow)] } else { vec![] },
lint_cap: Some(lint::Allow),
lint_opts: if !allow_warnings {
lints
} else {
vec![]
},
lint_cap: Some(lint::Warn),
cg,
externs,
target_triple: triple.unwrap_or(host_triple),
Expand Down
14 changes: 14 additions & 0 deletions src/test/rustdoc-ui/deny-intra-link-resolution-failure.rs
@@ -0,0 +1,14 @@
// Copyright 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.

#![deny(intra_link_resolution_failure)]

/// [v2] //~ ERROR
pub fn foo() {}
16 changes: 16 additions & 0 deletions src/test/rustdoc-ui/deny-intra-link-resolution-failure.stderr
@@ -0,0 +1,16 @@
warning: [v2] cannot be resolved, ignoring it...
--> src/test/rustdoc-ui/deny-intra-link-resolution-failure.rs:13:1
|
LL | /// [v2] //~ ERROR
| ^^^^^^^^^^^^^^^^^^
|
note: lint level defined here
--> src/test/rustdoc-ui/deny-intra-link-resolution-failure.rs:11:9
|
LL | #![deny(intra_link_resolution_failure)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: the link appears in this line:

[v2] //~ ERROR
^^

0 comments on commit b000cf0

Please sign in to comment.