Skip to main content

cargo/diagnostics/rules/
non_kebab_case_features.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_features",
26    desc: "features 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 feature 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 would expect that a feature tightly coupled to a dependency would match the dependency's name.
43
44### Example
45
46```toml
47[features]
48foo_bar = []
49```
50
51Should be written as:
52
53```toml
54[features]
55foo-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    for original_name in pkg.summary().features().keys() {
89        let original_name = &**original_name;
90        let kebab_case = heck::ToKebabCase::to_kebab_case(original_name);
91        if kebab_case == original_name {
92            continue;
93        }
94
95        let manifest = pkg.manifest();
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, &["features", original_name])
105        {
106            primary = primary.element(
107                Snippet::source(contents)
108                    .path(manifest_path)
109                    .annotation(AnnotationKind::Primary.span(span.key)),
110            );
111        } else if let Some(document) = document
112            && let Some(contents) = contents
113            && let Some(dep_span) = get_key_value_span(document, &["dependencies", original_name])
114            && let Some(optional_span) =
115                get_key_value_span(document, &["dependencies", original_name, "optional"])
116        {
117            primary = primary.element(
118                Snippet::source(contents)
119                    .path(manifest_path)
120                    .annotation(AnnotationKind::Primary.span(dep_span.key).label("source of feature name"))
121                    .annotation(
122                        AnnotationKind::Context
123                            .span(optional_span.key.start..optional_span.value.end)
124                            .label("cause of feature"),
125                    ),
126            ).element(Level::NOTE.message("see also <https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies>"));
127        } else {
128            primary = primary.element(Origin::path(manifest_path));
129        }
130        primary = primary.element(Level::NOTE.message(emitted_source));
131        let mut report = vec![primary];
132        if let Some(document) = document
133            && let Some(contents) = contents
134            && let Some(span) = get_key_value_span(document, &["features", original_name])
135        {
136            let mut help = Group::with_title(Level::HELP.secondary_title(
137                "to change the feature name to kebab case, convert the `features` key",
138            ));
139            help = help.element(
140                Snippet::source(contents)
141                    .path(manifest_path)
142                    .patch(Patch::new(span.key, kebab_case.as_str())),
143            );
144            report.push(help);
145        }
146
147        stats.record_lint(lint_level);
148        gctx.shell().print_report(&report, lint_level.force())?;
149    }
150
151    Ok(())
152}