Skip to content

Commit daf730c

Browse files
committed
Add sanitize_explanation
1 parent 0735031 commit daf730c

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

clippy_lints/src/lib.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -522,30 +522,35 @@ impl LintInfo {
522522
}
523523
}
524524

525-
pub fn explain(name: &str) -> i32 {
526-
let target = format!("clippy::{}", name.to_ascii_uppercase());
527-
528-
if let Some(info) = declared_lints::LINTS.iter().find(|info| info.lint.name == target) {
529-
// Remove tags and hidden code:
530-
let mut explanation = String::with_capacity(128);
531-
let mut in_code = false;
532-
for line in info.explanation.lines().map(|line| line.trim()) {
533-
if let Some(lang) = line.strip_prefix("```") {
534-
let tag = lang.split_once(',').map_or(lang, |(left, _)| left);
535-
if !in_code && matches!(tag, "" | "rust" | "ignore" | "should_panic" | "no_run" | "compile_fail") {
536-
explanation += "```rust\n";
537-
} else {
538-
explanation += line;
539-
explanation.push('\n');
540-
}
541-
in_code = !in_code;
542-
} else if !(in_code && line.starts_with("# ")) {
525+
// Remove code tags and code behind '# 's, as they are not needed for the lint docs and --explain
526+
pub fn sanitize_explanation(raw_docs: &str) -> String {
527+
// Remove tags and hidden code:
528+
let mut explanation = String::with_capacity(128);
529+
let mut in_code = false;
530+
for line in raw_docs.lines().map(|line| line.trim()) {
531+
if let Some(lang) = line.strip_prefix("```") {
532+
let tag = lang.split_once(',').map_or(lang, |(left, _)| left);
533+
if !in_code && matches!(tag, "" | "rust" | "ignore" | "should_panic" | "no_run" | "compile_fail") {
534+
explanation += "```rust\n";
535+
} else {
543536
explanation += line;
544537
explanation.push('\n');
545538
}
539+
in_code = !in_code;
540+
} else if !(in_code && line.starts_with("# ")) {
541+
explanation += line;
542+
explanation.push('\n');
546543
}
544+
}
547545

548-
println!("{}", explanation);
546+
explanation
547+
}
548+
549+
pub fn explain(name: &str) -> i32 {
550+
let target = format!("clippy::{}", name.to_ascii_uppercase());
551+
552+
if let Some(info) = declared_lints::LINTS.iter().find(|info| info.lint.name == target) {
553+
println!("{}", sanitize_explanation(info.explanation));
549554
// Check if the lint has configuration
550555
let mut mdconf = get_configuration_metadata();
551556
let name = name.to_ascii_lowercase();

tests/compile-test.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use cargo_metadata::Message;
66
use cargo_metadata::diagnostic::{Applicability, Diagnostic};
77
use clippy_config::ClippyConfiguration;
8-
use clippy_lints::LintInfo;
98
use clippy_lints::declared_lints::LINTS;
109
use clippy_lints::deprecated_lints::{DEPRECATED, DEPRECATED_VERSION, RENAMED};
10+
use clippy_lints::{LintInfo, sanitize_explanation};
1111
use serde::{Deserialize, Serialize};
1212
use test_utils::IS_RUSTC_TEST_SUITE;
1313
use ui_test::custom_flags::Flag;
@@ -444,7 +444,12 @@ impl DiagnosticCollector {
444444
iter::zip(DEPRECATED, DEPRECATED_VERSION)
445445
.map(|((lint, reason), version)| LintMetadata::new_deprecated(lint, reason, version)),
446446
)
447+
.map(|mut metadata| {
448+
metadata.docs = sanitize_explanation(&metadata.docs);
449+
metadata
450+
})
447451
.collect();
452+
448453
metadata.sort_unstable_by(|a, b| a.id.cmp(&b.id));
449454

450455
let json = serde_json::to_string_pretty(&metadata).unwrap();

0 commit comments

Comments
 (0)