Improve metadata code block parsing

This commit is contained in:
Cameron Steffen 2021-06-14 14:10:19 -05:00
parent 1ee99d9bf5
commit a557f37bb4

View File

@ -9,6 +9,7 @@
//! a simple mistake) //! a simple mistake)
use if_chain::if_chain; use if_chain::if_chain;
use rustc_ast as ast;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_hir::{ use rustc_hir::{
self as hir, def::DefKind, intravisit, intravisit::Visitor, ExprKind, Item, ItemKind, Mutability, QPath, 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` /// Would result in `Hello world!\n=^.^=\n`
fn extract_attr_docs(cx: &LateContext<'_>, item: &Item<'_>) -> Option<String> { fn extract_attr_docs(cx: &LateContext<'_>, item: &Item<'_>) -> Option<String> {
cx.tcx let attrs = cx.tcx.hir().attrs(item.hir_id());
.hir() let mut lines = attrs.iter().filter_map(ast::Attribute::doc_str);
.attrs(item.hir_id()) let mut docs = String::from(&*lines.next()?.as_str());
.iter() let mut in_code_block = false;
.filter_map(|x| x.doc_str().map(|sym| sym.as_str().to_string())) for line in lines {
.reduce(|mut acc, sym| { docs.push('\n');
acc.push_str(&sym); let line = line.as_str();
acc.push('\n'); let line = &*line;
acc 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( fn get_lint_group_and_level_or_lint(