-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add unknown-lints
lint
#13639
Closed
Closed
Add unknown-lints
lint
#13639
Changes from 2 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 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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
use crate::core::FeatureValue::Dep; | ||
use crate::core::{Edition, FeatureValue, Package}; | ||
use crate::core::{Edition, FeatureValue, MaybePackage}; | ||
use crate::util::interning::InternedString; | ||
use crate::{CargoResult, GlobalContext}; | ||
use annotate_snippets::{Level, Renderer, Snippet}; | ||
|
@@ -58,6 +58,8 @@ fn rel_cwd_manifest_path(path: &Path, gctx: &GlobalContext) -> String { | |
.to_string() | ||
} | ||
|
||
const LINT_GROUPS: &[LintGroup] = &[RUST_2024_COMPATIBILITY]; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub struct LintGroup { | ||
pub name: &'static str, | ||
|
@@ -136,6 +138,121 @@ impl From<TomlLintLevel> for LintLevel { | |
} | ||
} | ||
|
||
const LINTS: &[Lint] = &[IMPLICIT_FEATURES, UNKNOWN_LINTS]; | ||
|
||
const UNKNOWN_LINTS: Lint = Lint { | ||
name: "unknown_lints", | ||
desc: "detects unrecognized lint names", | ||
groups: &[], | ||
default_level: LintLevel::Warn, | ||
edition_lint_opts: None, | ||
}; | ||
|
||
pub fn unknown_lints( | ||
maybe_pkg: &MaybePackage, | ||
path: &Path, | ||
resolved_lints: &TomlToolLints, | ||
error_count: &mut usize, | ||
gctx: &GlobalContext, | ||
) -> CargoResult<()> { | ||
let edition = match maybe_pkg { | ||
MaybePackage::Package(pkg) => pkg.manifest().edition(), | ||
MaybePackage::Virtual(_) => Edition::default(), | ||
}; | ||
|
||
let lint_level = UNKNOWN_LINTS.level(resolved_lints, edition); | ||
if lint_level == LintLevel::Allow { | ||
return Ok(()); | ||
} | ||
|
||
let original_toml = match maybe_pkg { | ||
MaybePackage::Package(pkg) => pkg.manifest().original_toml(), | ||
MaybePackage::Virtual(vm) => vm.original_toml(), | ||
}; | ||
|
||
let contents = match maybe_pkg { | ||
MaybePackage::Package(pkg) => pkg.manifest().contents(), | ||
MaybePackage::Virtual(vm) => vm.contents(), | ||
}; | ||
|
||
let document = match maybe_pkg { | ||
MaybePackage::Package(pkg) => pkg.manifest().document(), | ||
MaybePackage::Virtual(vm) => vm.document(), | ||
}; | ||
|
||
let renderer = Renderer::styled().term_width( | ||
gctx.shell() | ||
.err_width() | ||
.diagnostic_terminal_width() | ||
.unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH), | ||
); | ||
let level = lint_level.to_diagnostic_level(); | ||
let manifest_path = rel_cwd_manifest_path(path, gctx); | ||
|
||
let all_names = LINTS | ||
.iter() | ||
.map(|lint| lint.name) | ||
.chain(LINT_GROUPS.iter().map(|group| group.name)) | ||
.collect::<HashSet<_>>(); | ||
|
||
if let Some(lints) = original_toml | ||
.workspace | ||
.as_ref() | ||
.map_or(&None, |ws| &ws.lints) | ||
{ | ||
if let Some(cargo_lints) = lints.get("cargo") { | ||
for (name, _) in cargo_lints.iter() { | ||
let normalized_name = name.replace("-", "_"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is having me lean more towards normalizing earlier in the process |
||
if all_names.contains(normalized_name.as_str()) { | ||
continue; | ||
} | ||
if lint_level == LintLevel::Forbid || lint_level == LintLevel::Deny { | ||
*error_count += 1; | ||
} | ||
let title = format!("unknown lint: `{}`", name); | ||
let message = level.title(&title).snippet( | ||
Snippet::source(contents) | ||
.origin(&manifest_path) | ||
.annotation( | ||
level.span( | ||
get_span(document, &["workspace", "lints", "cargo", name], false) | ||
.unwrap(), | ||
), | ||
) | ||
.fold(true), | ||
); | ||
writeln!(gctx.shell().err(), "{}", renderer.render(message))?; | ||
} | ||
} | ||
} | ||
|
||
if let Some(lints) = original_toml.lints.as_ref().map(|l| &l.lints) { | ||
if let Some(cargo_lints) = lints.get("cargo") { | ||
for (name, _) in cargo_lints.iter() { | ||
let normalized_name = name.replace("-", "_"); | ||
if all_names.contains(normalized_name.as_str()) { | ||
continue; | ||
} | ||
if lint_level == LintLevel::Forbid || lint_level == LintLevel::Deny { | ||
*error_count += 1; | ||
} | ||
let title = format!("unknown lint: `{}`", name); | ||
let message = | ||
level.title(&title).snippet( | ||
Snippet::source(contents) | ||
.origin(&manifest_path) | ||
.annotation(level.span( | ||
get_span(document, &["lints", "cargo", name], false).unwrap(), | ||
)) | ||
.fold(true), | ||
); | ||
writeln!(gctx.shell().err(), "{}", renderer.render(message))?; | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// By default, cargo will treat any optional dependency as a [feature]. As of | ||
/// cargo 1.60, these can be disabled by declaring a feature that activates the | ||
/// optional dependency as `dep:<name>` (see [RFC #3143]). | ||
|
@@ -158,12 +275,16 @@ const IMPLICIT_FEATURES: Lint = Lint { | |
}; | ||
|
||
pub fn check_implicit_features( | ||
pkg: &Package, | ||
maybe_pkg: &MaybePackage, | ||
path: &Path, | ||
lints: &TomlToolLints, | ||
error_count: &mut usize, | ||
gctx: &GlobalContext, | ||
) -> CargoResult<()> { | ||
let MaybePackage::Package(pkg) = maybe_pkg else { | ||
return Ok(()); | ||
}; | ||
|
||
let lint_level = IMPLICIT_FEATURES.level(lints, pkg.manifest().edition()); | ||
if lint_level == LintLevel::Allow { | ||
return Ok(()); | ||
|
This file contains 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
33 changes: 33 additions & 0 deletions
33
tests/testsuite/lints/implicit_features/edition_2021/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod implicit_features; | ||
mod rust_2024_compatibility; | ||
mod unknown_lints; |
This file contains 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.
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.
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.
imo this should be treated as a separate "workspace" lint that is only subject to
workspace.lints
so we have consistency between virtual and real workspace packages.