about summary refs log tree commit diff
diff options
context:
space:
mode:
authorblyxyas <blyxyas@gmail.com>2024-09-26 00:23:38 +0200
committerblyxyas <blyxyas@gmail.com>2024-09-26 00:23:38 +0200
commitdaf730caedb638696ee704e22942617cf5848e5f (patch)
treee4275414d5a4995c1f4d0b39bf08e24fa68639ed
parent0735031be94cd4f6a102e83e72630fae04a18090 (diff)
downloadrust-daf730caedb638696ee704e22942617cf5848e5f.tar.gz
rust-daf730caedb638696ee704e22942617cf5848e5f.zip
Add `sanitize_explanation`
-rw-r--r--clippy_lints/src/lib.rs43
-rw-r--r--tests/compile-test.rs7
2 files changed, 30 insertions, 20 deletions
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index e927d9a2d5b..0bfbb92dfda 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -522,30 +522,35 @@ impl LintInfo {
     }
 }
 
-pub fn explain(name: &str) -> i32 {
-    let target = format!("clippy::{}", name.to_ascii_uppercase());
-
-    if let Some(info) = declared_lints::LINTS.iter().find(|info| info.lint.name == target) {
-        // Remove tags and hidden code:
-        let mut explanation = String::with_capacity(128);
-        let mut in_code = false;
-        for line in info.explanation.lines().map(|line| line.trim()) {
-            if let Some(lang) = line.strip_prefix("```") {
-                let tag = lang.split_once(',').map_or(lang, |(left, _)| left);
-                if !in_code && matches!(tag, "" | "rust" | "ignore" | "should_panic" | "no_run" | "compile_fail") {
-                    explanation += "```rust\n";
-                } else {
-                    explanation += line;
-                    explanation.push('\n');
-                }
-                in_code = !in_code;
-            } else if !(in_code && line.starts_with("# ")) {
+// Remove code tags and code behind '# 's, as they are not needed for the lint docs and --explain
+pub fn sanitize_explanation(raw_docs: &str) -> String {
+    // Remove tags and hidden code:
+    let mut explanation = String::with_capacity(128);
+    let mut in_code = false;
+    for line in raw_docs.lines().map(|line| line.trim()) {
+        if let Some(lang) = line.strip_prefix("```") {
+            let tag = lang.split_once(',').map_or(lang, |(left, _)| left);
+            if !in_code && matches!(tag, "" | "rust" | "ignore" | "should_panic" | "no_run" | "compile_fail") {
+                explanation += "```rust\n";
+            } else {
                 explanation += line;
                 explanation.push('\n');
             }
+            in_code = !in_code;
+        } else if !(in_code && line.starts_with("# ")) {
+            explanation += line;
+            explanation.push('\n');
         }
+    }
 
-        println!("{}", explanation);
+    explanation
+}
+
+pub fn explain(name: &str) -> i32 {
+    let target = format!("clippy::{}", name.to_ascii_uppercase());
+
+    if let Some(info) = declared_lints::LINTS.iter().find(|info| info.lint.name == target) {
+        println!("{}", sanitize_explanation(info.explanation));
         // Check if the lint has configuration
         let mut mdconf = get_configuration_metadata();
         let name = name.to_ascii_lowercase();
diff --git a/tests/compile-test.rs b/tests/compile-test.rs
index af2aa519257..23dd41235bd 100644
--- a/tests/compile-test.rs
+++ b/tests/compile-test.rs
@@ -5,9 +5,9 @@
 use cargo_metadata::Message;
 use cargo_metadata::diagnostic::{Applicability, Diagnostic};
 use clippy_config::ClippyConfiguration;
-use clippy_lints::LintInfo;
 use clippy_lints::declared_lints::LINTS;
 use clippy_lints::deprecated_lints::{DEPRECATED, DEPRECATED_VERSION, RENAMED};
+use clippy_lints::{LintInfo, sanitize_explanation};
 use serde::{Deserialize, Serialize};
 use test_utils::IS_RUSTC_TEST_SUITE;
 use ui_test::custom_flags::Flag;
@@ -444,7 +444,12 @@ impl DiagnosticCollector {
                     iter::zip(DEPRECATED, DEPRECATED_VERSION)
                         .map(|((lint, reason), version)| LintMetadata::new_deprecated(lint, reason, version)),
                 )
+                .map(|mut metadata| {
+                    metadata.docs = sanitize_explanation(&metadata.docs);
+                    metadata
+                })
                 .collect();
+
             metadata.sort_unstable_by(|a, b| a.id.cmp(&b.id));
 
             let json = serde_json::to_string_pretty(&metadata).unwrap();