Skip to content

Commit 8625c3f

Browse files
committed
Changed Cognitive Complexity as per issue 3793
* CoC is now a pre-expansion Early pass. * The full draft of issue 3793 has been implemented. * `compile-test.rs` now allows `clippy::cognitive_complexity` in ui tests by default. This is because most ui tests don't use functions in a standard manner, and tend to have rather large ones. * Tests for CoC have been updated.
1 parent fbb3a47 commit 8625c3f

File tree

213 files changed

+3289
-3521
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

213 files changed

+3289
-3521
lines changed

.cargo/config

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[alias]
22
uitest = "test --test compile-test"
3+
4+
[build]
5+
rustflags = ["-Zunstable-options"]

.github/PULL_REQUEST_TEMPLATE

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!--
2+
Thank you for making Clippy better!
3+
4+
We're collecting our changelog from pull request descriptions.
5+
If your PR only updates to the latest nightly, you can leave the
6+
`changelog` entry as `none`. Otherwise, please write a short comment
7+
explaining your change.
8+
9+
If your PR fixes an issue, you can add "fixes #issue_number" into this
10+
PR description. This way the issue will be automatically closed when
11+
your PR is merged.
12+
13+
If you added a new lint, here's a checklist for things that will be
14+
checked during review or continuous integration.
15+
16+
- [ ] Followed [lint naming conventions][lint_naming]
17+
- [ ] Added passing UI tests (including committed `.stderr` file)
18+
- [ ] `cargo test` passes locally
19+
- [ ] Executed `util/dev update_lints`
20+
- [ ] Added lint documentation
21+
- [ ] Run `cargo fmt`
22+
23+
Note that you can skip the above if you are just opening a WIP PR in
24+
order to get feedback.
25+
26+
Delete this line and everything above before opening your PR -->
27+
28+
changelog: none

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ All notable changes to this project will be documented in this file.
10151015
[`panic_params`]: https://rust-lang.github.io/rust-clippy/master/index.html#panic_params
10161016
[`panicking_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#panicking_unwrap
10171017
[`partialeq_ne_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_ne_impl
1018+
[`path_buf_push_overwrite`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_buf_push_overwrite
10181019
[`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma
10191020
[`precedence`]: https://rust-lang.github.io/rust-clippy/master/index.html#precedence
10201021
[`print_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_literal

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ rustc_tools_util = { version = "0.1.1", path = "rustc_tools_util"}
4646

4747
[dev-dependencies]
4848
cargo_metadata = "0.7.1"
49-
compiletest_rs = { version = "0.3.21", features = ["tmp"] }
49+
compiletest_rs = { version = "0.3.22", features = ["tmp"] }
5050
lazy_static = "1.0"
5151
serde_derive = "1.0"
5252
clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
99

10-
[There are 298 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
10+
[There are 299 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1111

1212
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1313

clippy_dev/rust-toolchain

-1
This file was deleted.

clippy_dev/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(clippy::default_hash_types)]
2-
31
use itertools::Itertools;
42
use lazy_static::lazy_static;
53
use regex::Regex;

clippy_lints/src/approx_const.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::span_lint;
22
use rustc::hir::*;
33
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4-
use rustc::{declare_tool_lint, lint_array};
4+
use rustc::{declare_lint_pass, declare_tool_lint};
55
use std::f64::consts as f64;
66
use syntax::ast::{FloatTy, Lit, LitKind};
77
use syntax::symbol;
@@ -53,20 +53,9 @@ const KNOWN_CONSTS: &[(f64, &str, usize)] = &[
5353
(f64::SQRT_2, "SQRT_2", 5),
5454
];
5555

56-
#[derive(Copy, Clone)]
57-
pub struct Pass;
56+
declare_lint_pass!(ApproxConstant => [APPROX_CONSTANT]);
5857

59-
impl LintPass for Pass {
60-
fn get_lints(&self) -> LintArray {
61-
lint_array!(APPROX_CONSTANT)
62-
}
63-
64-
fn name(&self) -> &'static str {
65-
"ApproxConstant"
66-
}
67-
}
68-
69-
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
58+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ApproxConstant {
7059
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
7160
if let ExprKind::Lit(lit) = &e.node {
7261
check_lit(cx, lit, e);

clippy_lints/src/arithmetic.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::consts::constant_simple;
22
use crate::utils::span_lint;
33
use rustc::hir;
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5-
use rustc::{declare_tool_lint, lint_array};
5+
use rustc::{declare_tool_lint, impl_lint_pass};
66
use syntax::source_map::Span;
77

88
declare_clippy_lint! {
@@ -48,15 +48,7 @@ pub struct Arithmetic {
4848
const_span: Option<Span>,
4949
}
5050

51-
impl LintPass for Arithmetic {
52-
fn get_lints(&self) -> LintArray {
53-
lint_array!(INTEGER_ARITHMETIC, FLOAT_ARITHMETIC)
54-
}
55-
56-
fn name(&self) -> &'static str {
57-
"Arithmetic"
58-
}
59-
}
51+
impl_lint_pass!(Arithmetic => [INTEGER_ARITHMETIC, FLOAT_ARITHMETIC]);
6052

6153
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
6254
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {

clippy_lints/src/assertions_on_constants.rs

+32-43
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use if_chain::if_chain;
22
use rustc::hir::{Expr, ExprKind};
33
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4-
use rustc::{declare_tool_lint, lint_array};
4+
use rustc::{declare_lint_pass, declare_tool_lint};
5+
use syntax_pos::Span;
56

67
use crate::consts::{constant, Constant};
7-
use crate::syntax::ast::LitKind;
88
use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
99

1010
declare_clippy_lint! {
@@ -29,55 +29,44 @@ declare_clippy_lint! {
2929
"`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
3030
}
3131

32-
pub struct AssertionsOnConstants;
33-
34-
impl LintPass for AssertionsOnConstants {
35-
fn get_lints(&self) -> LintArray {
36-
lint_array![ASSERTIONS_ON_CONSTANTS]
37-
}
38-
39-
fn name(&self) -> &'static str {
40-
"AssertionsOnConstants"
41-
}
42-
}
32+
declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
4333

4434
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
4535
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
36+
let mut is_debug_assert = false;
37+
let debug_assert_not_in_macro = |span: Span| {
38+
is_debug_assert = true;
39+
// Check that `debug_assert!` itself is not inside a macro
40+
!in_macro(span)
41+
};
4642
if_chain! {
4743
if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
4844
if !in_macro(assert_span)
49-
|| is_direct_expn_of(assert_span, "debug_assert").map_or(false, |span| !in_macro(span));
45+
|| is_direct_expn_of(assert_span, "debug_assert")
46+
.map_or(false, debug_assert_not_in_macro);
5047
if let ExprKind::Unary(_, ref lit) = e.node;
48+
if let Some(bool_const) = constant(cx, cx.tables, lit);
5149
then {
52-
if let ExprKind::Lit(ref inner) = lit.node {
53-
match inner.node {
54-
LitKind::Bool(true) => {
55-
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
56-
"assert!(true) will be optimized out by the compiler",
57-
"remove it");
58-
},
59-
LitKind::Bool(false) => {
60-
span_help_and_lint(
61-
cx, ASSERTIONS_ON_CONSTANTS, e.span,
62-
"assert!(false) should probably be replaced",
63-
"use panic!() or unreachable!()");
64-
},
65-
_ => (),
66-
}
67-
} else if let Some(bool_const) = constant(cx, cx.tables, lit) {
68-
match bool_const.0 {
69-
Constant::Bool(true) => {
70-
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
71-
"assert!(const: true) will be optimized out by the compiler",
72-
"remove it");
73-
},
74-
Constant::Bool(false) => {
75-
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
76-
"assert!(const: false) should probably be replaced",
77-
"use panic!() or unreachable!()");
78-
},
79-
_ => (),
80-
}
50+
match bool_const.0 {
51+
Constant::Bool(true) => {
52+
span_help_and_lint(
53+
cx,
54+
ASSERTIONS_ON_CONSTANTS,
55+
e.span,
56+
"`assert!(true)` will be optimized out by the compiler",
57+
"remove it"
58+
);
59+
},
60+
Constant::Bool(false) if !is_debug_assert => {
61+
span_help_and_lint(
62+
cx,
63+
ASSERTIONS_ON_CONSTANTS,
64+
e.span,
65+
"`assert!(false)` should probably be replaced",
66+
"use `panic!()` or `unreachable!()`"
67+
);
68+
},
69+
_ => (),
8170
}
8271
}
8372
}

clippy_lints/src/assign_ops.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use if_chain::if_chain;
22
use rustc::hir;
33
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5-
use rustc::{declare_tool_lint, lint_array};
5+
use rustc::{declare_lint_pass, declare_tool_lint};
66
use rustc_errors::Applicability;
77

88
use crate::utils::{
@@ -53,18 +53,7 @@ declare_clippy_lint! {
5353
"having a variable on both sides of an assign op"
5454
}
5555

56-
#[derive(Copy, Clone, Default)]
57-
pub struct AssignOps;
58-
59-
impl LintPass for AssignOps {
60-
fn get_lints(&self) -> LintArray {
61-
lint_array!(ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP)
62-
}
63-
64-
fn name(&self) -> &'static str {
65-
"AssignOps"
66-
}
67-
}
56+
declare_lint_pass!(AssignOps => [ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP]);
6857

6958
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
7059
#[allow(clippy::too_many_lines)]

0 commit comments

Comments
 (0)