Skip to content

Commit 19f08c2

Browse files
committed
Make block_in_if_condition auto applicable
1 parent 5fd22b3 commit 19f08c2

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

clippy_lints/src/block_in_if_condition.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::utils::*;
22
use matches::matches;
33
use rustc::hir::map::Map;
44
use rustc::lint::in_external_macro;
5+
use rustc_errors::Applicability;
56
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
67
use rustc_hir::*;
78
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -79,8 +80,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
7980
if in_external_macro(cx.sess(), expr.span) {
8081
return;
8182
}
82-
if let Some((check, then, _)) = higher::if_block(&expr) {
83-
if let ExprKind::Block(block, _) = &check.kind {
83+
if let Some((cond, _, _)) = higher::if_block(&expr) {
84+
if let ExprKind::Block(block, _) = &cond.kind {
8485
if block.rules == BlockCheckMode::DefaultBlock {
8586
if block.stmts.is_empty() {
8687
if let Some(ex) = &block.expr {
@@ -89,16 +90,24 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
8990
if expr.span.from_expansion() || differing_macro_contexts(expr.span, ex.span) {
9091
return;
9192
}
92-
span_lint_and_help(
93+
let mut applicability = Applicability::MachineApplicable;
94+
span_lint_and_sugg(
9395
cx,
9496
BLOCK_IN_IF_CONDITION_EXPR,
95-
check.span,
97+
cond.span,
9698
BRACED_EXPR_MESSAGE,
97-
&format!(
98-
"try\nif {} {} ... ",
99-
snippet_block(cx, ex.span, ".."),
100-
snippet_block(cx, then.span, "..")
99+
"try",
100+
format!(
101+
"{}",
102+
snippet_block_with_applicability(
103+
cx,
104+
ex.span,
105+
"..",
106+
Some(expr.span),
107+
&mut applicability
108+
)
101109
),
110+
applicability,
102111
);
103112
}
104113
} else {
@@ -107,22 +116,30 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
107116
return;
108117
}
109118
// move block higher
110-
span_lint_and_help(
119+
let mut applicability = Applicability::MachineApplicable;
120+
span_lint_and_sugg(
111121
cx,
112122
BLOCK_IN_IF_CONDITION_STMT,
113-
check.span,
123+
expr.span.with_hi(cond.span.hi()),
114124
COMPLEX_BLOCK_MESSAGE,
115-
&format!(
116-
"try\nlet res = {};\nif res {} ... ",
117-
snippet_block(cx, block.span, ".."),
118-
snippet_block(cx, then.span, "..")
125+
"try",
126+
format!(
127+
"let res = {}; if res",
128+
snippet_block_with_applicability(
129+
cx,
130+
block.span,
131+
"..",
132+
Some(expr.span),
133+
&mut applicability
134+
),
119135
),
136+
applicability,
120137
);
121138
}
122139
}
123140
} else {
124141
let mut visitor = ExVisitor { found_block: None, cx };
125-
walk_expr(&mut visitor, check);
142+
walk_expr(&mut visitor, cond);
126143
if let Some(block) = visitor.found_block {
127144
span_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE);
128145
}

0 commit comments

Comments
 (0)