Skip to main content

cargo/diagnostics/rules/
missing_lints_features.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::Snippet;
8use tracing::instrument;
9
10use super::find_lint_or_group;
11use crate::CargoResult;
12use crate::GlobalContext;
13use crate::core::Feature;
14use crate::diagnostics::ManifestFor;
15use crate::diagnostics::get_key_value_span;
16use crate::diagnostics::rel_cwd_manifest_path;
17
18#[instrument(skip_all)]
19pub fn missing_lints_features(
20    manifest: ManifestFor<'_>,
21    manifest_path: &Path,
22    cargo_lints: &TomlToolLints,
23    error_count: &mut usize,
24    gctx: &GlobalContext,
25) -> CargoResult<()> {
26    let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
27    for lint_name in cargo_lints.keys().map(|name| name) {
28        let Some((name, default_level, feature_gate)) = find_lint_or_group(lint_name) else {
29            continue;
30        };
31
32        let (_, source, _) =
33            crate::diagnostics::lint::level_priority(name, *default_level, cargo_lints);
34
35        // Only run analysis on user-specified lints
36        if !source.is_user_specified() {
37            continue;
38        }
39
40        // Only run this on lints that are gated by a feature
41        if let Some(feature_gate) = feature_gate
42            && !manifest.unstable_features().is_enabled(feature_gate)
43        {
44            report_feature_not_enabled(
45                name,
46                feature_gate,
47                &manifest,
48                &manifest_path,
49                error_count,
50                gctx,
51            )?;
52        }
53    }
54
55    Ok(())
56}
57
58fn report_feature_not_enabled(
59    lint_name: &str,
60    feature_gate: &Feature,
61    manifest: &ManifestFor<'_>,
62    manifest_path: &str,
63    error_count: &mut usize,
64    gctx: &GlobalContext,
65) -> CargoResult<()> {
66    let dash_feature_name = feature_gate.name().replace("_", "-");
67
68    let mut error = Group::with_title(
69        Level::ERROR.primary_title(format!("use of unstable lint `{lint_name}`")),
70    );
71
72    if let Some(document) = manifest.document()
73        && let Some(contents) = manifest.contents()
74    {
75        let key_path = match manifest {
76            ManifestFor::Package(_) => &["lints", "cargo", lint_name][..],
77            ManifestFor::Workspace { .. } => &["workspace", "lints", "cargo", lint_name][..],
78        };
79        let Some(span) = get_key_value_span(document, key_path) else {
80            // This lint is handled by either package or workspace lint.
81            return Ok(());
82        };
83
84        error = error.element(Snippet::source(contents).path(manifest_path).annotation(
85            AnnotationKind::Primary.span(span.key).label(format!(
86                "this is behind `{dash_feature_name}`, which is not enabled"
87            )),
88        ))
89    }
90
91    let report = [error.element(Level::HELP.message(format!(
92        "consider adding `cargo-features = [\"{dash_feature_name}\"]` to the top of the manifest"
93    )))];
94
95    *error_count += 1;
96    gctx.shell().print_report(&report, true)?;
97
98    Ok(())
99}