@@ -3,6 +3,7 @@ use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, Lin
3
3
use rustc:: { declare_tool_lint, lint_array} ;
4
4
use syntax:: ast;
5
5
use syntax:: ptr:: P ;
6
+ use if_chain:: if_chain;
6
7
7
8
declare_clippy_lint ! {
8
9
/// **What it does:** Checks for use of the non-existent `=*`, `=!` and `=-`
@@ -146,44 +147,40 @@ 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
- if let Some ( else_pos) = else_snippet. find ( "else" ) {
169
- if else_snippet[ else_pos..] . contains ( '\n' ) {
170
- let else_desc = if unsugar_if ( else_) . is_some ( ) { "if" } else { "{..}" } ;
159
+ // this will be a span from the closing ‘}’ of the “then” block (excluding) to
160
+ // the
161
+ // “if” of the “else if” block (excluding)
162
+ let else_span = then. span. between( else_. span) ;
171
163
172
- span_note_and_lint (
173
- cx,
174
- SUSPICIOUS_ELSE_FORMATTING ,
175
- else_span,
176
- & format ! ( "this is an `else {}` but the formatting might hide it" , else_desc) ,
177
- else_span,
178
- & format ! (
179
- "to remove this lint, remove the `else` or remove the new line between \
180
- `else` and `{}`",
181
- else_desc,
182
- ) ,
183
- ) ;
184
- }
185
- }
186
- }
164
+ // the snippet should look like " else \n " with maybe comments anywhere
165
+ // it’s bad when there is a ‘\n’ after the “else”
166
+ if let Some ( else_snippet) = snippet_opt( cx, else_span) ;
167
+ if let Some ( else_pos) = else_snippet. find( "else" ) ;
168
+ if else_snippet[ else_pos..] . contains( '\n' ) ;
169
+ let else_desc = if unsugar_if( else_) . is_some( ) { "if" } else { "{..}" } ;
170
+
171
+ then {
172
+ span_note_and_lint(
173
+ cx,
174
+ SUSPICIOUS_ELSE_FORMATTING ,
175
+ else_span,
176
+ & format!( "this is an `else {}` but the formatting might hide it" , else_desc) ,
177
+ else_span,
178
+ & format!(
179
+ "to remove this lint, remove the `else` or remove the new line between \
180
+ `else` and `{}`",
181
+ else_desc,
182
+ ) ,
183
+ ) ;
187
184
}
188
185
}
189
186
}
0 commit comments