Skip to content

Commit dd1929e

Browse files
committed
Auto merge of rust-lang#6544 - matthiaskrgr:else_if, r=flip1995
collapsible_if: split collapsible_else_if into its own lint so we can enable/disable it particularly This splits up clippy::collapsible_if into collapsible_if for if x { if y { } } => if x && y { } and collapsible_else_if for if x { } else { if y { } } => if x { } else if y { } so that we can lint for only the latter but not the first if we desire. changelog: collapsible_if: split up linting for if x {} else { if y {} } into collapsible_else_if lint
2 parents 8951916 + 6dcec6a commit dd1929e

File tree

8 files changed

+59
-34
lines changed

8 files changed

+59
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,7 @@ Released 2018-09-13
18961896
[`cmp_null`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_null
18971897
[`cmp_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_owned
18981898
[`cognitive_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity
1899+
[`collapsible_else_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_else_if
18991900
[`collapsible_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
19001901
[`collapsible_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_match
19011902
[`comparison_chain`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain

clippy_lints/src/collapsible_if.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ use rustc_errors::Applicability;
2323

2424
declare_clippy_lint! {
2525
/// **What it does:** Checks for nested `if` statements which can be collapsed
26-
/// by `&&`-combining their conditions and for `else { if ... }` expressions
27-
/// that
28-
/// can be collapsed to `else if ...`.
26+
/// by `&&`-combining their conditions.
2927
///
3028
/// **Why is this bad?** Each `if`-statement adds one level of nesting, which
3129
/// makes code look more complex than it really is.
@@ -40,7 +38,31 @@ declare_clippy_lint! {
4038
/// }
4139
/// }
4240
///
43-
/// // or
41+
/// ```
42+
///
43+
/// Should be written:
44+
///
45+
/// ```rust.ignore
46+
/// if x && y {
47+
/// …
48+
/// }
49+
/// ```
50+
pub COLLAPSIBLE_IF,
51+
style,
52+
"nested `if`s that can be collapsed (e.g., `if x { if y { ... } }`"
53+
}
54+
55+
declare_clippy_lint! {
56+
/// **What it does:** Checks for collapsible `else { if ... }` expressions
57+
/// that can be collapsed to `else if ...`.
58+
///
59+
/// **Why is this bad?** Each `if`-statement adds one level of nesting, which
60+
/// makes code look more complex than it really is.
61+
///
62+
/// **Known problems:** None.
63+
///
64+
/// **Example:**
65+
/// ```rust,ignore
4466
///
4567
/// if x {
4668
/// …
@@ -54,24 +76,18 @@ declare_clippy_lint! {
5476
/// Should be written:
5577
///
5678
/// ```rust.ignore
57-
/// if x && y {
58-
/// …
59-
/// }
60-
///
61-
/// // or
62-
///
6379
/// if x {
6480
/// …
6581
/// } else if y {
6682
/// …
6783
/// }
6884
/// ```
69-
pub COLLAPSIBLE_IF,
85+
pub COLLAPSIBLE_ELSE_IF,
7086
style,
71-
"`if`s that can be collapsed (e.g., `if x { if y { ... } }` and `else { if x { ... } }`)"
87+
"nested `else`-`if` expressions that can be collapsed (e.g., `else { if x { ... } }`)"
7288
}
7389

74-
declare_lint_pass!(CollapsibleIf => [COLLAPSIBLE_IF]);
90+
declare_lint_pass!(CollapsibleIf => [COLLAPSIBLE_IF, COLLAPSIBLE_ELSE_IF]);
7591

7692
impl EarlyLintPass for CollapsibleIf {
7793
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
@@ -112,7 +128,7 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
112128
let mut applicability = Applicability::MachineApplicable;
113129
span_lint_and_sugg(
114130
cx,
115-
COLLAPSIBLE_IF,
131+
COLLAPSIBLE_ELSE_IF,
116132
block.span,
117133
"this `else { if .. }` block can be collapsed",
118134
"collapse nested if block",

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
556556
&cargo_common_metadata::CARGO_COMMON_METADATA,
557557
&checked_conversions::CHECKED_CONVERSIONS,
558558
&cognitive_complexity::COGNITIVE_COMPLEXITY,
559+
&collapsible_if::COLLAPSIBLE_ELSE_IF,
559560
&collapsible_if::COLLAPSIBLE_IF,
560561
&collapsible_match::COLLAPSIBLE_MATCH,
561562
&comparison_chain::COMPARISON_CHAIN,
@@ -1384,6 +1385,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13841385
LintId::of(&booleans::LOGIC_BUG),
13851386
LintId::of(&booleans::NONMINIMAL_BOOL),
13861387
LintId::of(&bytecount::NAIVE_BYTECOUNT),
1388+
LintId::of(&collapsible_if::COLLAPSIBLE_ELSE_IF),
13871389
LintId::of(&collapsible_if::COLLAPSIBLE_IF),
13881390
LintId::of(&collapsible_match::COLLAPSIBLE_MATCH),
13891391
LintId::of(&comparison_chain::COMPARISON_CHAIN),
@@ -1653,6 +1655,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16531655
LintId::of(&attrs::UNKNOWN_CLIPPY_LINTS),
16541656
LintId::of(&blacklisted_name::BLACKLISTED_NAME),
16551657
LintId::of(&blocks_in_if_conditions::BLOCKS_IN_IF_CONDITIONS),
1658+
LintId::of(&collapsible_if::COLLAPSIBLE_ELSE_IF),
16561659
LintId::of(&collapsible_if::COLLAPSIBLE_IF),
16571660
LintId::of(&collapsible_match::COLLAPSIBLE_MATCH),
16581661
LintId::of(&comparison_chain::COMPARISON_CHAIN),

tests/ui/collapsible_else_if.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#[rustfmt::skip]
55
#[warn(clippy::collapsible_if)]
6+
#[warn(clippy::collapsible_else_if)]
7+
68
fn main() {
79
let x = "hello";
810
let y = "world";

tests/ui/collapsible_else_if.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#[rustfmt::skip]
55
#[warn(clippy::collapsible_if)]
6+
#[warn(clippy::collapsible_else_if)]
7+
68
fn main() {
79
let x = "hello";
810
let y = "world";

tests/ui/collapsible_else_if.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this `else { if .. }` block can be collapsed
2-
--> $DIR/collapsible_else_if.rs:12:12
2+
--> $DIR/collapsible_else_if.rs:14:12
33
|
44
LL | } else {
55
| ____________^
@@ -9,7 +9,7 @@ LL | | }
99
LL | | }
1010
| |_____^
1111
|
12-
= note: `-D clippy::collapsible-if` implied by `-D warnings`
12+
= note: `-D clippy::collapsible-else-if` implied by `-D warnings`
1313
help: collapse nested if block
1414
|
1515
LL | } else if y == "world" {
@@ -18,7 +18,7 @@ LL | }
1818
|
1919

2020
error: this `else { if .. }` block can be collapsed
21-
--> $DIR/collapsible_else_if.rs:20:12
21+
--> $DIR/collapsible_else_if.rs:22:12
2222
|
2323
LL | } else {
2424
| ____________^
@@ -36,7 +36,7 @@ LL | }
3636
|
3737

3838
error: this `else { if .. }` block can be collapsed
39-
--> $DIR/collapsible_else_if.rs:28:12
39+
--> $DIR/collapsible_else_if.rs:30:12
4040
|
4141
LL | } else {
4242
| ____________^
@@ -59,7 +59,7 @@ LL | }
5959
|
6060

6161
error: this `else { if .. }` block can be collapsed
62-
--> $DIR/collapsible_else_if.rs:39:12
62+
--> $DIR/collapsible_else_if.rs:41:12
6363
|
6464
LL | } else {
6565
| ____________^
@@ -82,7 +82,7 @@ LL | }
8282
|
8383

8484
error: this `else { if .. }` block can be collapsed
85-
--> $DIR/collapsible_else_if.rs:50:12
85+
--> $DIR/collapsible_else_if.rs:52:12
8686
|
8787
LL | } else {
8888
| ____________^
@@ -105,7 +105,7 @@ LL | }
105105
|
106106

107107
error: this `else { if .. }` block can be collapsed
108-
--> $DIR/collapsible_else_if.rs:61:12
108+
--> $DIR/collapsible_else_if.rs:63:12
109109
|
110110
LL | } else {
111111
| ____________^
@@ -128,7 +128,7 @@ LL | }
128128
|
129129

