Skip to content

Commit 662037b

Browse files
committed
Simplify unsugar_if to just an if expr check
1 parent c192c07 commit 662037b

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

clippy_lints/src/formatting.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ 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};
55
use syntax::ast::*;
6-
use syntax::ptr::P;
76

87
declare_clippy_lint! {
98
/// **What it does:** Checks for use of the non-existent `=*`, `=!` and `=-`
@@ -137,8 +136,8 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
137136
/// Implementation of the `SUSPICIOUS_ELSE_FORMATTING` lint for weird `else`.
138137
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: &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(
@@ -207,15 +206,15 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
207206
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")
@@ -245,10 +244,11 @@ fn is_block(expr: &Expr) -> bool {
245244
}
246245
}
247246

248-
/// Match `if` or `if let` expressions and return the `then` and `else` block.
249-
fn unsugar_if(expr: &Expr) -> Option<(&P<Block>, &Option<P<Expr>>)> {
250-
match expr.node {
251-
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)