-
-
Notifications
You must be signed in to change notification settings - Fork 3k
refactor(lints): Pull out unknown_lints lint logic and missing_lints_features diagnostic logic
#16976
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
Merged
refactor(lints): Pull out unknown_lints lint logic and missing_lints_features diagnostic logic
#16976
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d457dc
refactor(lints): Tighten missing feature diagnostic
epage 600af40
refactor(lints): Split unknown_lints from missing_features
epage 7d0396a
refactor(lints): Consolidate unknown_lints
epage 3cd183f
refactor(lints): Inline output_unknown_lints
epage 878b4e7
refactor(lints): Order unknown_lints like others
epage 4ca4fb1
test(lints): Filter out diagnostics
epage 9c3a290
refactor(lints): Move missing_features diagnostic to its own rules mod
epage 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
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,98 @@ | ||
| use std::path::Path; | ||
|
|
||
| use cargo_util_schemas::manifest::TomlToolLints; | ||
| use cargo_util_terminal::report::AnnotationKind; | ||
| use cargo_util_terminal::report::Group; | ||
| use cargo_util_terminal::report::Level; | ||
| use cargo_util_terminal::report::Snippet; | ||
| use tracing::instrument; | ||
|
|
||
| use super::find_lint_or_group; | ||
| use crate::CargoResult; | ||
| use crate::GlobalContext; | ||
| use crate::core::Feature; | ||
| use crate::lints::ManifestFor; | ||
| use crate::lints::get_key_value_span; | ||
| use crate::lints::rel_cwd_manifest_path; | ||
|
|
||
| #[instrument(skip_all)] | ||
| pub fn missing_lints_features( | ||
| manifest: ManifestFor<'_>, | ||
| manifest_path: &Path, | ||
| cargo_lints: &TomlToolLints, | ||
| error_count: &mut usize, | ||
| gctx: &GlobalContext, | ||
| ) -> CargoResult<()> { | ||
| let manifest_path = rel_cwd_manifest_path(manifest_path, gctx); | ||
| for lint_name in cargo_lints.keys().map(|name| name) { | ||
| let Some((name, default_level, feature_gate)) = find_lint_or_group(lint_name) else { | ||
| continue; | ||
| }; | ||
|
|
||
| let (_, source, _) = crate::lints::lint::level_priority(name, *default_level, cargo_lints); | ||
|
|
||
| // Only run analysis on user-specified lints | ||
| if !source.is_user_specified() { | ||
| continue; | ||
| } | ||
|
|
||
| // Only run this on lints that are gated by a feature | ||
| if let Some(feature_gate) = feature_gate | ||
| && !manifest.unstable_features().is_enabled(feature_gate) | ||
| { | ||
| report_feature_not_enabled( | ||
| name, | ||
| feature_gate, | ||
| &manifest, | ||
| &manifest_path, | ||
| error_count, | ||
| gctx, | ||
| )?; | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn report_feature_not_enabled( | ||
| lint_name: &str, | ||
| feature_gate: &Feature, | ||
| manifest: &ManifestFor<'_>, | ||
| manifest_path: &str, | ||
| error_count: &mut usize, | ||
| gctx: &GlobalContext, | ||
| ) -> CargoResult<()> { | ||
| let dash_feature_name = feature_gate.name().replace("_", "-"); | ||
|
|
||
| let mut error = Group::with_title( | ||
| Level::ERROR.primary_title(format!("use of unstable lint `{lint_name}`")), | ||
| ); | ||
|
|
||
| if let Some(document) = manifest.document() | ||
| && let Some(contents) = manifest.contents() | ||
| { | ||
| let key_path = match manifest { | ||
| ManifestFor::Package(_) => &["lints", "cargo", lint_name][..], | ||
| ManifestFor::Workspace { .. } => &["workspace", "lints", "cargo", lint_name][..], | ||
| }; | ||
| let Some(span) = get_key_value_span(document, key_path) else { | ||
| // This lint is handled by either package or workspace lint. | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| error = error.element(Snippet::source(contents).path(manifest_path).annotation( | ||
| AnnotationKind::Primary.span(span.key).label(format!( | ||
| "this is behind `{dash_feature_name}`, which is not enabled" | ||
| )), | ||
| )) | ||
| } | ||
|
|
||
| let report = [error.element(Level::HELP.message(format!( | ||
| "consider adding `cargo-features = [\"{dash_feature_name}\"]` to the top of the manifest" | ||
| )))]; | ||
|
|
||
| *error_count += 1; | ||
| gctx.shell().print_report(&report, true)?; | ||
|
|
||
| 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
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.
Should we be more paranoid checking
static LINTin case it might accidentally matches? Or should we list diagnostic files out explicitly?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.
If we do
static LINT, we could miss some things as diagnostics. I figured it would be good to err on the being as permissive as possible.As for a diagnostic list, maybe? In theory, we should have more lints than diagnostics but likely the first step towards making many of our deferred warning diagnostics into lints is to pull them out as dedicated diagnostics.
I also want to play with unused item warnings to help manage this. If we switch them from
pubtopub(crate), we might be able to have the compiler help us catch missing ones. I just didn't want this to be blocked on that experiment and getting consensus on merging it.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.
Reasonable. Merging!