130130
error: this `else { if .. }` block can be collapsed
131-
--> $DIR/collapsible_else_if.rs:72:12
131+
--> $DIR/collapsible_else_if.rs:74:12
132132
|
133133
LL | } else {
134134
| ____________^

tests/ui/if_same_then_else2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![warn(clippy::if_same_then_else)]
22
#![allow(
33
clippy::blacklisted_name,
4+
clippy::collapsible_else_if,
45
clippy::collapsible_if,
56
clippy::ifs_same_cond,
67
clippy::needless_return,

tests/ui/if_same_then_else2.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this `if` has identical blocks
2-
--> $DIR/if_same_then_else2.rs:20:12
2+
--> $DIR/if_same_then_else2.rs:21:12
33
|
44
LL | } else {
55
| ____________^
@@ -13,7 +13,7 @@ LL | | }
1313
|
1414
= note: `-D clippy::if-same-then-else` implied by `-D warnings`
1515
note: same as this
16-
--> $DIR/if_same_then_else2.rs:11:13
16+
--> $DIR/if_same_then_else2.rs:12:13
1717
|
1818
LL | if true {
1919
| _____________^
@@ -26,7 +26,7 @@ LL | | } else {
2626
| |_____^
2727

2828
error: this `if` has identical blocks
29-
--> $DIR/if_same_then_else2.rs:34:12
29+
--> $DIR/if_same_then_else2.rs:35:12
3030
|
3131
LL | } else {
3232
| ____________^
@@ -36,7 +36,7 @@ LL | | }
3636
| |_____^
3737
|
3838
note: same as this
39-
--> $DIR/if_same_then_else2.rs:32:13
39+
--> $DIR/if_same_then_else2.rs:33:13
4040
|
4141
LL | if true {
4242
| _____________^
@@ -45,7 +45,7 @@ LL | | } else {
4545
| |_____^
4646

4747
error: this `if` has identical blocks
48-
--> $DIR/if_same_then_else2.rs:41:12
48+
--> $DIR/if_same_then_else2.rs:42:12
4949
|
5050
LL | } else {
5151
| ____________^
@@ -55,7 +55,7 @@ LL | | }
5555
| |_____^
5656
|
5757
note: same as this
58-
--> $DIR/if_same_then_else2.rs:39:13
58+
--> $DIR/if_same_then_else2.rs:40:13
5959
|
6060
LL | if true {
6161
| _____________^
@@ -64,7 +64,7 @@ LL | | } else {
6464
| |_____^
6565

6666
error: this `if` has identical blocks
67-
--> $DIR/if_same_then_else2.rs:91:12
67+
--> $DIR/if_same_then_else2.rs:92:12
6868
|
6969
LL | } else {
7070
| ____________^
@@ -74,7 +74,7 @@ LL | | };
7474
| |_____^
7575
|
7676
note: same as this
77-
--> $DIR/if_same_then_else2.rs:89:21
77+
--> $DIR/if_same_then_else2.rs:90:21
7878
|
7979
LL | let _ = if true {
8080
| _____________________^
@@ -83,7 +83,7 @@ LL | | } else {
8383
| |_____^
8484

8585
error: this `if` has identical blocks
86-
--> $DIR/if_same_then_else2.rs:98:12
86+
--> $DIR/if_same_then_else2.rs:99:12
8787
|
8888
LL | } else {
8989
| ____________^
@@ -93,7 +93,7 @@ LL | | }
9393
| |_____^
9494
|
9595
note: same as this
96-
--> $DIR/if_same_then_else2.rs:96:13
96+
--> $DIR/if_same_then_else2.rs:97:13
9797
|
9898
LL | if true {
9999
| _____________^
@@ -102,7 +102,7 @@ LL | | } else {
102102
| |_____^
103103

104104
error: this `if` has identical blocks
105-
--> $DIR/if_same_then_else2.rs:123:12
105+
--> $DIR/if_same_then_else2.rs:124:12
106106
|
107107
LL | } else {
108108
| ____________^
@@ -112,7 +112,7 @@ LL | | }
112112
| |_____^
113113
|
114114
note: same as this
115-
--> $DIR/if_same_then_else2.rs:120:20
115+
--> $DIR/if_same_then_else2.rs:121:20
116116
|
117117
LL | } else if true {
118118
| ____________________^

0 commit comments

Comments
 (0)