-
-
Notifications
You must be signed in to change notification settings - Fork 3k
feat(lints): add unused_workspace_exclude lint
#17143
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
Draft
raushan728
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
raushan728:issues/17089
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
100 changes: 100 additions & 0 deletions
100
src/cargo/diagnostics/rules/unused_workspace_exclude.rs
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,100 @@ | ||
| use std::path::Path; | ||
|
|
||
| use cargo_util_terminal::report::{AnnotationKind, Group, Level, Origin, Snippet}; | ||
| use tracing::instrument; | ||
|
|
||
| use super::SUSPICIOUS; | ||
| use crate::diagnostics::{ | ||
| Lint, LintLevelProduct, ScopedDiagnosticStats, get_key_value_span, workspace_rel_path, | ||
| }; | ||
| use crate::{ | ||
| CargoResult, GlobalContext, | ||
| core::{MaybePackage, Workspace}, | ||
| }; | ||
|
|
||
| pub static LINT: &Lint = &Lint { | ||
| name: "unused_workspace_exclude", | ||
| desc: "unused workspace exclude", | ||
| primary_group: &SUSPICIOUS, | ||
| msrv: Some(super::CARGO_LINTS_MSRV), | ||
| feature_gate: None, | ||
| docs: Some( | ||
| r#" | ||
| ### What it does | ||
| Checks for any entry in `[workspace.exclude]` that does not match any workspace member | ||
|
|
||
| ### Why it is bad | ||
| They can give the false impression that a package is excluded when it is actually not present | ||
|
|
||
| ### Example | ||
| ```toml | ||
| [workspace] | ||
| exclude = ["does-not-exist"] | ||
| ``` | ||
| "#, | ||
| ), | ||
| }; | ||
|
|
||
| #[instrument(skip_all)] | ||
| pub(crate) fn lint_workspace( | ||
| ws: &Workspace<'_>, | ||
| maybe_pkg: &MaybePackage, | ||
| manifest_path: &Path, | ||
| level: LintLevelProduct, | ||
| pkg_stats: &mut ScopedDiagnosticStats<'_>, | ||
| gctx: &GlobalContext, | ||
| ) -> CargoResult<()> { | ||
| let LintLevelProduct { | ||
| level: lint_level, | ||
| source, | ||
| } = level; | ||
|
|
||
| let Some(original_toml) = maybe_pkg.original_toml() else { | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| let Some(workspace) = original_toml.workspace.as_ref() else { | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| let Some(exclude) = workspace.exclude.as_ref() else { | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| let used_exclude_patterns = ws.used_exclude_patterns(); | ||
|
|
||
| for (i, unused) in exclude | ||
| .iter() | ||
| .filter(|pattern| !used_exclude_patterns.contains(*pattern)) | ||
| .enumerate() | ||
| { | ||
| let document = maybe_pkg.document(); | ||
| let contents = maybe_pkg.contents(); | ||
| let level = lint_level.to_diagnostic_level(); | ||
| let manifest_path = workspace_rel_path(ws, manifest_path); | ||
| let emitted_source = LINT.emitted_source(lint_level, source); | ||
|
|
||
| let mut primary = | ||
| Group::with_title(level.primary_title(format!("unused exclude pattern '{}'", unused))); | ||
| if let Some(document) = document | ||
| && let Some(contents) = contents | ||
| { | ||
| let mut snippet = Snippet::source(contents).path(&manifest_path); | ||
| if let Some(span) = get_key_value_span(document, &["workspace", "exclude"]) { | ||
| snippet = snippet.annotation(AnnotationKind::Primary.span(span.key)); | ||
| } | ||
| primary = primary.element(snippet); | ||
| } else { | ||
| primary = primary.element(Origin::path(&manifest_path)); | ||
| } | ||
| if i == 0 { | ||
| primary = primary.element(Level::NOTE.message(emitted_source)); | ||
| } | ||
| let report = vec![primary]; | ||
|
|
||
| pkg_stats.record_lint(lint_level); | ||
| 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@raushan728 I really appreciate your work and directly went with an implementation. You've contributed quite a lot useful fixes and improvements recently!
I believe you may already know but just remind you again that the issue is not marked as
S-acceptedand hasn't yet got sufficient design discussions.View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the kind words. I understand the issue isn't accepted yet. #17089 (comment)
Based on that, it seemed clear that a lint (not a hard-coded diagnostic) was the right path. There wasn’t any pushback on the issue, so I went ahead with an implementation that tracks which
excludepatterns actually matched a crate during resolution only truly unused ones warnThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not exactly a pushback, though I have one new comment #17089 (comment). I don't really have time thinking on this harder unfortunately
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I've seen #6745 the
nested-pathexclude behavior is definitely wonky but, this lint is completely independent though. It only warns when an exclude pattern matched zero packages during resolution, so it catches typos and stale config without touching the buggynested-pathlogic. That bug exists with or without the lint.If you'd prefer to land fixes for #6745 first, we can convert this PR to draft until then. Just let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To add to what was said, no pushback does not mean everything has been figured out. In particular, this was still in the Triage state which means we haven't really thought at all about this. There is one lint where we are unsure if it is worth the review and maintainance cost. We could also have a different way to reframe a lint to take it in a different direction.
One particular aspect of our label system is to align contributions with our availability, including people pressuring us (intententionally or not) to commit time we do not have by posting PRs. What we most need is thinking, not code, and that is what we lack to move a lot of these issues forward. Note that Weihang did not have time to think more on this but that led to more questions. This is why we encourage contributing through working on accepted issues and enabling decisions on those that are not.