Skip to content
/ rust Public
forked from rust-lang/rust

Commit effc297

Browse files
authored
Rollup merge of rust-lang#136927 - GuillaumeGomez:add-missing-hashtag-escape, r=notriddle
Correctly escape hashtags when running `invalid_rust_codeblocks` lint Fixes rust-lang#136899. We forgot to use `map_line` when we wrote this lint. r? `@notriddle`
2 parents 4481c53 + 17cf100 commit effc297

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-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
@@ -139,7 +139,7 @@ impl ErrorCodes {
139139
/// Controls whether a line will be hidden or shown in HTML output.
140140
///
141141
/// All lines are used in documentation tests.
142-
enum Line<'a> {
142+
pub(crate) enum Line<'a> {
143143
Hidden(&'a str),
144144
Shown(Cow<'a, str>),
145145
}
@@ -152,20 +152,22 @@ impl<'a> Line<'a> {
152152
}
153153
}
154154

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

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

src/librustdoc/passes/lint/check_code_block_syntax.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Validates syntax inside Rust code blocks (\`\`\`rust).
22
3+
use std::borrow::Cow;
34
use std::sync::Arc;
45

56
use rustc_data_structures::sync::Lock;
@@ -43,7 +44,11 @@ fn check_rust_syntax(
4344

4445
let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
4546
let dcx = DiagCtxt::new(Box::new(emitter)).disable_warnings();
46-
let source = dox[code_block.code].to_owned();
47+
let source = dox[code_block.code]
48+
.lines()
49+
.map(|line| crate::html::markdown::map_line(line).for_code())
50+
.intersperse(Cow::Borrowed("\n"))
51+
.collect::<String>();
4752
let psess = ParseSess::with_dcx(dcx, sm);
4853

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

tests/rustdoc-ui/hashtag-doctest.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This test ensures that `##` are not emitting a warning when generating
2+
// docs with the 2024 edition (or any edition).
3+
// Regression test for <https://github.com/rust-lang/rust/issues/136899>.
4+
5+
//@ check-pass
6+
//@revisions: edition2021 edition2024
7+
//@[edition2021] edition:2021
8+
//@[edition2024] edition:2024
9+
10+
#![deny(warnings)]
11+
12+
//! Test
13+
//!
14+
//! ```
15+
//! ##[allow(dead_code)]
16+
//! println!("hello world");
17+
//! ```

0 commit comments

Comments
 (0)