|
1 | 1 | use crate::utils::{differing_macro_contexts, in_macro, snippet_opt, span_note_and_lint};
|
| 2 | +use if_chain::if_chain; |
2 | 3 | use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintPass};
|
3 | 4 | use rustc::{declare_tool_lint, lint_array};
|
4 | 5 | use syntax::ast;
|
@@ -146,44 +147,39 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &ast::Expr) {
|
146 | 147 |
|
147 | 148 | /// Implementation of the `SUSPICIOUS_ELSE_FORMATTING` lint for weird `else`.
|
148 | 149 | fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
|
149 |
| - if let Some((then, &Some(ref else_))) = unsugar_if(expr) { |
150 |
| - if (is_block(else_) || unsugar_if(else_).is_some()) |
151 |
| - && !differing_macro_contexts(then.span, else_.span) |
152 |
| - && !in_macro(then.span) |
153 |
| - && !in_external_macro(cx.sess, expr.span) |
154 |
| - { |
155 |
| - // workaround for rust-lang/rust#43081 |
156 |
| - if expr.span.lo().0 == 0 && expr.span.hi().0 == 0 { |
157 |
| - return; |
158 |
| - } |
| 150 | + if_chain! { |
| 151 | + if let Some((then, &Some(ref else_))) = unsugar_if(expr); |
| 152 | + if is_block(else_) || unsugar_if(else_).is_some(); |
| 153 | + if !differing_macro_contexts(then.span, else_.span); |
| 154 | + if !in_macro(then.span) && !in_external_macro(cx.sess, expr.span); |
159 | 155 |
|
160 |
| - // this will be a span from the closing ‘}’ of the “then” block (excluding) to |
161 |
| - // the |
162 |
| - // “if” of the “else if” block (excluding) |
163 |
| - let else_span = then.span.between(else_.span); |
| 156 | + // workaround for rust-lang/rust#43081 |
| 157 | + if expr.span.lo().0 != 0 && expr.span.hi().0 != 0; |
164 | 158 |
|
165 |
| - // the snippet should look like " else \n " with maybe comments anywhere |
166 |
| - // it’s bad when there is a ‘\n’ after the “else” |
167 |
| - if let Some(else_snippet) = snippet_opt(cx, else_span) { |
168 |
| - let else_pos = else_snippet.find("else").expect("there must be a `else` here"); |
| 159 | + // this will be a span from the closing ‘}’ of the “then” block (excluding) to |
| 160 | + // the “if” of the “else if” block (excluding) |
| 161 | + let else_span = then.span.between(else_.span); |
169 | 162 |
|
170 |
| - if else_snippet[else_pos..].contains('\n') { |
171 |
| - let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" }; |
| 163 | + // the snippet should look like " else \n " with maybe comments anywhere |
| 164 | + // it’s bad when there is a ‘\n’ after the “else” |
| 165 | + if let Some(else_snippet) = snippet_opt(cx, else_span); |
| 166 | + if let Some(else_pos) = else_snippet.find("else"); |
| 167 | + if else_snippet[else_pos..].contains('\n'); |
| 168 | + let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" }; |
172 | 169 |
|
173 |
| - span_note_and_lint( |
174 |
| - cx, |
175 |
| - SUSPICIOUS_ELSE_FORMATTING, |
176 |
| - else_span, |
177 |
| - &format!("this is an `else {}` but the formatting might hide it", else_desc), |
178 |
| - else_span, |
179 |
| - &format!( |
180 |
| - "to remove this lint, remove the `else` or remove the new line between \ |
181 |
| - `else` and `{}`", |
182 |
| - else_desc, |
183 |
| - ), |
184 |
| - ); |
185 |
| - } |
186 |
| - } |
| 170 | + then { |
| 171 | + span_note_and_lint( |
| 172 | + cx, |
| 173 | + SUSPICIOUS_ELSE_FORMATTING, |
| 174 | + else_span, |
| 175 | + &format!("this is an `else {}` but the formatting might hide it", else_desc), |
| 176 | + else_span, |
| 177 | + &format!( |
| 178 | + "to remove this lint, remove the `else` or remove the new line between \ |
| 179 | + `else` and `{}`", |
| 180 | + else_desc, |
| 181 | + ), |
| 182 | + ); |
187 | 183 | }
|
188 | 184 | }
|
189 | 185 | }
|
|
0 commit comments