Skip to content

Commit 945c944

Browse files
committed
Merge block_in_if_condition_expr and block_in_if_condition_stmt lints into block_in_if_condition lint
1 parent e1842b0 commit 945c944

9 files changed

+41
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,8 +1274,7 @@ Released 2018-09-13
12741274
[`await_holding_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_lock
12751275
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask
12761276
[`blacklisted_name`]: https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name
1277-
[`block_in_if_condition_expr`]: https://rust-lang.github.io/rust-clippy/master/index.html#block_in_if_condition_expr
1278-
[`block_in_if_condition_stmt`]: https://rust-lang.github.io/rust-clippy/master/index.html#block_in_if_condition_stmt
1277+
[`block_in_if_condition`]: https://rust-lang.github.io/rust-clippy/master/index.html#block_in_if_condition
12791278
[`bool_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
12801279
[`borrow_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const
12811280
[`borrowed_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box

clippy_lints/src/block_in_if_condition.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,40 @@ use rustc_middle::lint::in_external_macro;
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99

1010
declare_clippy_lint! {
11-
/// **What it does:** Checks for `if` conditions that use blocks to contain an
12-
/// expression.
11+
/// **What it does:** Checks for `if` conditions that use blocks containing an
12+
/// expression, statements or conditions that use closures with blocks.
1313
///
14-
/// **Why is this bad?** It isn't really Rust style, same as using parentheses
15-
/// to contain expressions.
14+
/// **Why is this bad?** Style, using blocks in the condition makes it hard to read.
1615
///
1716
/// **Known problems:** None.
1817
///
19-
/// **Example:**
18+
/// **Examples:**
2019
/// ```rust
20+
/// // Bad
2121
/// if { true } { /* ... */ }
22+
///
23+
/// // Good
24+
/// if true { /* ... */ }
2225
/// ```
23-
pub BLOCK_IN_IF_CONDITION_EXPR,
24-
style,
25-
"braces that can be eliminated in conditions, e.g., `if { true } ...`"
26-
}
27-
28-
declare_clippy_lint! {
29-
/// **What it does:** Checks for `if` conditions that use blocks containing
30-
/// statements, or conditions that use closures with blocks.
3126
///
32-
/// **Why is this bad?** Using blocks in the condition makes it hard to read.
27+
/// // or
3328
///
34-
/// **Known problems:** None.
29+
/// ```rust
30+
/// # fn somefunc() -> bool { true };
3531
///
36-
/// **Example:**
37-
/// ```rust,ignore
38-
/// if { let x = somefunc(); x } {}
39-
/// // or
40-
/// if somefunc(|x| { x == 47 }) {}
32+
/// // Bad
33+
/// if { let x = somefunc(); x } { /* ... */ }
34+
///
35+
/// // Good
36+
/// let res = { let x = somefunc(); x };
37+
/// if res { /* ... */ }
4138
/// ```
42-
pub BLOCK_IN_IF_CONDITION_STMT,
39+
pub BLOCK_IN_IF_CONDITION,
4340
style,
44-
"complex blocks in conditions, e.g., `if { let x = true; x } ...`"
41+
"useless or complex blocks that can be eliminated in conditions"
4542
}
4643

47-
declare_lint_pass!(BlockInIfCondition => [BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT]);
44+
declare_lint_pass!(BlockInIfCondition => [BLOCK_IN_IF_CONDITION]);
4845

4946
struct ExVisitor<'a, 'tcx> {
5047
found_block: Option<&'tcx Expr<'tcx>>,
@@ -72,7 +69,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
7269

7370
const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
7471
const COMPLEX_BLOCK_MESSAGE: &str = "in an `if` condition, avoid complex blocks or closures with blocks; \
75-
instead, move the block or closure higher and bind it with a `let`";
72+
instead, move the block or closure higher and bind it with a `let`";
7673

7774
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
7875
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
@@ -92,7 +89,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
9289
let mut applicability = Applicability::MachineApplicable;
9390
span_lint_and_sugg(
9491
cx,
95-
BLOCK_IN_IF_CONDITION_EXPR,
92+
BLOCK_IN_IF_CONDITION,
9693
cond.span,
9794
BRACED_EXPR_MESSAGE,
9895
"try",
@@ -118,7 +115,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
118115
let mut applicability = Applicability::MachineApplicable;
119116
span_lint_and_sugg(
120117
cx,
121-
BLOCK_IN_IF_CONDITION_STMT,
118+
BLOCK_IN_IF_CONDITION,
122119
expr.span.with_hi(cond.span.hi()),
123120
COMPLEX_BLOCK_MESSAGE,
124121
"try",
@@ -140,7 +137,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
140137
let mut visitor = ExVisitor { found_block: None, cx };
141138
walk_expr(&mut visitor, cond);
142139
if let Some(block) = visitor.found_block {
143-
span_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE);
140+
span_lint(cx, BLOCK_IN_IF_CONDITION, block.span, COMPLEX_BLOCK_MESSAGE);
144141
}
145142
}
146143
}

clippy_lints/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
507507
&bit_mask::INEFFECTIVE_BIT_MASK,
508508
&bit_mask::VERBOSE_BIT_MASK,
509509
&blacklisted_name::BLACKLISTED_NAME,
510-
&block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR,
511-
&block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT,
510+
&block_in_if_condition::BLOCK_IN_IF_CONDITION,
512511
&booleans::LOGIC_BUG,
513512
&booleans::NONMINIMAL_BOOL,
514513
&bytecount::NAIVE_BYTECOUNT,
@@ -1209,8 +1208,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12091208
LintId::of(&bit_mask::INEFFECTIVE_BIT_MASK),
12101209
LintId::of(&bit_mask::VERBOSE_BIT_MASK),
12111210
LintId::of(&blacklisted_name::BLACKLISTED_NAME),
1212-
LintId::of(&block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR),
1213-
LintId::of(&block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT),
1211+
LintId::of(&block_in_if_condition::BLOCK_IN_IF_CONDITION),
12141212
LintId::of(&booleans::LOGIC_BUG),
12151213
LintId::of(&booleans::NONMINIMAL_BOOL),
12161214
LintId::of(&bytecount::NAIVE_BYTECOUNT),
@@ -1456,8 +1454,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14561454
LintId::of(&attrs::UNKNOWN_CLIPPY_LINTS),
14571455
LintId::of(&bit_mask::VERBOSE_BIT_MASK),
14581456
LintId::of(&blacklisted_name::BLACKLISTED_NAME),
1459-
LintId::of(&block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR),
1460-
LintId::of(&block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT),
1457+
LintId::of(&block_in_if_condition::BLOCK_IN_IF_CONDITION),
14611458
LintId::of(&collapsible_if::COLLAPSIBLE_IF),
14621459
LintId::of(&comparison_chain::COMPARISON_CHAIN),
14631460
LintId::of(&doc::MISSING_SAFETY_DOC),

src/lintlist/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,9 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
7474
module: "blacklisted_name",
7575
},
7676
Lint {
77-
name: "block_in_if_condition_expr",
77+
name: "block_in_if_condition",
7878
group: "style",
79-
desc: "braces that can be eliminated in conditions, e.g., `if { true } ...`",
80-
deprecation: None,
81-
module: "block_in_if_condition",
82-
},
83-
Lint {
84-
name: "block_in_if_condition_stmt",
85-
group: "style",
86-
desc: "complex blocks in conditions, e.g., `if { let x = true; x } ...`",
79+
desc: "useless or complex blocks that can be eliminated in conditions",
8780
deprecation: None,
8881
module: "block_in_if_condition",
8982
},

tests/ui/block_in_if_condition.fixed

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// run-rustfix
2-
#![warn(clippy::block_in_if_condition_expr)]
3-
#![warn(clippy::block_in_if_condition_stmt)]
2+
#![warn(clippy::block_in_if_condition)]
43
#![allow(unused, clippy::let_and_return)]
54
#![warn(clippy::nonminimal_bool)]
65

tests/ui/block_in_if_condition.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// run-rustfix
2-
#![warn(clippy::block_in_if_condition_expr)]
3-
#![warn(clippy::block_in_if_condition_stmt)]
2+
#![warn(clippy::block_in_if_condition)]
43
#![allow(unused, clippy::let_and_return)]
54
#![warn(clippy::nonminimal_bool)]
65

tests/ui/block_in_if_condition.stderr

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
2-
--> $DIR/block_in_if_condition.rs:27:5
2+
--> $DIR/block_in_if_condition.rs:26:5
33
|
44
LL | / if {
55
LL | | let x = 3;
66
LL | | x == 3
77
LL | | } {
88
| |_____^
99
|
10-
= note: `-D clippy::block-in-if-condition-stmt` implied by `-D warnings`
10+
= note: `-D clippy::block-in-if-condition` implied by `-D warnings`
1111
help: try
1212
|
1313
LL | let res = {
@@ -17,15 +17,13 @@ LL | }; if res {
1717
|
1818

1919
error: omit braces around single expression condition
20-
--> $DIR/block_in_if_condition.rs:38:8
20+
--> $DIR/block_in_if_condition.rs:37:8
2121
|
2222
LL | if { true } {
2323
| ^^^^^^^^ help: try: `true`
24-
|
25-
= note: `-D clippy::block-in-if-condition-expr` implied by `-D warnings`
2624

2725
error: this boolean expression can be simplified
28-
--> $DIR/block_in_if_condition.rs:47:8
26+
--> $DIR/block_in_if_condition.rs:46:8
2927
|
3028
LL | if true && x == 3 {
3129
| ^^^^^^^^^^^^^^ help: try: `x == 3`

tests/ui/block_in_if_condition_closure.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#![warn(clippy::block_in_if_condition_expr)]
2-
#![warn(clippy::block_in_if_condition_stmt)]
1+
#![warn(clippy::block_in_if_condition)]
32
#![allow(unused, clippy::let_and_return)]
43

54
fn predicate<F: FnOnce(T) -> bool, T>(pfn: F, val: T) -> bool {
@@ -10,7 +9,7 @@ fn pred_test() {
109
let v = 3;
1110
let sky = "blue";
1211
// This is a sneaky case, where the block isn't directly in the condition,
13-
// but is actually nside a closure that the condition is using.
12+
// but is actually inside a closure that the condition is using.
1413
// The same principle applies -- add some extra expressions to make sure
1514
// linter isn't confused by them.
1615
if v == 3

tests/ui/block_in_if_condition_closure.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
2-
--> $DIR/block_in_if_condition_closure.rs:19:17
2+
--> $DIR/block_in_if_condition_closure.rs:18:17
33
|
44
LL | |x| {
55
| _________________^
@@ -8,10 +8,10 @@ LL | | x == target
88
LL | | },
99
| |_____________^
1010
|
11-
= note: `-D clippy::block-in-if-condition-stmt` implied by `-D warnings`
11+
= note: `-D clippy::block-in-if-condition` implied by `-D warnings`
1212

1313
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
14-
--> $DIR/block_in_if_condition_closure.rs:28:13
14+
--> $DIR/block_in_if_condition_closure.rs:27:13
1515
|
1616
LL | |x| {
1717
| _____________^

0 commit comments

Comments
 (0)