Skip to content

Commit f0edfab

Browse files
committed
Auto merge of #4230 - flip1995:unsugar_if, r=Manishearth
Replace `unsugar_if` function with `is_if` function cc #4123 (comment) changelog: none r? @Manishearth
2 parents 8c80b65 + 662037b commit f0edfab

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

clippy_lints/src/formatting.rs

+30-30
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use crate::utils::{differing_macro_contexts, in_macro_or_desugar, snippet_opt, s
22
use if_chain::if_chain;
33
use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintPass};
44
use rustc::{declare_lint_pass, declare_tool_lint};
5-
use syntax::ast;
6-
use syntax::ptr::P;
5+
use syntax::ast::*;
76

87
declare_clippy_lint! {
98
/// **What it does:** Checks for use of the non-existent `=*`, `=!` and `=-`
@@ -86,33 +85,33 @@ declare_lint_pass!(Formatting => [
8685
]);
8786

8887
impl EarlyLintPass for Formatting {
89-
fn check_block(&mut self, cx: &EarlyContext<'_>, block: &ast::Block) {
88+
fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
9089
for w in block.stmts.windows(2) {
9190
match (&w[0].node, &w[1].node) {
92-
(&ast::StmtKind::Expr(ref first), &ast::StmtKind::Expr(ref second))
93-
| (&ast::StmtKind::Expr(ref first), &ast::StmtKind::Semi(ref second)) => {
91+
(&StmtKind::Expr(ref first), &StmtKind::Expr(ref second))
92+
| (&StmtKind::Expr(ref first), &StmtKind::Semi(ref second)) => {
9493
check_missing_else(cx, first, second);
9594
},
9695
_ => (),
9796
}
9897
}
9998
}
10099

101-
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
100+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
102101
check_assign(cx, expr);
103102
check_else(cx, expr);
104103
check_array(cx, expr);
105104
}
106105
}
107106

108107
/// Implementation of the `SUSPICIOUS_ASSIGNMENT_FORMATTING` lint.
109-
fn check_assign(cx: &EarlyContext<'_>, expr: &ast::Expr) {
110-
if let ast::ExprKind::Assign(ref lhs, ref rhs) = expr.node {
108+
fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
109+
if let ExprKind::Assign(ref lhs, ref rhs) = expr.node {
111110
if !differing_macro_contexts(lhs.span, rhs.span) && !in_macro_or_desugar(lhs.span) {
112111
let eq_span = lhs.span.between(rhs.span);
113-
if let ast::ExprKind::Unary(op, ref sub_rhs) = rhs.node {
112+
if let ExprKind::Unary(op, ref sub_rhs) = rhs.node {
114113
if let Some(eq_snippet) = snippet_opt(cx, eq_span) {
115-
let op = ast::UnOp::to_string(op);
114+
let op = UnOp::to_string(op);
116115
let eqop_span = lhs.span.between(sub_rhs.span);
117116
if eq_snippet.ends_with('=') {
118117
span_note_and_lint(
@@ -135,10 +134,10 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &ast::Expr) {
135134
}
136135

137136
/// Implementation of the `SUSPICIOUS_ELSE_FORMATTING` lint for weird `else`.
138-
fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
137+
fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
139138
if_chain! {
140-
if let Some((then, &Some(ref else_))) = unsugar_if(expr);
141-
if is_block(else_) || unsugar_if(else_).is_some();
139+
if let ExprKind::If(_, then, Some(else_)) = &expr.node;
140+
if is_block(else_) || is_if(else_);
142141
if !differing_macro_contexts(then.span, else_.span);
143142
if !in_macro_or_desugar(then.span) && !in_external_macro(cx.sess, expr.span);
144143

@@ -154,7 +153,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
154153
if let Some(else_snippet) = snippet_opt(cx, else_span);
155154
if let Some(else_pos) = else_snippet.find("else");
156155
if else_snippet[else_pos..].contains('\n');
157-
let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" };
156+
let else_desc = if is_if(else_) { "if" } else { "{..}" };
158157

159158
then {
160159
span_note_and_lint(
@@ -173,16 +172,16 @@ fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
173172
}
174173
}
175174

176-
fn has_unary_equivalent(bin_op: ast::BinOpKind) -> bool {
175+
fn has_unary_equivalent(bin_op: BinOpKind) -> bool {
177176
// &, *, -
178-
bin_op == ast::BinOpKind::And || bin_op == ast::BinOpKind::Mul || bin_op == ast::BinOpKind::Sub
177+
bin_op == BinOpKind::And || bin_op == BinOpKind::Mul || bin_op == BinOpKind::Sub
179178
}
180179

181180
/// Implementation of the `POSSIBLE_MISSING_COMMA` lint for array
182-
fn check_array(cx: &EarlyContext<'_>, expr: &ast::Expr) {
183-
if let ast::ExprKind::Array(ref array) = expr.node {
181+
fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
182+
if let ExprKind::Array(ref array) = expr.node {
184183
for element in array {
185-
if let ast::ExprKind::Binary(ref op, ref lhs, _) = element.node {
184+
if let ExprKind::Binary(ref op, ref lhs, _) = element.node {
186185
if has_unary_equivalent(op.node) && !differing_macro_contexts(lhs.span, op.span) {
187186
let space_span = lhs.span.between(op.span);
188187
if let Some(space_snippet) = snippet_opt(cx, space_span) {
@@ -204,18 +203,18 @@ fn check_array(cx: &EarlyContext<'_>, expr: &ast::Expr) {
204203
}
205204
}
206205

207-
fn check_missing_else(cx: &EarlyContext<'_>, first: &ast::Expr, second: &ast::Expr) {
206+
fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
208207
if !differing_macro_contexts(first.span, second.span)
209208
&& !in_macro_or_desugar(first.span)
210-
&& unsugar_if(first).is_some()
211-
&& (is_block(second) || unsugar_if(second).is_some())
209+
&& is_if(first)
210+
&& (is_block(second) || is_if(second))
212211
{
213212
// where the else would be
214213
let else_span = first.span.between(second.span);
215214

216215
if let Some(else_snippet) = snippet_opt(cx, else_span) {
217216
if !else_snippet.contains('\n') {
218-
let (looks_like, next_thing) = if unsugar_if(second).is_some() {
217+
let (looks_like, next_thing) = if is_if(second) {
219218
("an `else if`", "the second `if`")
220219
} else {
221220
("an `else {..}`", "the next block")
@@ -237,18 +236,19 @@ fn check_missing_else(cx: &EarlyContext<'_>, first: &ast::Expr, second: &ast::Ex
237236
}
238237
}
239238

240-
fn is_block(expr: &ast::Expr) -> bool {
241-
if let ast::ExprKind::Block(..) = expr.node {
239+
fn is_block(expr: &Expr) -> bool {
240+
if let ExprKind::Block(..) = expr.node {
242241
true
243242
} else {
244243
false
245244
}
246245
}
247246

248-
/// Match `if` or `if let` expressions and return the `then` and `else` block.
249-
fn unsugar_if(expr: &ast::Expr) -> Option<(&P<ast::Block>, &Option<P<ast::Expr>>)> {
250-
match expr.node {
251-
ast::ExprKind::If(_, ref then, ref else_) => Some((then, else_)),
252-
_ => None,
247+
/// Check if the expression is an `if` or `if let`
248+
fn is_if(expr: &Expr) -> bool {
249+
if let ExprKind::If(..) = expr.node {
250+
true
251+
} else {
252+
false
253253
}
254254
}

0 commit comments

Comments
 (0)