Skip to content

Commit 9e36f89

Browse files
committed
Auto merge of rust-lang#125116 - blyxyas:ignore-allowed-lints-final, r=cjgillot
(Big performance change) Do not run lints that cannot emit Before this change, adding a lint was a difficult matter because it always had some overhead involved. This was because all lints would run, no matter their default level, or if the user had `#![allow]`ed them. This PR changes that. This change would improve both the Rust lint infrastructure and Clippy, but Clippy will see the most benefit, as it has about 900 registered lints (and growing!) So yeah, with this little patch we filter all lints pre-linting, and remove any lint that is either: - Manually `#![allow]`ed in the whole crate, - Allowed in the command line, or - Not manually enabled with `#[warn]` or similar, and its default level is `Allow` As some lints **need** to run, this PR also adds **loadbearing lints**. On a lint declaration, you can use the ``@eval_always` = true` marker to label it as loadbearing. A loadbearing lint will never be filtered (it will always run) Fixes rust-lang#106983
2 parents b75d511 + 3773534 commit 9e36f89

27 files changed

+69
-10
lines changed

clippy_lints/src/cognitive_complexity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ declare_clippy_lint! {
3131
pub COGNITIVE_COMPLEXITY,
3232
nursery,
3333
"functions that should be split up into multiple functions"
34+
@eval_always = true
3435
}
3536

