Follow review comments (optimize the filtering)
This commit is contained in:
parent
698363122e
commit
8f8aa46a87
6 changed files with 97 additions and 29 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
54
clippy_lints/src/ctfe.rs
Normal file
54
clippy_lints/src/ctfe.rs
Normal file
|
|
@ -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<isize> = 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
|
||||
}
|
||||
}
|
||||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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 <https://doc.rust-lang.org/nightly/clippy/development/adding_lints.html#author-lint>"
|
||||
};
|
||||
|
||||
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<String, ()> {
|
|||
inner(&mut s, path)?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod author;
|
||||
pub mod dump_hir;
|
||||
pub mod format_args_collector;
|
||||
#[cfg(feature = "internal")]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue