cargo/diagnostics/rules/
non_kebab_case_features.rs1use 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::diagnostics::Lint;
15use crate::diagnostics::LintLevel;
16use crate::diagnostics::LintLevelProduct;
17use crate::diagnostics::LintLevelSource;
18use crate::diagnostics::ScopedDiagnosticStats;
19use crate::diagnostics::get_key_value_span;
20use crate::diagnostics::workspace_rel_path;
21use crate::workspace::Package;
22use crate::workspace::Workspace;
23
24pub static LINT: &Lint = &Lint {
25 name: "non_kebab_case_features",
26 primary_group: &RESTRICTION,
27 msrv: None,
28 feature_gate: None,
29 docs: Some(
30 r#"
31### What it does
32
33Detect feature names that are not kebab-case.
34
35### Why restrict this?
36
37Having multiple naming styles within a workspace can be confusing.
38
39### Drawbacks
40
41Users would expect that a feature tightly coupled to a dependency would match the dependency's name.
42
43### Example
44
45```toml
46[features]
47foo_bar = []
48```
49
50Should be written as:
51
52```toml
53[features]
54foo-bar = []
55```
56"#,
57 ),
58};
59
60#[instrument(skip_all)]
61pub(crate) fn lint_package(
62 ws: &Workspace<'_>,
63 pkg: &Package,
64 manifest_path: &Path,
65 level: LintLevelProduct,
66 pkg_stats: &mut ScopedDiagnosticStats<'_>,
67 gctx: &GlobalContext,
68) -> CargoResult<()> {
69 let LintLevelProduct {
70 level: lint_level,
71 source,
72 } = level;
73
74 let manifest_path = workspace_rel_path(ws, manifest_path);
75
76 lint_package_inner(pkg, &manifest_path, lint_level, source, pkg_stats, gctx)
77}
78
79fn lint_package_inner(
80 pkg: &Package,
81 manifest_path: &str,
82 lint_level: LintLevel,
83 source: LintLevelSource,
84 pkg_stats: &mut ScopedDiagnosticStats<'_>,
85 gctx: &GlobalContext,
86) -> CargoResult<()> {
87 for original_name in pkg.summary().features().keys() {
88 let original_name = &**original_name;
89 let kebab_case = heck::ToKebabCase::to_kebab_case(original_name);
90 if kebab_case == original_name {
91 continue;
92 }
93
94 let manifest = pkg.manifest();
95 let document = manifest.document();
96 let contents = manifest.contents();
97 let level = lint_level.to_diagnostic_level();
98 let emitted_source = LINT.emitted_source(lint_level, source);
99
100 let mut primary = Group::with_title(level.primary_title(format!(
101 "feature `{original_name}` should have a kebab-case name"
102 )));
103 if let Some(document) = document
104 && let Some(contents) = contents
105 && let Some(span) = get_key_value_span(document, &["features", original_name])
106 {
107 primary = primary.element(
108 Snippet::source(contents)
109 .path(manifest_path)
110 .annotation(AnnotationKind::Primary.span(span.key)),
111 );
112 } else if let Some(document) = document
113 && let Some(contents) = contents
114 && let Some(dep_span) = get_key_value_span(document, &["dependencies", original_name])
115 && let Some(optional_span) =
116 get_key_value_span(document, &["dependencies", original_name, "optional"])
117 {
118 primary = primary.element(
119 Snippet::source(contents)
120 .path(manifest_path)
121 .annotation(AnnotationKind::Primary.span(dep_span.key).label("source of feature name"))
122 .annotation(
123 AnnotationKind::Context
124 .span(optional_span.key.start..optional_span.value.end)
125 .label("cause of feature"),
126 ),
127 ).element(Level::NOTE.message("see also <https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies>"));
128 } else {
129 primary = primary.element(Origin::path(manifest_path));
130 }
131 primary = primary.element(Level::NOTE.message(emitted_source));
132 let mut report = vec![primary];
133 if let Some(document) = document
134 && let Some(contents) = contents
135 && let Some(span) = get_key_value_span(document, &["features", original_name])
136 {
137 let mut help = Group::with_title(Level::HELP.secondary_title(format!(
138 "to change the feature name to `{kebab_case}`, convert the `features` key"
139 )));
140 help = help.element(
141 Snippet::source(contents)
142 .path(manifest_path)
143 .patch(Patch::new(span.key, kebab_case.as_str())),
144 );
145 report.push(help);
146 }
147
148 pkg_stats.record_lint(lint_level);
149 gctx.shell().print_report(&report, lint_level.force())?;
150 }
151
152 Ok(())
153}