3637
pub struct CognitiveComplexity {

clippy_lints/src/ctfe.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use rustc_hir::def_id::LocalDefId;
2+
use rustc_hir::intravisit::FnKind;
3+
use rustc_hir::{Body, FnDecl};
4+
use rustc_lint::Level::Deny;
5+
use rustc_lint::{LateContext, LateLintPass, Lint};
6+
use rustc_session::declare_lint_pass;
7+
use rustc_span::Span;
8+
9+
/// Ensures that Constant-time Function Evaluation is being done (specifically, MIR lint passes).
10+
/// As Clippy deactivates codegen, this lint ensures that CTFE (used in hard errors) is still ran.
11+
pub static CLIPPY_CTFE: &Lint = &Lint {
12+
name: &"clippy::CLIPPY_CTFE",
13+
default_level: Deny,
14+
desc: "Ensure CTFE is being made",
15+
edition_lint_opts: None,
16+
report_in_external_macro: true,
17+
future_incompatible: None,
18+
is_externally_loaded: true,
19+
crate_level_only: false,
20+
eval_always: true,
21+
..Lint::default_fields_for_macro()
22+
};
23+
24+
// No static CLIPPY_CTFE_INFO because we want this lint to be invisible
25+
26+
declare_lint_pass! { ClippyCtfe => [CLIPPY_CTFE] }
27+
28+
impl<'tcx> LateLintPass<'tcx> for ClippyCtfe {
29+
fn check_fn(
30+
&mut self,
31+
cx: &LateContext<'_>,
32+
_: FnKind<'tcx>,
33+
_: &'tcx FnDecl<'tcx>,
34+
_: &'tcx Body<'tcx>,
35+
_: Span,
36+
defid: LocalDefId,
37+
) {
38+
cx.tcx.ensure().mir_drops_elaborated_and_const_checked(defid); // Lint
39+
}
40+
}

clippy_lints/src/declare_clippy_lint.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ macro_rules! declare_clippy_lint {
99
$desc:literal,
1010
$version_expr:expr,
1111
$version_lit:literal
12+
$(, $eval_always: literal)?
1213
) => {
1314
rustc_session::declare_tool_lint! {
1415
$(#[doc = $lit])*
@@ -17,6 +18,7 @@ macro_rules! declare_clippy_lint {
1718
$category,
1819
$desc,
1920
report_in_external_macro:true
21+
$(, @eval_always = $eval_always)?
2022
}
2123

2224
pub(crate) static ${concat($lint_name, _INFO)}: &'static crate::LintInfo = &crate::LintInfo {
@@ -33,11 +35,12 @@ macro_rules! declare_clippy_lint {
3335
pub $lint_name:ident,
3436
restriction,
3537
$desc:literal
38+
$(@eval_always = $eval_always: literal)?
3639
) => {
3740
declare_clippy_lint! {@
3841
$(#[doc = $lit])*
3942
pub $lint_name, Allow, crate::LintCategory::Restriction, $desc,
40-
Some($version), $version
43+
Some($version), $version $(, $eval_always)?
4144
}
4245
};
4346
(
@@ -46,12 +49,12 @@ macro_rules! declare_clippy_lint {
4649
pub $lint_name:ident,
4750
style,
4851
$desc:literal
52+
$(@eval_always = $eval_always: literal)?
4953
) => {
5054
declare_clippy_lint! {@
5155
$(#[doc = $lit])*
5256
pub $lint_name, Warn, crate::LintCategory::Style, $desc,
53-
Some($version), $version
54-
57+
Some($version), $version $(, $eval_always)?
5558
}
5659
};
5760
(
@@ -60,11 +63,12 @@ macro_rules! declare_clippy_lint {
6063
pub $lint_name:ident,
6164
correctness,
6265
$desc:literal
66+
$(@eval_always = $eval_always: literal)?
6367
) => {
6468
declare_clippy_lint! {@
6569
$(#[doc = $lit])*
6670
pub $lint_name, Deny, crate::LintCategory::Correctness, $desc,
67-
Some($version), $version
71+
Some($version), $version $(, $eval_always)?
6872

6973
}
7074
};
@@ -74,11 +78,12 @@ macro_rules! declare_clippy_lint {
7478
pub $lint_name:ident,
7579
perf,
7680
$desc:literal
81+
$(@eval_always = $eval_always: literal)?
7782
) => {
7883
declare_clippy_lint! {@
7984
$(#[doc = $lit])*
8085
pub $lint_name, Warn, crate::LintCategory::Perf, $desc,
81-
Some($version), $version
86+
Some($version), $version $(, $eval_always)?
8287
}
8388
};
8489
(
@@ -87,11 +92,12 @@ macro_rules! declare_clippy_lint {
8792
pub $lint_name:ident,
8893
complexity,
8994
$desc:literal
95+
$(@eval_always = $eval_always: literal)?
9096
) => {
9197
declare_clippy_lint! {@
9298
$(#[doc = $lit])*
9399
pub $lint_name, Warn, crate::LintCategory::Complexity, $desc,
94-
Some($version), $version
100+
Some($version), $version $(, $eval_always)?
95101
}
96102
};
97103
(
@@ -100,11 +106,12 @@ macro_rules! declare_clippy_lint {
100106
pub $lint_name:ident,
101107
suspicious,
102108
$desc:literal
109+
$(@eval_always = $eval_always: literal)?
103110
) => {
104111
declare_clippy_lint! {@
105112
$(#[doc = $lit])*
106113
pub $lint_name, Warn, crate::LintCategory::Suspicious, $desc,
107-
Some($version), $version
114+
Some($version), $version $(, $eval_always)?
108115
}
109116
};
110117
(
@@ -113,11 +120,12 @@ macro_rules! declare_clippy_lint {
113120
pub $lint_name:ident,
114121
nursery,
115122
$desc:literal
123+
$(@eval_always = $eval_always: literal)?
116124
) => {
117125
declare_clippy_lint! {@
118126
$(#[doc = $lit])*
119127
pub $lint_name, Allow, crate::LintCategory::Nursery, $desc,
120-
Some($version), $version
128+
Some($version), $version $(, $eval_always)?
121129
}
122130
};
123131
(
@@ -126,11 +134,12 @@ macro_rules! declare_clippy_lint {
126134
pub $lint_name:ident,
127135
pedantic,
128136
$desc:literal
137+
$(@eval_always = $eval_always: literal)?
129138
) => {
130139
declare_clippy_lint! {@
131140
$(#[doc = $lit])*
132141
pub $lint_name, Allow, crate::LintCategory::Pedantic, $desc,
133-
Some($version), $version
142+
Some($version), $version $(, $eval_always)?
134143
}
135144
};
136145
(
@@ -139,11 +148,12 @@ macro_rules! declare_clippy_lint {
139148
pub $lint_name:ident,
140149
cargo,
141150
$desc:literal
151+
$(@eval_always = $eval_always: literal)?
142152
) => {
143153
declare_clippy_lint! {@
144154
$(#[doc = $lit])*
145155
pub $lint_name, Allow, crate::LintCategory::Cargo, $desc,
146-
Some($version), $version
156+
Some($version), $version $(, $eval_always)?
147157
}
148158
};
149159

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extern crate clippy_utils;
6565
#[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))]
6666
mod utils;
6767

68+
pub mod ctfe; // Very important lint, do not remove (rust#125116)
6869
pub mod declared_lints;
6970
pub mod deprecated_lints;
7071

@@ -605,6 +606,8 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
605606
});
606607
}
607608

609+
store.register_late_pass(|_| Box::new(ctfe::ClippyCtfe));
610+
608611
store.register_late_pass(move |_| Box::new(operators::arithmetic_side_effects::ArithmeticSideEffects::new(conf)));
609612
store.register_late_pass(|_| Box::new(utils::dump_hir::DumpHir));
610613
store.register_late_pass(|_| Box::new(utils::author::Author));

tests/ui/author.rs renamed to tests/ui-internal/author.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![warn(clippy::author)]
2+
13
fn main() {
24
#[clippy::author]
35
let x: char = 0x45 as char;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/ui/no_lints.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![deny(clippy::all)]
2+
3+
fn main() {}

0 commit comments

Comments
 (0)