diff options
| author | bors <bors@rust-lang.org> | 2021-06-15 21:44:00 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-06-15 21:44:00 +0000 |
| commit | 7c9da3c1e4f22504efbc198d5428ea2ac963b924 (patch) | |
| tree | 51cc96c29d546c35e1fe812bd12c5a13fb792a2d | |
| parent | 1ee99d9bf54f04bd41a684d75a4536fbcb6e2663 (diff) | |
| parent | a557f37bb4e03bc9ce75e4e22dea68c17f73f01b (diff) | |
| download | rust-7c9da3c1e4f22504efbc198d5428ea2ac963b924.tar.gz rust-7c9da3c1e4f22504efbc198d5428ea2ac963b924.zip | |
Auto merge of #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.
| -rw-r--r-- | clippy_lints/src/utils/internal_lints/metadata_collector.rs | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/clippy_lints/src/utils/internal_lints/metadata_collector.rs b/clippy_lints/src/utils/internal_lints/metadata_collector.rs index 46af03663b8..c980a0246fd 100644 --- a/clippy_lints/src/utils/internal_lints/metadata_collector.rs +++ b/clippy_lints/src/utils/internal_lints/metadata_collector.rs @@ -9,6 +9,7 @@ //! a simple mistake) use if_chain::if_chain; +use rustc_ast as ast; use rustc_data_structures::fx::FxHashMap; use rustc_hir::{ 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 /// /// Would result in `Hello world!\n=^.^=\n` fn extract_attr_docs(cx: &LateContext<'_>, item: &Item<'_>) -> Option<String> { - cx.tcx - .hir() - .attrs(item.hir_id()) - .iter() - .filter_map(|x| x.doc_str().map(|sym| sym.as_str().to_string())) - .reduce(|mut acc, sym| { - acc.push_str(&sym); - acc.push('\n'); - acc - }) + let attrs = cx.tcx.hir().attrs(item.hir_id()); + let mut lines = attrs.iter().filter_map(ast::Attribute::doc_str); + let mut docs = String::from(&*lines.next()?.as_str()); + let mut in_code_block = false; + for line in lines { + docs.push('\n'); + let line = line.as_str(); + let line = &*line; + if let Some(info) = line.trim_start().strip_prefix("```") { + in_code_block = !in_code_block; + if in_code_block { + let lang = info + .trim() + .split(',') + // remove rustdoc directives + .find(|&s| !matches!(s, "" | "ignore" | "no_run" | "should_panic")) + // if no language is present, fill in "rust" + .unwrap_or("rust"); + docs.push_str("```"); + docs.push_str(lang); + continue; + } + } + docs.push_str(line); + } + Some(docs) } fn get_lint_group_and_level_or_lint( |
