|
| 1 | +use super::{span_of_attrs, Pass}; |
| 2 | +use crate::clean::*; |
| 3 | +use crate::core::DocContext; |
| 4 | +use crate::fold::DocFolder; |
| 5 | +use crate::html::markdown::opts; |
| 6 | +use pulldown_cmark::{Event, Parser, Tag}; |
| 7 | +use rustc_feature::UnstableFeatures; |
| 8 | +use rustc_session::lint; |
| 9 | + |
| 10 | +pub const CHECK_AUTOMATIC_LINKS: Pass = Pass { |
| 11 | + name: "check-automatic-links", |
| 12 | + run: check_automatic_links, |
| 13 | + description: "detects URLS/email addresses that could be written using brackets", |
| 14 | +}; |
| 15 | + |
| 16 | +struct AutomaticLinksLinter<'a, 'tcx> { |
| 17 | + cx: &'a DocContext<'tcx>, |
| 18 | +} |
| 19 | + |
| 20 | +impl<'a, 'tcx> AutomaticLinksLinter<'a, 'tcx> { |
| 21 | + fn new(cx: &'a DocContext<'tcx>) -> Self { |
| 22 | + AutomaticLinksLinter { cx } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +pub fn check_automatic_links(krate: Crate, cx: &DocContext<'_>) -> Crate { |
| 27 | + if !UnstableFeatures::from_environment().is_nightly_build() { |
| 28 | + krate |
| 29 | + } else { |
| 30 | + let mut coll = AutomaticLinksLinter::new(cx); |
| 31 | + |
| 32 | + coll.fold_crate(krate) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl<'a, 'tcx> DocFolder for AutomaticLinksLinter<'a, 'tcx> { |
| 37 | + fn fold_item(&mut self, item: Item) -> Option<Item> { |
| 38 | + let hir_id = match self.cx.as_local_hir_id(item.def_id) { |
| 39 | + Some(hir_id) => hir_id, |
| 40 | + None => { |
| 41 | + // If non-local, no need to check anything. |
| 42 | + return self.fold_item_recur(item); |
| 43 | + } |
| 44 | + }; |
| 45 | + let dox = item.attrs.collapsed_doc_value().unwrap_or_default(); |
| 46 | + if !dox.is_empty() { |
| 47 | + let cx = &self.cx; |
| 48 | + |
| 49 | + let p = Parser::new_ext(&dox, opts()).into_offset_iter(); |
| 50 | + |
| 51 | + let mut title = String::new(); |
| 52 | + let mut in_link = false; |
| 53 | + |
| 54 | + for (event, range) in p { |
| 55 | + match event { |
| 56 | + Event::Start(Tag::Link(..)) => in_link = true, |
| 57 | + Event::End(Tag::Link(_, url, _)) => { |
| 58 | + in_link = false; |
| 59 | + if url.as_ref() != title { |
| 60 | + continue; |
| 61 | + } |
| 62 | + let sp = match super::source_span_for_markdown_range( |
| 63 | + cx, |
| 64 | + &dox, |
| 65 | + &range, |
| 66 | + &item.attrs, |
| 67 | + ) { |
| 68 | + Some(sp) => sp, |
| 69 | + None => span_of_attrs(&item.attrs).unwrap_or(item.source.span()), |
| 70 | + }; |
| 71 | + cx.tcx.struct_span_lint_hir( |
| 72 | + lint::builtin::AUTOMATIC_LINKS, |
| 73 | + hir_id, |
| 74 | + sp, |
| 75 | + |lint| { |
| 76 | + lint.build("Unneeded long form for URL") |
| 77 | + .help(&format!("Try with `<{}>` instead", url)) |
| 78 | + .emit() |
| 79 | + }, |
| 80 | + ); |
| 81 | + title.clear(); |
| 82 | + } |
| 83 | + Event::Text(s) if in_link => { |
| 84 | + title.push_str(&s); |
| 85 | + } |
| 86 | + _ => {} |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + self.fold_item_recur(item) |
| 92 | + } |
| 93 | +} |
0 commit comments