Skip to content

Commit

Permalink
Auto merge of #8729 - Serial-ATA:issue-7318, r=Manishearth
Browse files Browse the repository at this point in the history
Fix missing whitespace in `collapsible_else_if` suggestion

changelog: Fix missing whitespace in [`collapsible_else_if`] suggestion
closes #7318
  • Loading branch information
bors committed Apr 21, 2022
2 parents a9d31e7 + 14667d1 commit cf68cad
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
18 changes: 14 additions & 4 deletions clippy_lints/src/collapsible_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
//! This lint is **warn** by default

use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::source::{snippet_block, snippet_block_with_applicability};
use clippy_utils::source::{snippet, snippet_block, snippet_block_with_applicability};
use clippy_utils::sugg::Sugg;
use if_chain::if_chain;
use rustc_ast::ast;
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::Span;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -102,7 +103,7 @@ impl EarlyLintPass for CollapsibleIf {
fn check_if(cx: &EarlyContext<'_>, expr: &ast::Expr) {
if let ast::ExprKind::If(check, then, else_) = &expr.kind {
if let Some(else_) = else_ {
check_collapsible_maybe_if_let(cx, else_);
check_collapsible_maybe_if_let(cx, then.span, else_);
} else if let ast::ExprKind::Let(..) = check.kind {
// Prevent triggering on `if let a = b { if c { .. } }`.
} else {
Expand All @@ -119,7 +120,7 @@ fn block_starts_with_comment(cx: &EarlyContext<'_>, expr: &ast::Block) -> bool {
trimmed_block_text.starts_with("//") || trimmed_block_text.starts_with("/*")
}

fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, then_span: Span, else_: &ast::Expr) {
if_chain! {
if let ast::ExprKind::Block(ref block, _) = else_.kind;
if !block_starts_with_comment(cx, block);
Expand All @@ -128,14 +129,23 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
if !else_.span.from_expansion();
if let ast::ExprKind::If(..) = else_.kind;
then {
// Prevent "elseif"
// Check that the "else" is followed by whitespace
let up_to_else = then_span.between(block.span);
let requires_space = if let Some(c) = snippet(cx, up_to_else, "..").chars().last() { !c.is_whitespace() } else { false };

let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
COLLAPSIBLE_ELSE_IF,
block.span,
"this `else { if .. }` block can be collapsed",
"collapse nested if block",
snippet_block_with_applicability(cx, else_.span, "..", Some(block.span), &mut applicability).into_owned(),
format!(
"{}{}",
if requires_space { " " } else { "" },
snippet_block_with_applicability(cx, else_.span, "..", Some(block.span), &mut applicability)
),
applicability,
);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/collapsible_else_if.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,10 @@ fn main() {
}
}
}

#[rustfmt::skip]
#[allow(dead_code)]
fn issue_7318() {
if true { println!("I've been resolved!")
}else if false {}
}
9 changes: 9 additions & 0 deletions tests/ui/collapsible_else_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,12 @@ fn main() {
}
}
}

#[rustfmt::skip]
#[allow(dead_code)]
fn issue_7318() {
if true { println!("I've been resolved!")
}else{
if false {}
}
}
11 changes: 10 additions & 1 deletion tests/ui/collapsible_else_if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,14 @@ LL + println!("!")
LL + }
|

error: aborting due to 7 previous errors
error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_else_if.rs:97:10
|
LL | }else{
| __________^
LL | | if false {}
LL | | }
| |_____^ help: collapse nested if block: `if false {}`

error: aborting due to 8 previous errors

0 comments on commit cf68cad

Please sign in to comment.