Skip to main content

cargo/diagnostics/rules/
missing_lints_inheritance.rs

1use std::path::Path;
2
3use cargo_util_terminal::report::Group;
4use cargo_util_terminal::report::Level;
5use cargo_util_terminal::report::Origin;
6use cargo_util_terminal::report::Patch;
7use cargo_util_terminal::report::Snippet;
8use tracing::instrument;
9
10use super::SUSPICIOUS;
11use crate::CargoResult;
12use crate::GlobalContext;
13use crate::core::Package;
14use crate::core::Workspace;
15use crate::diagnostics::DiagnosticStats;
16use crate::diagnostics::Lint;
17use crate::diagnostics::LintLevelProduct;
18use crate::diagnostics::rel_cwd_manifest_path;
19
20pub static LINT: &Lint = &Lint {
21    name: "missing_lints_inheritance",
22    desc: "missing `[lints]` to inherit `[workspace.lints]`",
23    primary_group: &SUSPICIOUS,
24    msrv: Some(super::CARGO_LINTS_MSRV),
25    feature_gate: None,
26    docs: Some(
27        r#"
28### What it does
29
30Checks for packages without a `lints` table while `workspace.lints` is present.
31
32### Why it is bad
33
34Many people mistakenly think that `workspace.lints` is implicitly inherited when it is not.
35
36### Drawbacks
37
38### Example
39
40```toml
41[workspace.lints.cargo]
42```
43
44Should be written as:
45
46```toml
47[workspace.lints.cargo]
48
49[lints]
50workspace = true
51```
52"#,
53    ),
54};
55
56#[instrument(skip_all)]
57pub(crate) fn lint_package(
58    ws: &Workspace<'_>,
59    pkg: &Package,
60    manifest_path: &Path,
61    level: LintLevelProduct,
62    stats: &mut DiagnosticStats,
63    gctx: &GlobalContext,
64) -> CargoResult<()> {
65    let LintLevelProduct {
66        level: lint_level,
67        source,
68    } = level;
69
70    let root = ws.root_maybe();
71    // `normalized_toml` normally isn't guaranteed to include inheritance information except
72    // `workspace.lints` is used outside of inheritance for workspace-level lints.
73    let ws_lints = root
74        .normalized_toml()
75        .workspace
76        .as_ref()
77        .map(|ws| ws.lints.is_some())
78        .unwrap_or(false);
79    if !ws_lints {
80        return Ok(());
81    }
82    if pkg.manifest().normalized_toml().lints.is_some() {
83        return Ok(());
84    }
85
86    let manifest = pkg.manifest();
87    let contents = manifest.contents();
88    let level = lint_level.to_diagnostic_level();
89    let emitted_source = LINT.emitted_source(lint_level, source);
90    let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
91
92    let mut primary = Group::with_title(level.primary_title(LINT.desc));
93    primary = primary.element(Origin::path(&manifest_path));
94    primary = primary.element(Level::NOTE.message(emitted_source));
95    let mut report = vec![primary];
96    if let Some(contents) = contents {
97        let span = contents.len()..contents.len();
98        let mut help =
99            Group::with_title(Level::HELP.secondary_title("to inherit `workspace.lints, add:"));
100        help = help.element(
101            Snippet::source(contents)
102                .path(&manifest_path)
103                .patch(Patch::new(span.clone(), "\n[lints]\nworkspace = true")),
104        );
105        report.push(help);
106        let mut help = Group::with_title(
107            Level::HELP.secondary_title("to clarify your intent to not inherit, add:"),
108        );
109        help = help.element(
110            Snippet::source(contents)
111                .path(&manifest_path)
112                .patch(Patch::new(span, "\n[lints]")),
113        );
114        report.push(help);
115    }
116
117    stats.record_lint(lint_level);
118    gctx.shell().print_report(&report, lint_level.force())?;
119
120    Ok(())
121}