-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Cleanup linting system #13797
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
Cleanup linting system #13797
Changes from all commits
Commits
Show all changes
4 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
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 |
---|---|---|
|
@@ -68,6 +68,13 @@ pub struct LintGroup { | |
pub edition_lint_opts: Option<(Edition, LintLevel)>, | ||
} | ||
|
||
const TEST_DUMMY_UNSTABLE: LintGroup = LintGroup { | ||
name: "test_dummy_unstable", | ||
desc: "test_dummy_unstable is meant to only be used in tests", | ||
default_level: LintLevel::Allow, | ||
edition_lint_opts: None, | ||
}; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub struct Lint { | ||
pub name: &'static str, | ||
|
@@ -79,23 +86,37 @@ pub struct Lint { | |
|
||
impl Lint { | ||
pub fn level(&self, lints: &TomlToolLints, edition: Edition) -> LintLevel { | ||
let edition_level = self | ||
.edition_lint_opts | ||
.filter(|(e, _)| edition >= *e) | ||
.map(|(_, l)| l); | ||
|
||
if self.default_level == LintLevel::Forbid || edition_level == Some(LintLevel::Forbid) { | ||
return LintLevel::Forbid; | ||
} | ||
|
||
let level = self | ||
.groups | ||
.iter() | ||
.map(|g| g.name) | ||
.chain(std::iter::once(self.name)) | ||
.filter_map(|n| lints.get(n).map(|l| (n, l))) | ||
.max_by_key(|(n, l)| (l.priority(), std::cmp::Reverse(*n))); | ||
.max_by_key(|(n, l)| { | ||
( | ||
l.level() == TomlLintLevel::Forbid, | ||
l.priority(), | ||
std::cmp::Reverse(*n), | ||
) | ||
}); | ||
|
||
match level { | ||
Some((_, toml_lint)) => toml_lint.level().into(), | ||
None => { | ||
if let Some((lint_edition, lint_level)) = self.edition_lint_opts { | ||
if edition >= lint_edition { | ||
return lint_level; | ||
} | ||
if let Some(level) = edition_level { | ||
level | ||
} else { | ||
self.default_level | ||
} | ||
self.default_level | ||
} | ||
} | ||
} | ||
|
@@ -123,7 +144,7 @@ impl Display for LintLevel { | |
impl LintLevel { | ||
pub fn to_diagnostic_level(self) -> Level { | ||
match self { | ||
LintLevel::Allow => Level::Note, | ||
LintLevel::Allow => unreachable!("allow does not map to a diagnostic level"), | ||
LintLevel::Warn => Level::Warning, | ||
LintLevel::Deny => Level::Error, | ||
LintLevel::Forbid => Level::Error, | ||
|
@@ -142,6 +163,61 @@ impl From<TomlLintLevel> for LintLevel { | |
} | ||
} | ||
|
||
const IM_A_TEAPOT: Lint = Lint { | ||
name: "im_a_teapot", | ||
desc: "`im_a_teapot` is specified", | ||
groups: &[TEST_DUMMY_UNSTABLE], | ||
default_level: LintLevel::Allow, | ||
edition_lint_opts: None, | ||
}; | ||
|
||
pub fn check_im_a_teapot( | ||
pkg: &Package, | ||
path: &Path, | ||
lints: &TomlToolLints, | ||
error_count: &mut usize, | ||
gctx: &GlobalContext, | ||
) -> CargoResult<()> { | ||
let manifest = pkg.manifest(); | ||
let lint_level = IM_A_TEAPOT.level(lints, manifest.edition()); | ||
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.
|
||
if lint_level == LintLevel::Allow { | ||
return Ok(()); | ||
} | ||
|
||
if manifest | ||
.resolved_toml() | ||
.package() | ||
.is_some_and(|p| p.im_a_teapot.is_some()) | ||
{ | ||
if lint_level == LintLevel::Forbid || lint_level == LintLevel::Deny { | ||
*error_count += 1; | ||
} | ||
let level = lint_level.to_diagnostic_level(); | ||
let manifest_path = rel_cwd_manifest_path(path, gctx); | ||
let emitted_reason = format!("`cargo::{}` is set to `{lint_level}`", IM_A_TEAPOT.name); | ||
|
||
let key_span = get_span(manifest.document(), &["package", "im-a-teapot"], false).unwrap(); | ||
let value_span = get_span(manifest.document(), &["package", "im-a-teapot"], true).unwrap(); | ||
let message = level | ||
.title(IM_A_TEAPOT.desc) | ||
.snippet( | ||
Snippet::source(manifest.contents()) | ||
.origin(&manifest_path) | ||
.annotation(level.span(key_span.start..value_span.end)) | ||
.fold(true), | ||
) | ||
.footer(Level::Note.title(&emitted_reason)); | ||
let renderer = Renderer::styled().term_width( | ||
gctx.shell() | ||
.err_width() | ||
.diagnostic_terminal_width() | ||
.unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH), | ||
); | ||
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]). | ||
|
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
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.
The implementation for default and edition exists in two places. We should do this in one place.