Skip to content

Commit 8f8aa46

Browse files
committed
Follow review comments (optimize the filtering)
1 parent 6983631 commit 8f8aa46

File tree

6 files changed

+97
-29
lines changed

6 files changed

+97
-29
lines changed

clippy_lints/src/asm_syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use rustc_ast::ast::{Expr, ExprKind, InlineAsmOptions};
55
use rustc_ast::{InlineAsm, Item, ItemKind};
6-
use rustc_lint::{EarlyContext, EarlyLintPass, Lint, LintPass, LintContext};
6+
use rustc_lint::{EarlyContext, EarlyLintPass, Lint, LintContext};
77
use rustc_session::declare_lint_pass;
88
use rustc_span::Span;
99
use rustc_target::asm::InlineAsmArch;

clippy_lints/src/cognitive_complexity.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,44 @@ use core::ops::ControlFlow;
88
use rustc_ast::ast::Attribute;
99
use rustc_hir::intravisit::FnKind;
1010
use rustc_hir::{Body, Expr, ExprKind, FnDecl};
11-
use rustc_lint::{LateContext, LateLintPass, LintContext};
11+
use rustc_lint::Level::Allow;
12+
use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
1213
use rustc_session::impl_lint_pass;
1314
use rustc_span::def_id::LocalDefId;
1415
use rustc_span::{Span, sym};
1516

16-
declare_clippy_lint! {
17-
/// ### What it does
18-
/// Checks for methods with high cognitive complexity.
19-
///
20-
/// ### Why is this bad?
21-
/// Methods of high cognitive complexity tend to be hard to
22-
/// both read and maintain. Also LLVM will tend to optimize small methods better.
23-
///
24-
/// ### Known problems
25-
/// Sometimes it's hard to find a way to reduce the
26-
/// complexity.
27-
///
28-
/// ### Example
29-
/// You'll see it when you get the warning.
30-
#[clippy::version = "1.35.0"]
31-
pub COGNITIVE_COMPLEXITY,
32-
nursery,
33-
"functions that should be split up into multiple functions"
34-
}
17+
use crate::LintInfo;
18+
19+
pub static COGNITIVE_COMPLEXITY: &Lint = &Lint {
20+
name: &"clippy::COGNITIVE_COMPLEXITY",
21+
default_level: Allow,
22+
desc: "functions that should be split up into multiple functions",
23+
edition_lint_opts: None,
24+
report_in_external_macro: true,
25+
future_incompatible: None,
26+
is_externally_loaded: true,
27+
crate_level_only: false,
28+
loadbearing: true,
29+
..Lint::default_fields_for_macro()
30+
};
31+
pub(crate) static COGNITIVE_COMPLEXITY_INFO: &'static LintInfo = &LintInfo {
32+
lint: &COGNITIVE_COMPLEXITY,
33+
category: crate::LintCategory::Nursery,
34+
explanation: r"### What it does
35+
Checks for methods with high cognitive complexity.
36+
37+
### Why is this bad?
38+
Methods of high cognitive complexity tend to be hard to both read and maintain.
39+
Also LLVM will tend to optimize small methods better.
40+
41+
### Known problems
42+
Sometimes it's hard to find a way to reduce the complexity.
43+
44+
### Example
45+
You'll see it when you get the warning.",
46+
version: Some("1.35.0"),
47+
location: "#L0",
48+
};
3549

3650
pub struct CognitiveComplexity {
3751
limit: LimitStack,

clippy_lints/src/ctfe.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use rustc_hir::def_id::LocalDefId;
2+
use rustc_hir::intravisit::FnKind;
3+
use rustc_hir::{Body, FnDecl};
4+
use rustc_lint::{LateContext, LateLintPass};
5+
use rustc_session::declare_lint_pass;
6+
use rustc_span::Span;
7+
8+
declare_clippy_lint! {
9+
/// ### What it does
10+
/// Checks for comparisons where one side of the relation is
11+
/// either the minimum or maximum value for its type and warns if it involves a
12+
/// case that is always true or always false. Only integer and boolean types are
13+
/// checked.
14+
///
15+
/// ### Why is this bad?
16+
/// An expression like `min <= x` may misleadingly imply
17+
/// that it is possible for `x` to be less than the minimum. Expressions like
18+
/// `max < x` are probably mistakes.
19+
///
20+
/// ### Known problems
21+
/// For `usize` the size of the current compile target will
22+
/// be assumed (e.g., 64 bits on 64 bit systems). This means code that uses such
23+
/// a comparison to detect target pointer width will trigger this lint. One can
24+
/// use `mem::sizeof` and compare its value or conditional compilation
25+
/// attributes
26+
/// like `#[cfg(target_pointer_width = "64")] ..` instead.
27+
///
28+
/// ### Example
29+
/// ```no_run
30+
/// let vec: Vec<isize> = Vec::new();
31+
/// if vec.len() <= 0 {}
32+
/// if 100 > i32::MAX {}
33+
/// ```
34+
#[clippy::version = "1.82.0"]
35+
pub CLIPPY_CTFE,
36+
correctness,
37+
"a comparison with a maximum or minimum value that is always true or false"
38+
}
39+
40+
declare_lint_pass! { ClippyCtfe => [CLIPPY_CTFE] }
41+
42+
impl<'tcx> LateLintPass<'tcx> for ClippyCtfe {
43+
fn check_fn(
44+
&mut self,
45+
cx: &LateContext<'_>,
46+
_: FnKind<'tcx>,
47+
_: &'tcx FnDecl<'tcx>,
48+
_: &'tcx Body<'tcx>,
49+
_: Span,
50+
defid: LocalDefId,
51+
) {
52+
cx.tcx.ensure().mir_drops_elaborated_and_const_checked(defid); // Lint
53+
}
54+
}

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 (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));

clippy_lints/src/utils/internal_lints/author.rs renamed to clippy_lints/src/utils/author.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_span::symbol::{Ident, Symbol};
1212
use std::cell::Cell;
1313
use std::fmt::{Display, Formatter, Write as _};
1414

15-
declare_clippy_lint!{
15+
declare_lint_pass!(
1616
/// ### What it does
1717
/// Generates clippy code that detects the offending pattern
1818
///
@@ -44,13 +44,8 @@ declare_clippy_lint!{
4444
/// // report your lint here
4545
/// }
4646
/// ```
47-
#[clippy::version = "1.0.0"]
48-
pub AUTHOR,
49-
internal,
50-
"The author lint, see documentation at <https://doc.rust-lang.org/nightly/clippy/development/adding_lints.html#author-lint>"
51-
};
52-
53-
declare_lint_pass! { Author => [AUTHOR] }
47+
Author => []
48+
);
5449

5550
/// Writes a line of output with indentation added
5651
macro_rules! out {
@@ -803,3 +798,4 @@ fn path_to_string(path: &QPath<'_>) -> Result<String, ()> {
803798
inner(&mut s, path)?;
804799
Ok(s)
805800
}
801+

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod author;
12
pub mod dump_hir;
23
pub mod format_args_collector;
34
#[cfg(feature = "internal")]

0 commit comments

Comments
 (0)