Skip to content

Commit 206904c

Browse files
committed
add a broken_html_links lint
1 parent 3a242c2 commit 206904c

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/librustdoc/html/render/context.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::rc::Rc;
66
use std::sync::mpsc::{channel, Receiver};
77

88
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
9+
use rustc_hir::HirId;
910
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1011
use rustc_middle::ty::TyCtxt;
1112
use rustc_session::Session;
@@ -33,6 +34,7 @@ use crate::html::format::{join_with_double_colon, Buffer};
3334
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
3435
use crate::html::url_parts_builder::UrlPartsBuilder;
3536
use crate::html::{layout, sources, static_files};
37+
use crate::lint::BROKEN_HTML_LINKS;
3638
use crate::scrape_examples::AllCallLocations;
3739
use crate::try_err;
3840

@@ -174,8 +176,14 @@ impl<'tcx> Context<'tcx> {
174176

175177
let path = self.dst.canonicalize().unwrap();
176178
let errors: Vec<_> = unavailable_urls(&path, &CheckContext::default()).collect();
179+
let tcx = self.tcx();
177180
for err in errors {
178-
self.sess().struct_warn(&err.to_string().replace('\t', " ")).emit();
181+
// self.sess().struct_warn(&err.to_string().replace('\t', " ")).emit();
182+
let msg = err.to_string().replace('\t', " ");
183+
// TODO: map files back to items
184+
let hir_id: HirId = todo!();
185+
let span = tcx.def_span(tcx.hir().local_def_id(hir_id));
186+
self.tcx().struct_span_lint_hir(BROKEN_HTML_LINKS, hir_id, span, msg, |err| err);
179187
}
180188
}
181189

src/librustdoc/lint.rs

+11
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ declare_rustdoc_lint! {
174174
"codeblock could not be parsed as valid Rust or is empty"
175175
}
176176

177+
declare_rustdoc_lint! {
178+
/// The `broken_html_links` lint detects generated links that are broken.
179+
/// Unlike `broken_intra_doc_links`, this also works for manual links.
180+
/// This is a `rustdoc` only lint, see the documentation in the [rustdoc book].
181+
///
182+
/// [rustdoc book]: ../../../rustdoc/lints.html#broken_html_links
183+
BROKEN_HTML_LINKS,
184+
Warn,
185+
"generated link pointed to an invalid path"
186+
}
187+
177188
pub(crate) static RUSTDOC_LINTS: Lazy<Vec<&'static Lint>> = Lazy::new(|| {
178189
vec![
179190
BROKEN_INTRA_DOC_LINKS,

src/test/rustdoc/broken-link.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//! [broken link](./not-here.html)

0 commit comments

Comments
 (0)