Skip to content

Commit dc1adc6

Browse files
committed
Auto merge of #5505 - flip1995:avoid_running_lints, r=matthiaskrgr
Avoid running cargo+internal lints when not enabled r? @matthiaskrgr changelog: none
2 parents 02c9435 + f31502f commit dc1adc6

File tree

6 files changed

+66
-27
lines changed

6 files changed

+66
-27
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/internal_lints.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::SpanlessEq;
22
use crate::utils::{
3-
is_expn_of, match_def_path, match_qpath, match_type, method_calls, paths, snippet, span_lint, span_lint_and_help,
4-
span_lint_and_sugg, walk_ptrs_ty,
3+
is_expn_of, match_def_path, match_qpath, match_type, method_calls, paths, run_lints, snippet, span_lint,
4+
span_lint_and_help, span_lint_and_sugg, walk_ptrs_ty,
55
};
66
use if_chain::if_chain;
77
use rustc_ast::ast::{Crate as AstCrate, ItemKind, LitKind, Name, NodeId};
@@ -10,7 +10,8 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
use rustc_errors::Applicability;
1111
use rustc_hir as hir;
1212
use rustc_hir::def::{DefKind, Res};
13-
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
13+
use rustc_hir::hir_id::CRATE_HIR_ID;
14+
use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
1415
use rustc_hir::{Crate, Expr, ExprKind, HirId, Item, MutTy, Mutability, Path, StmtKind, Ty, TyKind};
1516
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
1617
use rustc_middle::hir::map::Map;
@@ -252,6 +253,10 @@ impl_lint_pass!(LintWithoutLintPass => [DEFAULT_LINT, LINT_WITHOUT_LINT_PASS]);
252253

253254
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
254255
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
256+
if !run_lints(cx, &[DEFAULT_LINT], item.hir_id) {
257+
return;
258+
}
259+
255260
if let hir::ItemKind::Static(ref ty, Mutability::Not, body_id) = item.kind {
256261
if is_lint_ref_type(cx, ty) {
257262
let expr = &cx.tcx.hir().body(body_id).value;
@@ -306,6 +311,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
306311
}
307312

308313
fn check_crate_post(&mut self, cx: &LateContext<'a, 'tcx>, _: &'tcx Crate<'_>) {
314+
if !run_lints(cx, &[LINT_WITHOUT_LINT_PASS], CRATE_HIR_ID) {
315+
return;
316+
}
317+
309318
for (lint_name, &lint_span) in &self.declared_lints {
310319
// When using the `declare_tool_lint!` macro, the original `lint_span`'s
311320
// file points to "<rustc macros>".
@@ -355,15 +364,12 @@ struct LintCollector<'a, 'tcx> {
355364
impl<'a, 'tcx> Visitor<'tcx> for LintCollector<'a, 'tcx> {
356365
type Map = Map<'tcx>;
357366

358-
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
359-
walk_expr(self, expr);
360-
}
361-
362367
fn visit_path(&mut self, path: &'tcx Path<'_>, _: HirId) {
363368
if path.segments.len() == 1 {
364369
self.output.insert(path.segments[0].ident.name);
365370
}
366371
}
372+
367373
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
368374
NestedVisitorMap::All(self.cx.tcx.hir())
369375
}
@@ -391,6 +397,10 @@ impl_lint_pass!(CompilerLintFunctions => [COMPILER_LINT_FUNCTIONS]);
391397

392398
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CompilerLintFunctions {
393399
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
400+
if !run_lints(cx, &[COMPILER_LINT_FUNCTIONS], expr.hir_id) {
401+
return;
402+
}
403+
394404
if_chain! {
395405
if let ExprKind::MethodCall(ref path, _, ref args) = expr.kind;
396406
let fn_name = path.ident;
@@ -416,6 +426,10 @@ declare_lint_pass!(OuterExpnDataPass => [OUTER_EXPN_EXPN_DATA]);
416426

417427
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnDataPass {
418428
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
429+
if !run_lints(cx, &[OUTER_EXPN_EXPN_DATA], expr.hir_id) {
430+
return;
431+
}
432+
419433
let (method_names, arg_lists, spans) = method_calls(expr, 2);
420434
let method_names: Vec<SymbolStr> = method_names.iter().map(|s| s.as_str()).collect();
421435
let method_names: Vec<&str> = method_names.iter().map(|s| &**s).collect();
@@ -462,6 +476,10 @@ declare_lint_pass!(CollapsibleCalls => [COLLAPSIBLE_SPAN_LINT_CALLS]);
462476

463477
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CollapsibleCalls {
464478
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
479+
if !run_lints(cx, &[COLLAPSIBLE_SPAN_LINT_CALLS], expr.hir_id) {
480+
return;
481+
}
482+
465483
if_chain! {
466484
if let ExprKind::Call(ref func, ref and_then_args) = expr.kind;
467485
if let ExprKind::Path(ref path) = func.kind;

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)