-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(lints): Add missing_lints_inheritance #16588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+378
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| use std::path::Path; | ||
|
|
||
| use annotate_snippets::Group; | ||
| use annotate_snippets::Level; | ||
| use annotate_snippets::Origin; | ||
| use annotate_snippets::Patch; | ||
| use annotate_snippets::Snippet; | ||
| use cargo_util_schemas::manifest::TomlToolLints; | ||
|
|
||
| use crate::CargoResult; | ||
| use crate::GlobalContext; | ||
| use crate::core::Package; | ||
| use crate::core::Workspace; | ||
| use crate::lints::Lint; | ||
| use crate::lints::LintLevel; | ||
| use crate::lints::SUSPICIOUS; | ||
| use crate::lints::rel_cwd_manifest_path; | ||
|
|
||
| pub const LINT: Lint = Lint { | ||
| name: "missing_lints_inheritance", | ||
| desc: "missing `[lints]` to inherit `[workspace.lints]`", | ||
| primary_group: &SUSPICIOUS, | ||
| edition_lint_opts: None, | ||
| feature_gate: None, | ||
| docs: Some( | ||
| r#" | ||
| ### What it does | ||
|
|
||
| Checks for packages without a `lints` table while `workspace.lints` is present. | ||
|
|
||
| ### Why it is bad | ||
|
|
||
| Many people mistakenly think that `workspace.lints` is implicitly inherited when it is not. | ||
|
|
||
| ### Drawbacks | ||
|
|
||
| ### Example | ||
|
|
||
| ```toml | ||
| [workspace.lints.cargo] | ||
| ``` | ||
|
|
||
| Should be written as: | ||
|
|
||
| ```toml | ||
| [workspace.lints.cargo] | ||
|
|
||
| [lints] | ||
| workspace = true | ||
| ``` | ||
| "#, | ||
| ), | ||
| }; | ||
|
|
||
| pub fn missing_lints_inheritance( | ||
| ws: &Workspace<'_>, | ||
| pkg: &Package, | ||
| manifest_path: &Path, | ||
| cargo_lints: &TomlToolLints, | ||
| error_count: &mut usize, | ||
| gctx: &GlobalContext, | ||
| ) -> CargoResult<()> { | ||
| let (lint_level, reason) = LINT.level( | ||
| cargo_lints, | ||
| pkg.manifest().edition(), | ||
| pkg.manifest().unstable_features(), | ||
| ); | ||
|
|
||
| if lint_level == LintLevel::Allow { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let root = ws.root_maybe(); | ||
| // `normalized_toml` normally isn't guaranteed to include inheritance information except | ||
| // `workspace.lints` is used outside of inheritance for workspace-level lints. | ||
| let ws_lints = root | ||
| .normalized_toml() | ||
| .workspace | ||
| .as_ref() | ||
| .map(|ws| ws.lints.is_some()) | ||
| .unwrap_or(false); | ||
| if !ws_lints { | ||
| return Ok(()); | ||
| } | ||
| if pkg.manifest().normalized_toml().lints.is_some() { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let manifest = pkg.manifest(); | ||
| let contents = manifest.contents(); | ||
| let level = lint_level.to_diagnostic_level(); | ||
| let emitted_source = LINT.emitted_source(lint_level, reason); | ||
| let manifest_path = rel_cwd_manifest_path(manifest_path, gctx); | ||
|
|
||
| let mut primary = Group::with_title(level.primary_title(LINT.desc)); | ||
| primary = primary.element(Origin::path(&manifest_path)); | ||
| primary = primary.element(Level::NOTE.message(emitted_source)); | ||
| let mut report = vec![primary]; | ||
| if let Some(contents) = contents { | ||
| let span = contents.len()..contents.len(); | ||
| let mut help = | ||
| Group::with_title(Level::HELP.secondary_title("to inherit `workspace.lints, add:")); | ||
| help = help.element( | ||
| Snippet::source(contents) | ||
| .path(&manifest_path) | ||
| .patch(Patch::new(span.clone(), "\n[lints]\nworkspace = true")), | ||
| ); | ||
| report.push(help); | ||
| let mut help = Group::with_title( | ||
| Level::HELP.secondary_title("to clarify your intent to not inherit, add:"), | ||
| ); | ||
| help = help.element( | ||
| Snippet::source(contents) | ||
| .path(&manifest_path) | ||
| .patch(Patch::new(span, "\n[lints]")), | ||
| ); | ||
| report.push(help); | ||
| } | ||
|
|
||
| if lint_level.is_error() { | ||
| *error_count += 1; | ||
| } | ||
| gctx.shell().print_report(&report, lint_level.force())?; | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| use crate::prelude::*; | ||
| use cargo_test_support::project; | ||
| use cargo_test_support::str; | ||
|
|
||
| #[cargo_test] | ||
| fn no_lints() { | ||
| let p = project() | ||
| .file( | ||
| "Cargo.toml", | ||
| r#" | ||
| [workspace] | ||
| members = ["bar"] | ||
| "#, | ||
| ) | ||
| .file("src/main.rs", "fn main() {}") | ||
| .file( | ||
| "bar/Cargo.toml", | ||
| r#" | ||
| [package] | ||
| name = "bar" | ||
| version = "0.0.1" | ||
| edition = "2015" | ||
| "#, | ||
| ) | ||
| .file("bar/src/main.rs", "fn main() {}") | ||
| .build(); | ||
|
|
||
| p.cargo("check -Zcargo-lints") | ||
| .masquerade_as_nightly_cargo(&["cargo-lints"]) | ||
| .with_stderr_data(str![[r#" | ||
| [CHECKING] bar v0.0.1 ([ROOT]/foo/bar) | ||
| [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
|
|
||
| "#]]) | ||
| .run(); | ||
| } | ||
|
|
||
| #[cargo_test] | ||
| fn ws_lints() { | ||
| let p = project() | ||
| .file( | ||
| "Cargo.toml", | ||
| r#" | ||
| [workspace] | ||
| members = ["bar"] | ||
|
|
||
| [workspace.lints.cargo] | ||
| missing_lints_inheritance = "warn" | ||
| "#, | ||
| ) | ||
| .file("src/main.rs", "fn main() {}") | ||
| .file( | ||
| "bar/Cargo.toml", | ||
| r#" | ||
| [package] | ||
| name = "bar" | ||
| version = "0.0.1" | ||
| edition = "2015" | ||
| "#, | ||
| ) | ||
| .file("bar/src/main.rs", "fn main() {}") | ||
| .build(); | ||
|
|
||
| p.cargo("check -Zcargo-lints") | ||
| .masquerade_as_nightly_cargo(&["cargo-lints"]) | ||
| .with_stderr_data(str![[r#" | ||
| [WARNING] missing `[lints]` to inherit `[workspace.lints]` | ||
| --> bar/Cargo.toml | ||
| = [NOTE] `cargo::missing_lints_inheritance` is set to `warn` by default | ||
| [HELP] to inherit `workspace.lints, add: | ||
| | | ||
| 5 ~ edition = "2015" | ||
| 6 + [lints] | ||
| 7 + workspace = true | ||
| | | ||
| [HELP] to clarify your intent to not inherit, add: | ||
| | | ||
| 5 ~ edition = "2015" | ||
| 6 + [lints] | ||
| | | ||
| [CHECKING] bar v0.0.1 ([ROOT]/foo/bar) | ||
| [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
|
|
||
| "#]]) | ||
| .run(); | ||
| } | ||
|
|
||
| #[cargo_test] | ||
| fn empty_pkg_lints() { | ||
| let p = project() | ||
| .file( | ||
| "Cargo.toml", | ||
| r#" | ||
| [workspace] | ||
| members = ["bar"] | ||
|
|
||
| [workspace.lints.cargo] | ||
| missing_lints_inheritance = "warn" | ||
| "#, | ||
| ) | ||
| .file("src/main.rs", "fn main() {}") | ||
| .file( | ||
| "bar/Cargo.toml", | ||
| r#" | ||
| [package] | ||
| name = "bar" | ||
| version = "0.0.1" | ||
| edition = "2015" | ||
|
|
||
| [lints] | ||
| "#, | ||
| ) | ||
| .file("bar/src/main.rs", "fn main() {}") | ||
| .build(); | ||
|
|
||
| p.cargo("check -Zcargo-lints") | ||
| .masquerade_as_nightly_cargo(&["cargo-lints"]) | ||
| .with_stderr_data(str![[r#" | ||
| [CHECKING] bar v0.0.1 ([ROOT]/foo/bar) | ||
| [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
|
|
||
| "#]]) | ||
| .run(); | ||
| } | ||
|
|
||
| #[cargo_test] | ||
| fn inherit_lints() { | ||
| let p = project() | ||
| .file( | ||
| "Cargo.toml", | ||
| r#" | ||
| [workspace] | ||
| members = ["bar"] | ||
|
|
||
| [workspace.lints.cargo] | ||
| missing_lints_inheritance = "warn" | ||
| "#, | ||
| ) | ||
| .file("src/main.rs", "fn main() {}") | ||
| .file( | ||
| "bar/Cargo.toml", | ||
| r#" | ||
| [package] | ||
| name = "bar" | ||
| version = "0.0.1" | ||
| edition = "2015" | ||
|
|
||
| [lints] | ||
| workspace = true | ||
| "#, | ||
| ) | ||
| .file("bar/src/main.rs", "fn main() {}") | ||
| .build(); | ||
|
|
||
| p.cargo("check -Zcargo-lints") | ||
| .masquerade_as_nightly_cargo(&["cargo-lints"]) | ||
| .with_stderr_data(str![[r#" | ||
| [CHECKING] bar v0.0.1 ([ROOT]/foo/bar) | ||
| [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
|
|
||
| "#]]) | ||
| .run(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.