Skip to content

Commit 7c9da3c

Browse files
committed
Auto merge of rust-lang#7352 - camsteffen:metadata-syntax, r=xFrednet
Fix metadata code block syntax highlighting changelog: none Fixes code fence block in the metadata collector to change ` ``` ` or ` ```ignore` or ` ```rust,ignore` to ` ```rust`. This makes syntax highlighting work more consistently on the website.
2 parents 1ee99d9 + a557f37 commit 7c9da3c

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

clippy_lints/src/utils/internal_lints/metadata_collector.rs

+27-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! a simple mistake)
1010
1111
use if_chain::if_chain;
12+
use rustc_ast as ast;
1213
use rustc_data_structures::fx::FxHashMap;
1314
use rustc_hir::{
1415
self as hir, def::DefKind, intravisit, intravisit::Visitor, ExprKind, Item, ItemKind, Mutability, QPath,
@@ -485,16 +486,32 @@ fn extract_attr_docs_or_lint(cx: &LateContext<'_>, item: &Item<'_>) -> Option<St
485486
///
486487
/// Would result in `Hello world!\n=^.^=\n`
487488
fn extract_attr_docs(cx: &LateContext<'_>, item: &Item<'_>) -> Option<String> {
488-
cx.tcx
489-
.hir()
490-
.attrs(item.hir_id())
491-
.iter()
492-
.filter_map(|x| x.doc_str().map(|sym| sym.as_str().to_string()))
493-
.reduce(|mut acc, sym| {
494-
acc.push_str(&sym);
495-
acc.push('\n');
496-
acc
497-
})
489+
let attrs = cx.tcx.hir().attrs(item.hir_id());
490+
let mut lines = attrs.iter().filter_map(ast::Attribute::doc_str);
491+
let mut docs = String::from(&*lines.next()?.as_str());
492+
let mut in_code_block = false;
493+
for line in lines {
494+
docs.push('\n');
495+
let line = line.as_str();
496+
let line = &*line;
497+
if let Some(info) = line.trim_start().strip_prefix("```") {
498+
in_code_block = !in_code_block;
499+
if in_code_block {
500+
let lang = info
501+
.trim()
502+
.split(',')
503+
// remove rustdoc directives
504+
.find(|&s| !matches!(s, "" | "ignore" | "no_run" | "should_panic"))
505+
// if no language is present, fill in "rust"
506+
.unwrap_or("rust");
507+
docs.push_str("```");
508+
docs.push_str(lang);
509+
continue;
510+
}
511+
}
512+
docs.push_str(line);
513+
}
514+
Some(docs)
498515
}
499516

500517
fn get_lint_group_and_level_or_lint(

0 commit comments

Comments
 (0)