1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use std::fmt::{self, Write};

#[derive(Debug, Clone, Default)]
pub struct ClippyConfiguration {
    pub name: String,
    pub default: String,
    pub lints: Vec<String>,
    pub doc: String,
    pub deprecation_reason: Option<&'static str>,
}

impl fmt::Display for ClippyConfiguration {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "- `{}`: {}", self.name, self.doc)?;
        if !self.default.is_empty() {
            write!(f, " (default: `{}`)", self.default)?;
        }
        Ok(())
    }
}

impl ClippyConfiguration {
    pub fn new(
        name: &'static str,
        default: String,
        doc_comment: &'static str,
        deprecation_reason: Option<&'static str>,
    ) -> Self {
        let (mut lints, doc) = parse_config_field_doc(doc_comment)
            .unwrap_or_else(|| (vec![], "[ERROR] MALFORMED DOC COMMENT".to_string()));

        lints.sort();

        Self {
            name: to_kebab(name),
            lints,
            doc,
            default,
            deprecation_reason,
        }
    }

    pub fn to_markdown_paragraph(&self) -> String {
        let mut out = format!(
            "## `{}`\n{}\n\n",
            self.name,
            self.doc
                .lines()
                .map(|line| line.strip_prefix("    ").unwrap_or(line))
                .collect::<Vec<_>>()
                .join("\n"),
        );

        if !self.default.is_empty() {
            write!(out, "**Default Value:** `{}`\n\n", self.default).unwrap();
        }

        write!(
            out,
            "---\n**Affected lints:**\n{}\n\n",
            self.lints
                .iter()
                .map(|name| name.to_string().split_whitespace().next().unwrap().to_string())
                .map(|name| format!("* [`{name}`](https://rust-lang.github.io/rust-clippy/master/index.html#{name})"))
                .collect::<Vec<_>>()
                .join("\n"),
        )
        .unwrap();

        out
    }

    pub fn to_markdown_link(&self) -> String {
        const BOOK_CONFIGS_PATH: &str = "https://doc.rust-lang.org/clippy/lint_configuration.html";
        format!("[`{}`]: {BOOK_CONFIGS_PATH}#{}", self.name, self.name)
    }
}

/// This parses the field documentation of the config struct.
///
/// ```rust, ignore
/// parse_config_field_doc(cx, "Lint: LINT_NAME_1, LINT_NAME_2. Papa penguin, papa penguin")
/// ```
///
/// Would yield:
/// ```rust, ignore
/// Some(["lint_name_1", "lint_name_2"], "Papa penguin, papa penguin")
/// ```
fn parse_config_field_doc(doc_comment: &str) -> Option<(Vec<String>, String)> {
    const DOC_START: &str = " Lint: ";
    if doc_comment.starts_with(DOC_START)
        && let Some(split_pos) = doc_comment.find('.')
    {
        let mut doc_comment = doc_comment.to_string();
        let mut documentation = doc_comment.split_off(split_pos);

        // Extract lints
        doc_comment.make_ascii_lowercase();
        let lints: Vec<String> = doc_comment
            .split_off(DOC_START.len())
            .lines()
            .next()
            .unwrap()
            .split(", ")
            .map(str::to_string)
            .collect();

        // Format documentation correctly
        // split off leading `.` from lint name list and indent for correct formatting
        documentation = documentation.trim_start_matches('.').trim().replace("\n ", "\n    ");

        Some((lints, documentation))
    } else {
        None
    }
}

/// Transforms a given `snake_case_string` to a tasty `kebab-case-string`
fn to_kebab(config_name: &str) -> String {
    config_name.replace('_', "-")
}