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