Skip to main content

cargo/diagnostics/rules/
non_kebab_case_packages.rs

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