Skip to content

Commit 14f596c

Browse files
committed
Only run cargo lints, when they are warn/deny/forbid
1 parent 1d4dd3d commit 14f596c

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

clippy_lints/src/cargo_common_metadata.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
use std::path::PathBuf;
44

5-
use crate::utils::span_lint;
6-
use rustc_ast::ast::Crate;
7-
use rustc_lint::{EarlyContext, EarlyLintPass};
5+
use crate::utils::{run_lints, span_lint};
6+
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
7+
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use rustc_span::source_map::DUMMY_SP;
1010

@@ -35,11 +35,11 @@ declare_clippy_lint! {
3535
"common metadata is defined in `Cargo.toml`"
3636
}
3737

38-
fn warning(cx: &EarlyContext<'_>, message: &str) {
38+
fn warning(cx: &LateContext<'_, '_>, message: &str) {
3939
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
4040
}
4141

42-
fn missing_warning(cx: &EarlyContext<'_>, package: &cargo_metadata::Package, field: &str) {
42+
fn missing_warning(cx: &LateContext<'_, '_>, package: &cargo_metadata::Package, field: &str) {
4343
let message = format!("package `{}` is missing `{}` metadata", package.name, field);
4444
warning(cx, &message);
4545
}
@@ -59,8 +59,12 @@ fn is_empty_vec(value: &[String]) -> bool {
5959

6060
declare_lint_pass!(CargoCommonMetadata => [CARGO_COMMON_METADATA]);
6161

62-
impl EarlyLintPass for CargoCommonMetadata {
63-
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
62+
impl LateLintPass<'_, '_> for CargoCommonMetadata {
63+
fn check_crate(&mut self, cx: &LateContext<'_, '_>, _: &Crate<'_>) {
64+
if !run_lints(cx, &[CARGO_COMMON_METADATA], CRATE_HIR_ID) {
65+
return;
66+
}
67+
6468
let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
6569
metadata
6670
} else {

clippy_lints/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,9 +1024,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10241024
store.register_early_pass(|| box precedence::Precedence);
10251025
store.register_early_pass(|| box needless_continue::NeedlessContinue);
10261026
store.register_early_pass(|| box redundant_static_lifetimes::RedundantStaticLifetimes);
1027-
store.register_early_pass(|| box cargo_common_metadata::CargoCommonMetadata);
1028-
store.register_early_pass(|| box multiple_crate_versions::MultipleCrateVersions);
1029-
store.register_early_pass(|| box wildcard_dependencies::WildcardDependencies);
1027+
store.register_late_pass(|| box cargo_common_metadata::CargoCommonMetadata);
1028+
store.register_late_pass(|| box multiple_crate_versions::MultipleCrateVersions);
1029+
store.register_late_pass(|| box wildcard_dependencies::WildcardDependencies);
10301030
store.register_early_pass(|| box literal_representation::LiteralDigitGrouping);
10311031
let literal_representation_threshold = conf.literal_representation_threshold;
10321032
store.register_early_pass(move || box literal_representation::DecimalLiteralRepresentation::new(literal_representation_threshold));

clippy_lints/src/multiple_crate_versions.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! lint on multiple versions of a crate being used
22
3-
use crate::utils::span_lint;
4-
use rustc_ast::ast::Crate;
5-
use rustc_lint::{EarlyContext, EarlyLintPass};
3+
use crate::utils::{run_lints, span_lint};
4+
use rustc_hir::{Crate, CRATE_HIR_ID};
5+
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
77
use rustc_span::source_map::DUMMY_SP;
88

@@ -33,8 +33,12 @@ declare_clippy_lint! {
3333

3434
declare_lint_pass!(MultipleCrateVersions => [MULTIPLE_CRATE_VERSIONS]);
3535

36-
impl EarlyLintPass for MultipleCrateVersions {
37-
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
36+
impl LateLintPass<'_, '_> for MultipleCrateVersions {
37+
fn check_crate(&mut self, cx: &LateContext<'_, '_>, _: &Crate<'_>) {
38+
if !run_lints(cx, &[MULTIPLE_CRATE_VERSIONS], CRATE_HIR_ID) {
39+
return;
40+
}
41+
3842
let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().exec() {
3943
metadata
4044
} else {

clippy_lints/src/utils/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,15 @@ pub fn fn_has_unsatisfiable_preds(cx: &LateContext<'_, '_>, did: DefId) -> bool
13991399
)
14001400
}
14011401

1402+
pub fn run_lints(cx: &LateContext<'_, '_>, lints: &[&'static Lint], id: HirId) -> bool {
1403+
lints.iter().any(|lint| {
1404+
matches!(
1405+
cx.tcx.lint_level_at_node(lint, id),
1406+
(Level::Forbid | Level::Deny | Level::Warn, _)
1407+
)
1408+
})
1409+
}
1410+
14021411
#[cfg(test)]
14031412
mod test {
14041413
use super::{trim_multiline, without_block_comments};

clippy_lints/src/wildcard_dependencies.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::utils::span_lint;
2-
use rustc_ast::ast::Crate;
3-
use rustc_lint::{EarlyContext, EarlyLintPass};
1+
use crate::utils::{run_lints, span_lint};
2+
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
3+
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
55
use rustc_span::source_map::DUMMY_SP;
66

@@ -28,8 +28,12 @@ declare_clippy_lint! {
2828

2929
declare_lint_pass!(WildcardDependencies => [WILDCARD_DEPENDENCIES]);
3030

31-
impl EarlyLintPass for WildcardDependencies {
32-
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
31+
impl LateLintPass<'_, '_> for WildcardDependencies {
32+
fn check_crate(&mut self, cx: &LateContext<'_, '_>, _: &Crate<'_>) {
33+
if !run_lints(cx, &[WILDCARD_DEPENDENCIES], CRATE_HIR_ID) {
34+
return;
35+
}
36+
3337
let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
3438
metadata
3539
} else {

0 commit comments

Comments
 (0)