|
| 1 | +//! Detects specific markdown syntax that's different between pulldown-cmark |
| 2 | +//! 0.9 and 0.11. |
| 3 | +//! |
| 4 | +//! This is a mitigation for old parser bugs that affected some |
| 5 | +//! real crates' docs. The old parser claimed to comply with CommonMark, |
| 6 | +//! but it did not. These warnings will eventually be removed, |
| 7 | +//! though some of them may become Clippy lints. |
| 8 | +//! |
| 9 | +//! <https://github.com/rust-lang/rust/pull/121659#issuecomment-1992752820> |
| 10 | +//! |
| 11 | +//! <https://rustc-dev-guide.rust-lang.org/bug-fix-procedure.html#add-the-lint-to-the-list-of-removed-lists> |
| 12 | +
|
| 13 | +use crate::clean::Item; |
| 14 | +use crate::core::DocContext; |
| 15 | +use pulldown_cmark as cmarkn; |
| 16 | +use pulldown_cmark_old as cmarko; |
| 17 | +use rustc_lint_defs::Applicability; |
| 18 | +use rustc_resolve::rustdoc::source_span_for_markdown_range; |
| 19 | +use std::collections::{BTreeMap, BTreeSet}; |
| 20 | + |
| 21 | +pub(crate) fn visit_item(cx: &DocContext<'_>, item: &Item) { |
| 22 | + let tcx = cx.tcx; |
| 23 | + let Some(hir_id) = DocContext::as_local_hir_id(tcx, item.item_id) else { |
| 24 | + // If non-local, no need to check anything. |
| 25 | + return; |
| 26 | + }; |
| 27 | + |
| 28 | + let dox = item.doc_value(); |
| 29 | + if dox.is_empty() { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // P1: unintended strikethrough was fixed by requiring single-tildes to flank |
| 34 | + // the same way underscores do, so nothing is done here |
| 35 | + |
| 36 | + // P2: block quotes without following space parsed wrong |
| 37 | + // |
| 38 | + // This is the set of starting points for block quotes with no space after |
| 39 | + // the `>`. It is populated by the new parser, and if the old parser fails to |
| 40 | + // clear it out, it'll produce a warning. |
| 41 | + let mut spaceless_block_quotes = BTreeSet::new(); |
| 42 | + |
| 43 | + // P3: missing footnote references |
| 44 | + // |
| 45 | + // This is populated by listening for FootnoteReference from |
| 46 | + // the new parser and old parser. |
| 47 | + let mut missing_footnote_references = BTreeMap::new(); |
| 48 | + let mut found_footnote_references = BTreeSet::new(); |
| 49 | + |
| 50 | + // populate problem cases from new parser |
| 51 | + { |
| 52 | + pub fn main_body_opts_new() -> cmarkn::Options { |
| 53 | + cmarkn::Options::ENABLE_TABLES |
| 54 | + | cmarkn::Options::ENABLE_FOOTNOTES |
| 55 | + | cmarkn::Options::ENABLE_STRIKETHROUGH |
| 56 | + | cmarkn::Options::ENABLE_TASKLISTS |
| 57 | + | cmarkn::Options::ENABLE_SMART_PUNCTUATION |
| 58 | + } |
| 59 | + let mut parser_new = cmarkn::Parser::new_ext(&dox, main_body_opts_new()).into_offset_iter(); |
| 60 | + while let Some((event, span)) = parser_new.next() { |
| 61 | + if let cmarkn::Event::Start(cmarkn::Tag::BlockQuote(_)) = event { |
| 62 | + if !dox[span.clone()].starts_with("> ") { |
| 63 | + spaceless_block_quotes.insert(span.start); |
| 64 | + } |
| 65 | + } |
| 66 | + if let cmarkn::Event::FootnoteReference(_) = event { |
| 67 | + found_footnote_references.insert(span.start + 1); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // remove cases where they don't actually differ |
| 73 | + { |
| 74 | + pub fn main_body_opts_old() -> cmarko::Options { |
| 75 | + cmarko::Options::ENABLE_TABLES |
| 76 | + | cmarko::Options::ENABLE_FOOTNOTES |
| 77 | + | cmarko::Options::ENABLE_STRIKETHROUGH |
| 78 | + | cmarko::Options::ENABLE_TASKLISTS |
| 79 | + | cmarko::Options::ENABLE_SMART_PUNCTUATION |
| 80 | + } |
| 81 | + let mut parser_old = cmarko::Parser::new_ext(&dox, main_body_opts_old()).into_offset_iter(); |
| 82 | + while let Some((event, span)) = parser_old.next() { |
| 83 | + if let cmarko::Event::Start(cmarko::Tag::BlockQuote) = event { |
| 84 | + if !dox[span.clone()].starts_with("> ") { |
| 85 | + spaceless_block_quotes.remove(&span.start); |
| 86 | + } |
| 87 | + } |
| 88 | + if let cmarko::Event::FootnoteReference(_) = event { |
| 89 | + if !found_footnote_references.contains(&(span.start + 1)) { |
| 90 | + missing_footnote_references.insert(span.start + 1, span); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + for start in spaceless_block_quotes { |
| 97 | + let (span, precise) = |
| 98 | + source_span_for_markdown_range(tcx, &dox, &(start..start + 1), &item.attrs.doc_strings) |
| 99 | + .map(|span| (span, true)) |
| 100 | + .unwrap_or_else(|| (item.attr_span(tcx), false)); |
| 101 | + |
| 102 | + tcx.node_span_lint(crate::lint::UNPORTABLE_MARKDOWN, hir_id, span, |lint| { |
| 103 | + lint.primary_message("unportable markdown"); |
| 104 | + lint.help(format!("confusing block quote with no space after the `>` marker")); |
| 105 | + if precise { |
| 106 | + lint.span_suggestion( |
| 107 | + span.shrink_to_hi(), |
| 108 | + "if the quote is intended, add a space", |
| 109 | + " ", |
| 110 | + Applicability::MaybeIncorrect, |
| 111 | + ); |
| 112 | + lint.span_suggestion( |
| 113 | + span.shrink_to_lo(), |
| 114 | + "if it should not be a quote, escape it", |
| 115 | + "\\", |
| 116 | + Applicability::MaybeIncorrect, |
| 117 | + ); |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + for (_caret, span) in missing_footnote_references { |
| 122 | + let (ref_span, precise) = |
| 123 | + source_span_for_markdown_range(tcx, &dox, &span, &item.attrs.doc_strings) |
| 124 | + .map(|span| (span, true)) |
| 125 | + .unwrap_or_else(|| (item.attr_span(tcx), false)); |
| 126 | + |
| 127 | + tcx.node_span_lint(crate::lint::UNPORTABLE_MARKDOWN, hir_id, ref_span, |lint| { |
| 128 | + lint.primary_message("unportable markdown"); |
| 129 | + if precise { |
| 130 | + lint.span_suggestion( |
| 131 | + ref_span.shrink_to_lo(), |
| 132 | + "if it should not be a footnote, escape it", |
| 133 | + "\\", |
| 134 | + Applicability::MaybeIncorrect, |
| 135 | + ); |
| 136 | + } |
| 137 | + if dox.as_bytes().get(span.end) == Some(&b'[') { |
| 138 | + lint.help("confusing footnote reference and link"); |
| 139 | + if precise { |
| 140 | + lint.span_suggestion( |
| 141 | + ref_span.shrink_to_hi(), |
| 142 | + "if the footnote is intended, add a space", |
| 143 | + " ", |
| 144 | + Applicability::MaybeIncorrect, |
| 145 | + ); |
| 146 | + } else { |
| 147 | + lint.help("there should be a space between the link and the footnote"); |
| 148 | + } |
| 149 | + } |
| 150 | + }); |
| 151 | + } |
| 152 | +} |
0 commit comments