From 698363122eb28c37f79b1cda5e7ff4a197919dae Mon Sep 17 00:00:00 2001 From: blyxyas Date: Mon, 13 Nov 2023 14:35:37 +0100 Subject: [PATCH 1/5] 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 --- clippy_lints/src/asm_syntax.rs | 2 +- clippy_lints/src/utils/{ => internal_lints}/author.rs | 11 ++++++++--- clippy_lints/src/utils/mod.rs | 1 - tests/{ui => ui-internal}/author.rs | 2 ++ tests/{ui => ui-internal}/author.stdout | 0 tests/{ui => ui-internal}/author/blocks.rs | 0 tests/{ui => ui-internal}/author/blocks.stdout | 0 tests/{ui => ui-internal}/author/call.rs | 0 tests/{ui => ui-internal}/author/call.stdout | 0 tests/{ui => ui-internal}/author/if.rs | 0 tests/{ui => ui-internal}/author/if.stdout | 0 tests/{ui => ui-internal}/author/issue_3849.rs | 0 tests/{ui => ui-internal}/author/issue_3849.stdout | 0 tests/{ui => ui-internal}/author/loop.rs | 0 tests/{ui => ui-internal}/author/loop.stdout | 0 tests/{ui => ui-internal}/author/macro_in_closure.rs | 0 .../author/macro_in_closure.stdout | 0 tests/{ui => ui-internal}/author/macro_in_loop.rs | 0 tests/{ui => ui-internal}/author/macro_in_loop.stdout | 0 tests/{ui => ui-internal}/author/matches.rs | 0 tests/{ui => ui-internal}/author/matches.stdout | 0 tests/{ui => ui-internal}/author/repeat.rs | 0 tests/{ui => ui-internal}/author/repeat.stdout | 0 tests/{ui => ui-internal}/author/struct.rs | 0 tests/{ui => ui-internal}/author/struct.stdout | 0 tests/ui/no_lints.rs | 3 +++ 26 files changed, 14 insertions(+), 5 deletions(-) rename clippy_lints/src/utils/{ => internal_lints}/author.rs (99%) rename tests/{ui => ui-internal}/author.rs (72%) rename tests/{ui => ui-internal}/author.stdout (100%) rename tests/{ui => ui-internal}/author/blocks.rs (100%) rename tests/{ui => ui-internal}/author/blocks.stdout (100%) rename tests/{ui => ui-internal}/author/call.rs (100%) rename tests/{ui => ui-internal}/author/call.stdout (100%) rename tests/{ui => ui-internal}/author/if.rs (100%) rename tests/{ui => ui-internal}/author/if.stdout (100%) rename tests/{ui => ui-internal}/author/issue_3849.rs (100%) rename tests/{ui => ui-internal}/author/issue_3849.stdout (100%) rename tests/{ui => ui-internal}/author/loop.rs (100%) rename tests/{ui => ui-internal}/author/loop.stdout (100%) rename tests/{ui => ui-internal}/author/macro_in_closure.rs (100%) rename tests/{ui => ui-internal}/author/macro_in_closure.stdout (100%) rename tests/{ui => ui-internal}/author/macro_in_loop.rs (100%) rename tests/{ui => ui-internal}/author/macro_in_loop.stdout (100%) rename tests/{ui => ui-internal}/author/matches.rs (100%) rename tests/{ui => ui-internal}/author/matches.stdout (100%) rename tests/{ui => ui-internal}/author/repeat.rs (100%) rename tests/{ui => ui-internal}/author/repeat.stdout (100%) rename tests/{ui => ui-internal}/author/struct.rs (100%) rename tests/{ui => ui-internal}/author/struct.stdout (100%) create mode 100644 tests/ui/no_lints.rs diff --git a/clippy_lints/src/asm_syntax.rs b/clippy_lints/src/asm_syntax.rs index 69a8eb7d94e7..9f3c24a9e806 100644 --- a/clippy_lints/src/asm_syntax.rs +++ b/clippy_lints/src/asm_syntax.rs @@ -3,7 +3,7 @@ use std::fmt; use clippy_utils::diagnostics::span_lint_and_then; use rustc_ast::ast::{Expr, ExprKind, InlineAsmOptions}; use rustc_ast::{InlineAsm, Item, ItemKind}; -use rustc_lint::{EarlyContext, EarlyLintPass, Lint, LintContext}; +use rustc_lint::{EarlyContext, EarlyLintPass, Lint, LintPass, LintContext}; use rustc_session::declare_lint_pass; use rustc_span::Span; use rustc_target::asm::InlineAsmArch; diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/internal_lints/author.rs similarity index 99% rename from clippy_lints/src/utils/author.rs rename to clippy_lints/src/utils/internal_lints/author.rs index 31f9d84f5e46..0ed606a836e6 100644 --- a/clippy_lints/src/utils/author.rs +++ b/clippy_lints/src/utils/internal_lints/author.rs @@ -12,7 +12,7 @@ use rustc_span::symbol::{Ident, Symbol}; use std::cell::Cell; use std::fmt::{Display, Formatter, Write as _}; -declare_lint_pass!( +declare_clippy_lint!{ /// ### What it does /// Generates clippy code that detects the offending pattern /// @@ -44,8 +44,13 @@ declare_lint_pass!( /// // report your lint here /// } /// ``` - Author => [] -); + #[clippy::version = "1.0.0"] + pub AUTHOR, + internal, + "The author lint, see documentation at " +}; + +declare_lint_pass! { Author => [AUTHOR] } /// Writes a line of output with indentation added macro_rules! out { diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 13e9ead9a57f..abd10ac024c7 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -1,4 +1,3 @@ -pub mod author; pub mod dump_hir; pub mod format_args_collector; #[cfg(feature = "internal")] diff --git a/tests/ui/author.rs b/tests/ui-internal/author.rs similarity index 72% rename from tests/ui/author.rs rename to tests/ui-internal/author.rs index 0a1be3568967..eb1f3e3f8704 100644 --- a/tests/ui/author.rs +++ b/tests/ui-internal/author.rs @@ -1,3 +1,5 @@ +#![warn(clippy::author)] + fn main() { #[clippy::author] let x: char = 0x45 as char; diff --git a/tests/ui/author.stdout b/tests/ui-internal/author.stdout similarity index 100% rename from tests/ui/author.stdout rename to tests/ui-internal/author.stdout diff --git a/tests/ui/author/blocks.rs b/tests/ui-internal/author/blocks.rs similarity index 100% rename from tests/ui/author/blocks.rs rename to tests/ui-internal/author/blocks.rs diff --git a/tests/ui/author/blocks.stdout b/tests/ui-internal/author/blocks.stdout similarity index 100% rename from tests/ui/author/blocks.stdout rename to tests/ui-internal/author/blocks.stdout diff --git a/tests/ui/author/call.rs b/tests/ui-internal/author/call.rs similarity index 100% rename from tests/ui/author/call.rs rename to tests/ui-internal/author/call.rs diff --git a/tests/ui/author/call.stdout b/tests/ui-internal/author/call.stdout similarity index 100% rename from tests/ui/author/call.stdout rename to tests/ui-internal/author/call.stdout diff --git a/tests/ui/author/if.rs b/tests/ui-internal/author/if.rs similarity index 100% rename from tests/ui/author/if.rs rename to tests/ui-internal/author/if.rs diff --git a/tests/ui/author/if.stdout b/tests/ui-internal/author/if.stdout similarity index 100% rename from tests/ui/author/if.stdout rename to tests/ui-internal/author/if.stdout diff --git a/tests/ui/author/issue_3849.rs b/tests/ui-internal/author/issue_3849.rs similarity index 100% rename from tests/ui/author/issue_3849.rs rename to tests/ui-internal/author/issue_3849.rs diff --git a/tests/ui/author/issue_3849.stdout b/tests/ui-internal/author/issue_3849.stdout similarity index 100% rename from tests/ui/author/issue_3849.stdout rename to tests/ui-internal/author/issue_3849.stdout diff --git a/tests/ui/author/loop.rs b/tests/ui-internal/author/loop.rs similarity index 100% rename from tests/ui/author/loop.rs rename to tests/ui-internal/author/loop.rs diff --git a/tests/ui/author/loop.stdout b/tests/ui-internal/author/loop.stdout similarity index 100% rename from tests/ui/author/loop.stdout rename to tests/ui-internal/author/loop.stdout diff --git a/tests/ui/author/macro_in_closure.rs b/tests/ui-internal/author/macro_in_closure.rs similarity index 100% rename from tests/ui/author/macro_in_closure.rs rename to tests/ui-internal/author/macro_in_closure.rs diff --git a/tests/ui/author/macro_in_closure.stdout b/tests/ui-internal/author/macro_in_closure.stdout similarity index 100% rename from tests/ui/author/macro_in_closure.stdout rename to tests/ui-internal/author/macro_in_closure.stdout diff --git a/tests/ui/author/macro_in_loop.rs b/tests/ui-internal/author/macro_in_loop.rs similarity index 100% rename from tests/ui/author/macro_in_loop.rs rename to tests/ui-internal/author/macro_in_loop.rs diff --git a/tests/ui/author/macro_in_loop.stdout b/tests/ui-internal/author/macro_in_loop.stdout similarity index 100% rename from tests/ui/author/macro_in_loop.stdout rename to tests/ui-internal/author/macro_in_loop.stdout diff --git a/tests/ui/author/matches.rs b/tests/ui-internal/author/matches.rs similarity index 100% rename from tests/ui/author/matches.rs rename to tests/ui-internal/author/matches.rs diff --git a/tests/ui/author/matches.stdout b/tests/ui-internal/author/matches.stdout similarity index 100% rename from tests/ui/author/matches.stdout rename to tests/ui-internal/author/matches.stdout diff --git a/tests/ui/author/repeat.rs b/tests/ui-internal/author/repeat.rs similarity index 100% rename from tests/ui/author/repeat.rs rename to tests/ui-internal/author/repeat.rs diff --git a/tests/ui/author/repeat.stdout b/tests/ui-internal/author/repeat.stdout similarity index 100% rename from tests/ui/author/repeat.stdout rename to tests/ui-internal/author/repeat.stdout diff --git a/tests/ui/author/struct.rs b/tests/ui-internal/author/struct.rs similarity index 100% rename from tests/ui/author/struct.rs rename to tests/ui-internal/author/struct.rs diff --git a/tests/ui/author/struct.stdout b/tests/ui-internal/author/struct.stdout similarity index 100% rename from tests/ui/author/struct.stdout rename to tests/ui-internal/author/struct.stdout diff --git a/tests/ui/no_lints.rs b/tests/ui/no_lints.rs new file mode 100644 index 000000000000..a8467bb6ef78 --- /dev/null +++ b/tests/ui/no_lints.rs @@ -0,0 +1,3 @@ +#![deny(clippy::all)] + +fn main() {} \ No newline at end of file From 8f8aa46a87bc281cc152ff2dfdc4e9a03b4ddd87 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Tue, 18 Jun 2024 22:44:28 +0200 Subject: [PATCH 2/5] Follow review comments (optimize the filtering) --- clippy_lints/src/asm_syntax.rs | 2 +- clippy_lints/src/cognitive_complexity.rs | 54 ++++++++++++------- clippy_lints/src/ctfe.rs | 54 +++++++++++++++++++ clippy_lints/src/lib.rs | 3 ++ .../src/utils/{internal_lints => }/author.rs | 12 ++--- clippy_lints/src/utils/mod.rs | 1 + 6 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 clippy_lints/src/ctfe.rs rename clippy_lints/src/utils/{internal_lints => }/author.rs (99%) diff --git a/clippy_lints/src/asm_syntax.rs b/clippy_lints/src/asm_syntax.rs index 9f3c24a9e806..69a8eb7d94e7 100644 --- a/clippy_lints/src/asm_syntax.rs +++ b/clippy_lints/src/asm_syntax.rs @@ -3,7 +3,7 @@ use std::fmt; use clippy_utils::diagnostics::span_lint_and_then; use rustc_ast::ast::{Expr, ExprKind, InlineAsmOptions}; use rustc_ast::{InlineAsm, Item, ItemKind}; -use rustc_lint::{EarlyContext, EarlyLintPass, Lint, LintPass, LintContext}; +use rustc_lint::{EarlyContext, EarlyLintPass, Lint, LintContext}; use rustc_session::declare_lint_pass; use rustc_span::Span; use rustc_target::asm::InlineAsmArch; diff --git a/clippy_lints/src/cognitive_complexity.rs b/clippy_lints/src/cognitive_complexity.rs index 495d8ce3fa70..b027c289a7fc 100644 --- a/clippy_lints/src/cognitive_complexity.rs +++ b/clippy_lints/src/cognitive_complexity.rs @@ -8,30 +8,44 @@ use core::ops::ControlFlow; use rustc_ast::ast::Attribute; use rustc_hir::intravisit::FnKind; use rustc_hir::{Body, Expr, ExprKind, FnDecl}; -use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_lint::Level::Allow; +use rustc_lint::{LateContext, LateLintPass, Lint, LintContext}; use rustc_session::impl_lint_pass; use rustc_span::def_id::LocalDefId; use rustc_span::{Span, sym}; -declare_clippy_lint! { - /// ### What it does - /// Checks for methods with high cognitive complexity. - /// - /// ### Why is this bad? - /// Methods of high cognitive complexity tend to be hard to - /// both read and maintain. Also LLVM will tend to optimize small methods better. - /// - /// ### Known problems - /// Sometimes it's hard to find a way to reduce the - /// complexity. - /// - /// ### Example - /// You'll see it when you get the warning. - #[clippy::version = "1.35.0"] - pub COGNITIVE_COMPLEXITY, - nursery, - "functions that should be split up into multiple functions" -} +use crate::LintInfo; + +pub static COGNITIVE_COMPLEXITY: &Lint = &Lint { + name: &"clippy::COGNITIVE_COMPLEXITY", + default_level: Allow, + desc: "functions that should be split up into multiple functions", + edition_lint_opts: None, + report_in_external_macro: true, + future_incompatible: None, + is_externally_loaded: true, + crate_level_only: false, + loadbearing: true, + ..Lint::default_fields_for_macro() +}; +pub(crate) static COGNITIVE_COMPLEXITY_INFO: &'static LintInfo = &LintInfo { + lint: &COGNITIVE_COMPLEXITY, + category: crate::LintCategory::Nursery, + explanation: r"### What it does +Checks for methods with high cognitive complexity. + +### Why is this bad? +Methods of high cognitive complexity tend to be hard to both read and maintain. +Also LLVM will tend to optimize small methods better. + +### Known problems +Sometimes it's hard to find a way to reduce the complexity. + +### Example +You'll see it when you get the warning.", + version: Some("1.35.0"), + location: "#L0", +}; pub struct CognitiveComplexity { limit: LimitStack, diff --git a/clippy_lints/src/ctfe.rs b/clippy_lints/src/ctfe.rs new file mode 100644 index 000000000000..7b9f71810a99 --- /dev/null +++ b/clippy_lints/src/ctfe.rs @@ -0,0 +1,54 @@ +use rustc_hir::def_id::LocalDefId; +use rustc_hir::intravisit::FnKind; +use rustc_hir::{Body, FnDecl}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_session::declare_lint_pass; +use rustc_span::Span; + +declare_clippy_lint! { + /// ### What it does + /// Checks for comparisons where one side of the relation is + /// either the minimum or maximum value for its type and warns if it involves a + /// case that is always true or always false. Only integer and boolean types are + /// checked. + /// + /// ### Why is this bad? + /// An expression like `min <= x` may misleadingly imply + /// that it is possible for `x` to be less than the minimum. Expressions like + /// `max < x` are probably mistakes. + /// + /// ### Known problems + /// For `usize` the size of the current compile target will + /// be assumed (e.g., 64 bits on 64 bit systems). This means code that uses such + /// a comparison to detect target pointer width will trigger this lint. One can + /// use `mem::sizeof` and compare its value or conditional compilation + /// attributes + /// like `#[cfg(target_pointer_width = "64")] ..` instead. + /// + /// ### Example + /// ```no_run + /// let vec: Vec = Vec::new(); + /// if vec.len() <= 0 {} + /// if 100 > i32::MAX {} + /// ``` + #[clippy::version = "1.82.0"] + pub CLIPPY_CTFE, + correctness, + "a comparison with a maximum or minimum value that is always true or false" +} + +declare_lint_pass! { ClippyCtfe => [CLIPPY_CTFE] } + +impl<'tcx> LateLintPass<'tcx> for ClippyCtfe { + fn check_fn( + &mut self, + cx: &LateContext<'_>, + _: FnKind<'tcx>, + _: &'tcx FnDecl<'tcx>, + _: &'tcx Body<'tcx>, + _: Span, + defid: LocalDefId, + ) { + cx.tcx.ensure().mir_drops_elaborated_and_const_checked(defid); // Lint + } +} diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 6e29dde2211f..a5d2f6a4122a 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -65,6 +65,7 @@ extern crate clippy_utils; #[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))] mod utils; +pub mod ctfe; // VERY important lint (rust#125116) pub mod declared_lints; pub mod deprecated_lints; @@ -605,6 +606,8 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) { }); } + store.register_late_pass(|_| Box::new(ctfe::ClippyCtfe)); + store.register_late_pass(move |_| Box::new(operators::arithmetic_side_effects::ArithmeticSideEffects::new(conf))); store.register_late_pass(|_| Box::new(utils::dump_hir::DumpHir)); store.register_late_pass(|_| Box::new(utils::author::Author)); diff --git a/clippy_lints/src/utils/internal_lints/author.rs b/clippy_lints/src/utils/author.rs similarity index 99% rename from clippy_lints/src/utils/internal_lints/author.rs rename to clippy_lints/src/utils/author.rs index 0ed606a836e6..f4e166327afc 100644 --- a/clippy_lints/src/utils/internal_lints/author.rs +++ b/clippy_lints/src/utils/author.rs @@ -12,7 +12,7 @@ use rustc_span::symbol::{Ident, Symbol}; use std::cell::Cell; use std::fmt::{Display, Formatter, Write as _}; -declare_clippy_lint!{ +declare_lint_pass!( /// ### What it does /// Generates clippy code that detects the offending pattern /// @@ -44,13 +44,8 @@ declare_clippy_lint!{ /// // report your lint here /// } /// ``` - #[clippy::version = "1.0.0"] - pub AUTHOR, - internal, - "The author lint, see documentation at " -}; - -declare_lint_pass! { Author => [AUTHOR] } + Author => [] +); /// Writes a line of output with indentation added macro_rules! out { @@ -803,3 +798,4 @@ fn path_to_string(path: &QPath<'_>) -> Result { inner(&mut s, path)?; Ok(s) } + diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index abd10ac024c7..13e9ead9a57f 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -1,3 +1,4 @@ +pub mod author; pub mod dump_hir; pub mod format_args_collector; #[cfg(feature = "internal")] From e427a4e694794938a17ba6c29a1b66c8e6dc4d7d Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sat, 7 Sep 2024 12:13:03 +0200 Subject: [PATCH 3/5] Remove module passes filtering --- clippy_lints/src/cognitive_complexity.rs | 4 +- clippy_lints/src/ctfe.rs | 51 +++++++++--------------- clippy_lints/src/lib.rs | 2 +- clippy_lints/src/utils/author.rs | 1 - 4 files changed, 22 insertions(+), 36 deletions(-) diff --git a/clippy_lints/src/cognitive_complexity.rs b/clippy_lints/src/cognitive_complexity.rs index b027c289a7fc..91cb8b78ba8c 100644 --- a/clippy_lints/src/cognitive_complexity.rs +++ b/clippy_lints/src/cognitive_complexity.rs @@ -25,7 +25,7 @@ pub static COGNITIVE_COMPLEXITY: &Lint = &Lint { future_incompatible: None, is_externally_loaded: true, crate_level_only: false, - loadbearing: true, + eval_always: true, ..Lint::default_fields_for_macro() }; pub(crate) static COGNITIVE_COMPLEXITY_INFO: &'static LintInfo = &LintInfo { @@ -44,7 +44,7 @@ Sometimes it's hard to find a way to reduce the complexity. ### Example You'll see it when you get the warning.", version: Some("1.35.0"), - location: "#L0", + location: "clippy_lints/src/cognitive_complexity.rs#L47", }; pub struct CognitiveComplexity { diff --git a/clippy_lints/src/ctfe.rs b/clippy_lints/src/ctfe.rs index 7b9f71810a99..ddb4eb821659 100644 --- a/clippy_lints/src/ctfe.rs +++ b/clippy_lints/src/ctfe.rs @@ -1,41 +1,28 @@ use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit::FnKind; use rustc_hir::{Body, FnDecl}; -use rustc_lint::{LateContext, LateLintPass}; +use rustc_lint::Level::Deny; +use rustc_lint::{LateContext, LateLintPass, Lint}; use rustc_session::declare_lint_pass; use rustc_span::Span; -declare_clippy_lint! { - /// ### What it does - /// Checks for comparisons where one side of the relation is - /// either the minimum or maximum value for its type and warns if it involves a - /// case that is always true or always false. Only integer and boolean types are - /// checked. - /// - /// ### Why is this bad? - /// An expression like `min <= x` may misleadingly imply - /// that it is possible for `x` to be less than the minimum. Expressions like - /// `max < x` are probably mistakes. - /// - /// ### Known problems - /// For `usize` the size of the current compile target will - /// be assumed (e.g., 64 bits on 64 bit systems). This means code that uses such - /// a comparison to detect target pointer width will trigger this lint. One can - /// use `mem::sizeof` and compare its value or conditional compilation - /// attributes - /// like `#[cfg(target_pointer_width = "64")] ..` instead. - /// - /// ### Example - /// ```no_run - /// let vec: Vec = Vec::new(); - /// if vec.len() <= 0 {} - /// if 100 > i32::MAX {} - /// ``` - #[clippy::version = "1.82.0"] - pub CLIPPY_CTFE, - correctness, - "a comparison with a maximum or minimum value that is always true or false" -} +/// Ensures that Constant-time Function Evaluation is being done (specifically, MIR lint passes). +/// See rust-lang/rust#125116 for more info. +#[clippy::version = "1.82.0"] +pub static CLIPPY_CTFE: &Lint = &Lint { + name: &"clippy::CLIPPY_CTFE", + default_level: Deny, + desc: "Ensure CTFE is being made", + edition_lint_opts: None, + report_in_external_macro: true, + future_incompatible: None, + is_externally_loaded: true, + crate_level_only: false, + eval_always: true, + ..Lint::default_fields_for_macro() +}; + +// No static CLIPPY_CTFE_INFO because we want this lint to be invisible declare_lint_pass! { ClippyCtfe => [CLIPPY_CTFE] } diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index a5d2f6a4122a..88a227f5b6db 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -65,7 +65,7 @@ extern crate clippy_utils; #[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))] mod utils; -pub mod ctfe; // VERY important lint (rust#125116) +pub mod ctfe; // Very important lint (rust#125116) pub mod declared_lints; pub mod deprecated_lints; diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/author.rs index f4e166327afc..31f9d84f5e46 100644 --- a/clippy_lints/src/utils/author.rs +++ b/clippy_lints/src/utils/author.rs @@ -798,4 +798,3 @@ fn path_to_string(path: &QPath<'_>) -> Result { inner(&mut s, path)?; Ok(s) } - From e518d66dc0721341ab331938f90d2e48328d2969 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Tue, 15 Oct 2024 22:15:07 +0200 Subject: [PATCH 4/5] Apply review comments + use `shallow_lint_levels_on` --- clippy_lints/src/ctfe.rs | 2 +- clippy_lints/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/ctfe.rs b/clippy_lints/src/ctfe.rs index ddb4eb821659..93d0ad789beb 100644 --- a/clippy_lints/src/ctfe.rs +++ b/clippy_lints/src/ctfe.rs @@ -7,7 +7,7 @@ use rustc_session::declare_lint_pass; use rustc_span::Span; /// Ensures that Constant-time Function Evaluation is being done (specifically, MIR lint passes). -/// See rust-lang/rust#125116 for more info. +/// As Clippy deactivates codegen, this lint ensures that CTFE (used in hard errors) is still ran. #[clippy::version = "1.82.0"] pub static CLIPPY_CTFE: &Lint = &Lint { name: &"clippy::CLIPPY_CTFE", diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 88a227f5b6db..14110539709d 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -65,7 +65,7 @@ extern crate clippy_utils; #[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))] mod utils; -pub mod ctfe; // Very important lint (rust#125116) +pub mod ctfe; // Very important lint, do not remove (rust#125116) pub mod declared_lints; pub mod deprecated_lints; From 3773534f29f817c46f13f30e118d430225d3a299 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sat, 19 Oct 2024 17:47:16 +0200 Subject: [PATCH 5/5] Move COGNITIVE_COMPLEXITY to use macro again --- clippy_lints/src/cognitive_complexity.rs | 55 +++++++++--------------- clippy_lints/src/ctfe.rs | 1 - clippy_lints/src/declare_clippy_lint.rs | 30 ++++++++----- 3 files changed, 41 insertions(+), 45 deletions(-) diff --git a/clippy_lints/src/cognitive_complexity.rs b/clippy_lints/src/cognitive_complexity.rs index 91cb8b78ba8c..477435236a52 100644 --- a/clippy_lints/src/cognitive_complexity.rs +++ b/clippy_lints/src/cognitive_complexity.rs @@ -8,44 +8,31 @@ use core::ops::ControlFlow; use rustc_ast::ast::Attribute; use rustc_hir::intravisit::FnKind; use rustc_hir::{Body, Expr, ExprKind, FnDecl}; -use rustc_lint::Level::Allow; -use rustc_lint::{LateContext, LateLintPass, Lint, LintContext}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::impl_lint_pass; use rustc_span::def_id::LocalDefId; use rustc_span::{Span, sym}; -use crate::LintInfo; - -pub static COGNITIVE_COMPLEXITY: &Lint = &Lint { - name: &"clippy::COGNITIVE_COMPLEXITY", - default_level: Allow, - desc: "functions that should be split up into multiple functions", - edition_lint_opts: None, - report_in_external_macro: true, - future_incompatible: None, - is_externally_loaded: true, - crate_level_only: false, - eval_always: true, - ..Lint::default_fields_for_macro() -}; -pub(crate) static COGNITIVE_COMPLEXITY_INFO: &'static LintInfo = &LintInfo { - lint: &COGNITIVE_COMPLEXITY, - category: crate::LintCategory::Nursery, - explanation: r"### What it does -Checks for methods with high cognitive complexity. - -### Why is this bad? -Methods of high cognitive complexity tend to be hard to both read and maintain. -Also LLVM will tend to optimize small methods better. - -### Known problems -Sometimes it's hard to find a way to reduce the complexity. - -### Example -You'll see it when you get the warning.", - version: Some("1.35.0"), - location: "clippy_lints/src/cognitive_complexity.rs#L47", -}; +declare_clippy_lint! { + /// ### What it does + /// Checks for methods with high cognitive complexity. + /// + /// ### Why is this bad? + /// Methods of high cognitive complexity tend to be hard to + /// both read and maintain. Also LLVM will tend to optimize small methods better. + /// + /// ### Known problems + /// Sometimes it's hard to find a way to reduce the + /// complexity. + /// + /// ### Example + /// You'll see it when you get the warning. + #[clippy::version = "1.35.0"] + pub COGNITIVE_COMPLEXITY, + nursery, + "functions that should be split up into multiple functions" + @eval_always = true +} pub struct CognitiveComplexity { limit: LimitStack, diff --git a/clippy_lints/src/ctfe.rs b/clippy_lints/src/ctfe.rs index 93d0ad789beb..2fe37a64db6a 100644 --- a/clippy_lints/src/ctfe.rs +++ b/clippy_lints/src/ctfe.rs @@ -8,7 +8,6 @@ use rustc_span::Span; /// Ensures that Constant-time Function Evaluation is being done (specifically, MIR lint passes). /// As Clippy deactivates codegen, this lint ensures that CTFE (used in hard errors) is still ran. -#[clippy::version = "1.82.0"] pub static CLIPPY_CTFE: &Lint = &Lint { name: &"clippy::CLIPPY_CTFE", default_level: Deny, diff --git a/clippy_lints/src/declare_clippy_lint.rs b/clippy_lints/src/declare_clippy_lint.rs index b1e39c70baa2..a785a9d377c5 100644 --- a/clippy_lints/src/declare_clippy_lint.rs +++ b/clippy_lints/src/declare_clippy_lint.rs @@ -9,6 +9,7 @@ macro_rules! declare_clippy_lint { $desc:literal, $version_expr:expr, $version_lit:literal + $(, $eval_always: literal)? ) => { rustc_session::declare_tool_lint! { $(#[doc = $lit])* @@ -17,6 +18,7 @@ macro_rules! declare_clippy_lint { $category, $desc, report_in_external_macro:true + $(, @eval_always = $eval_always)? } pub(crate) static ${concat($lint_name, _INFO)}: &'static crate::LintInfo = &crate::LintInfo { @@ -33,11 +35,12 @@ macro_rules! declare_clippy_lint { pub $lint_name:ident, restriction, $desc:literal + $(@eval_always = $eval_always: literal)? ) => { declare_clippy_lint! {@ $(#[doc = $lit])* pub $lint_name, Allow, crate::LintCategory::Restriction, $desc, - Some($version), $version + Some($version), $version $(, $eval_always)? } }; ( @@ -46,12 +49,12 @@ macro_rules! declare_clippy_lint { pub $lint_name:ident, style, $desc:literal + $(@eval_always = $eval_always: literal)? ) => { declare_clippy_lint! {@ $(#[doc = $lit])* pub $lint_name, Warn, crate::LintCategory::Style, $desc, - Some($version), $version - + Some($version), $version $(, $eval_always)? } }; ( @@ -60,11 +63,12 @@ macro_rules! declare_clippy_lint { pub $lint_name:ident, correctness, $desc:literal + $(@eval_always = $eval_always: literal)? ) => { declare_clippy_lint! {@ $(#[doc = $lit])* pub $lint_name, Deny, crate::LintCategory::Correctness, $desc, - Some($version), $version + Some($version), $version $(, $eval_always)? } }; @@ -74,11 +78,12 @@ macro_rules! declare_clippy_lint { pub $lint_name:ident, perf, $desc:literal + $(@eval_always = $eval_always: literal)? ) => { declare_clippy_lint! {@ $(#[doc = $lit])* pub $lint_name, Warn, crate::LintCategory::Perf, $desc, - Some($version), $version + Some($version), $version $(, $eval_always)? } }; ( @@ -87,11 +92,12 @@ macro_rules! declare_clippy_lint { pub $lint_name:ident, complexity, $desc:literal + $(@eval_always = $eval_always: literal)? ) => { declare_clippy_lint! {@ $(#[doc = $lit])* pub $lint_name, Warn, crate::LintCategory::Complexity, $desc, - Some($version), $version + Some($version), $version $(, $eval_always)? } }; ( @@ -100,11 +106,12 @@ macro_rules! declare_clippy_lint { pub $lint_name:ident, suspicious, $desc:literal + $(@eval_always = $eval_always: literal)? ) => { declare_clippy_lint! {@ $(#[doc = $lit])* pub $lint_name, Warn, crate::LintCategory::Suspicious, $desc, - Some($version), $version + Some($version), $version $(, $eval_always)? } }; ( @@ -113,11 +120,12 @@ macro_rules! declare_clippy_lint { pub $lint_name:ident, nursery, $desc:literal + $(@eval_always = $eval_always: literal)? ) => { declare_clippy_lint! {@ $(#[doc = $lit])* pub $lint_name, Allow, crate::LintCategory::Nursery, $desc, - Some($version), $version + Some($version), $version $(, $eval_always)? } }; ( @@ -126,11 +134,12 @@ macro_rules! declare_clippy_lint { pub $lint_name:ident, pedantic, $desc:literal + $(@eval_always = $eval_always: literal)? ) => { declare_clippy_lint! {@ $(#[doc = $lit])* pub $lint_name, Allow, crate::LintCategory::Pedantic, $desc, - Some($version), $version + Some($version), $version $(, $eval_always)? } }; ( @@ -139,11 +148,12 @@ macro_rules! declare_clippy_lint { pub $lint_name:ident, cargo, $desc:literal + $(@eval_always = $eval_always: literal)? ) => { declare_clippy_lint! {@ $(#[doc = $lit])* pub $lint_name, Allow, crate::LintCategory::Cargo, $desc, - Some($version), $version + Some($version), $version $(, $eval_always)? } };