Skip to main content

cargo/diagnostics/rules/
non_kebab_case_packages.rs

1use std::path::Path;
2
3use cargo_util_terminal::report::AnnotationKind;
4use cargo_util_terminal::report::Group;
5use cargo_util_terminal::report::Level;
6use cargo_util_terminal::report::Origin;
7use cargo_util_terminal::report::Patch;
8use cargo_util_terminal::report::Snippet;
9use tracing::instrument;
10
11use super::RESTRICTION;
12use crate::CargoResult;
13use crate::GlobalContext;
14use crate::core::Package;
15use crate::core::Workspace;
16use crate::diagnostics::DiagnosticStats;
17use crate::diagnostics::Lint;
18use crate::diagnostics::LintLevel;
19use crate::diagnostics::LintLevelProduct;
20use crate::diagnostics::LintLevelSource;
21use crate::diagnostics::get_key_value_span;
22use crate::diagnostics::rel_cwd_manifest_path;
23
24pub static LINT: &Lint = &Lint {
25    name: "non_kebab_case_packages",
26    desc: "packages should have a kebab-case name",
27    primary_group: &RESTRICTION,
28    msrv: None,
29    feature_gate: None,
30    docs: Some(
31        r#"
32### What it does
33
34Detect package names that are not kebab-case.
35
36### Why it is bad
37
38Having multiple naming styles within a workspace can be confusing.
39
40### Drawbacks
41
42Users have to mentally translate package names to namespaces in Rust.
43
44### Example
45
46```toml
47[package]
48name = "foo_bar"
49```
50
51Should be written as:
52
53```toml
54[package]
55name = "foo-bar"
56```
57"#,
58    ),
59};
60
61#[instrument(skip_all)]
62pub(crate) fn lint_package(
63    _ws: &Workspace<'_>,
64    pkg: &Package,
65    manifest_path: &Path,
66    level: LintLevelProduct,
67    stats: &mut DiagnosticStats,
68    gctx: &GlobalContext,
69) -> CargoResult<()> {
70    let LintLevelProduct {
71        level: lint_level,
72        source,
73    } = level;
74
75    let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
76
77    lint_package_inner(pkg, &manifest_path, lint_level, source, stats, gctx)
78}
79
80fn lint_package_inner(
81    pkg: &Package,
82    manifest_path: &str,
83    lint_level: LintLevel,
84    source: LintLevelSource,
85    stats: &mut DiagnosticStats,
86    gctx: &GlobalContext,
87) -> CargoResult<()> {
88    let manifest = pkg.manifest();
89
90    let original_name = &*manifest.name();
91    let kebab_case = heck::ToKebabCase::to_kebab_case(original_name);
92    if kebab_case == original_name {
93        return Ok(());
94    }
95
96    let document = manifest.document();
97    let contents = manifest.contents();
98    let level = lint_level.to_diagnostic_level();
99    let emitted_source = LINT.emitted_source(lint_level, source);
100
101    let mut primary = Group::with_title(level.primary_title(LINT.desc));
102    if let Some(document) = document
103        && let Some(contents) = contents
104        && let Some(span) = get_key_value_span(document, &["package", "name"])
105    {
106        primary = primary.element(
107            Snippet::source(contents)
108                .path(manifest_path)
109                .annotation(AnnotationKind::Primary.span(span.value)),
110        );
111    } else {
112        primary = primary.element(Origin::path(manifest_path));
113    }
114    primary = primary.element(Level::NOTE.message(emitted_source));
115    let mut report = vec![primary];
116    if let Some(document) = document
117        && let Some(contents) = contents
118        && let Some(span) = get_key_value_span(document, &["package", "name"])
119    {
120        let mut help =
121            Group::with_title(Level::HELP.secondary_title(
122                "to change the package name to kebab case, convert `package.name`",
123            ));
124        help = help.element(
125            Snippet::source(contents)
126                .path(manifest_path)
127                .patch(Patch::new(span.value, format!("\"{kebab_case}\""))),
128        );
129        report.push(help);
130    } else {
131        let path = pkg.manifest_path();
132        let display_path = path.as_os_str().to_string_lossy();
133        let end = display_path.len() - if display_path.ends_with(".rs") { 3 } else { 0 };
134        let start = path
135            .parent()
136            .map(|p| {
137                let p = p.as_os_str().to_string_lossy();
138                // Account for trailing slash that was removed
139                p.len() + if p.is_empty() { 0 } else { 1 }
140            })
141            .unwrap_or(0);
142        let help = Level::HELP
143            .secondary_title("to change the package name to kebab case, convert the file stem")
144            .element(Snippet::source(display_path).patch(Patch::new(start..end, kebab_case)));
145        report.push(help);
146    }
147
148    stats.record_lint(lint_level);
149    gctx.shell().print_report(&report, lint_level.force())?;
150
151    Ok(())
152}