Skip to content

Commit 7ec3695

Browse files
Correctly escape hashtags when running invalid_rust_codeblocks lint
1 parent 8c04e39 commit 7ec3695

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/librustdoc/doctest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ impl IndividualTestOptions {
786786
/// [`clean`]: crate::clean
787787
/// [`run_merged_tests`]: crate::doctest::runner::DocTestRunner::run_merged_tests
788788
/// [`generate_unique_doctest`]: crate::doctest::make::DocTestBuilder::generate_unique_doctest
789+
#[derive(Debug)]
789790
pub(crate) struct ScrapedDocTest {
790791
filename: FileName,
791792
line: usize,

src/librustdoc/html/markdown.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl ErrorCodes {
140140
/// Controls whether a line will be hidden or shown in HTML output.
141141
///
142142
/// All lines are used in documentation tests.
143-
enum Line<'a> {
143+
pub(crate) enum Line<'a> {
144144
Hidden(&'a str),
145145
Shown(Cow<'a, str>),
146146
}
@@ -153,20 +153,22 @@ impl<'a> Line<'a> {
153153
}
154154
}
155155

156-
fn for_code(self) -> Cow<'a, str> {
156+
pub(crate) fn for_code(self) -> Cow<'a, str> {
157157
match self {
158158
Line::Shown(l) => l,
159159
Line::Hidden(l) => Cow::Borrowed(l),
160160
}
161161
}
162162
}
163163

164+
/// This function is used to handle the "hidden lines" (ie starting with `#`) in
165+
/// doctests. It also transforms `##` back into `#`.
164166
// FIXME: There is a minor inconsistency here. For lines that start with ##, we
165167
// have no easy way of removing a potential single space after the hashes, which
166168
// is done in the single # case. This inconsistency seems okay, if non-ideal. In
167169
// order to fix it we'd have to iterate to find the first non-# character, and
168170
// then reallocate to remove it; which would make us return a String.
169-
fn map_line(s: &str) -> Line<'_> {
171+
pub(crate) fn map_line(s: &str) -> Line<'_> {
170172
let trimmed = s.trim();
171173
if trimmed.starts_with("##") {
172174
Line::Shown(Cow::Owned(s.replacen("##", "#", 1)))

src/librustdoc/passes/lint/check_code_block_syntax.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ fn check_rust_syntax(
4343

4444
let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
4545
let dcx = DiagCtxt::new(Box::new(emitter)).disable_warnings();
46-
let source = dox[code_block.code].to_owned();
46+
let source = dox[code_block.code]
47+
.lines()
48+
.map(|line| crate::html::markdown::map_line(line).for_code())
49+
.collect::<String>();
4750
let psess = ParseSess::with_dcx(dcx, sm);
4851

4952
let edition = code_block.lang_string.edition.unwrap_or_else(|| cx.tcx.sess.edition());

0 commit comments

Comments
 (0)