Skip to content

Commit

Permalink
Check for comments in collapsible ifs
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasstevens committed Oct 16, 2018
1 parent dae7abb commit b153000
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions clippy_lints/src/collapsible_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,20 @@ fn check_if(cx: &EarlyContext<'_>, expr: &ast::Expr) {
}
}

fn block_starts_with_comment(cx: &EarlyContext<'_>, expr: &ast::Block) -> bool {
// The zeroth string (ignoring whitespaces) is "{", which marks the beginning of the block.
// Therefore, we check the if the first string after that is a comment.
if let Some(first_string) = snippet_block(cx, expr.span, "..").split_whitespace().nth(1) {
first_string.starts_with("//")
} else {
false
}
}

fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
if_chain! {
if let ast::ExprKind::Block(ref block, _) = else_.node;
if !block_starts_with_comment(cx, block);
if let Some(else_) = expr_block(block);
if !in_macro(else_.span);
then {
Expand All @@ -135,6 +146,7 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {

fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: &ast::Expr, then: &ast::Block) {
if_chain! {
if !block_starts_with_comment(cx, then);
if let Some(inner) = expr_block(then);
if let ast::ExprKind::If(ref check_inner, ref content, None) = inner.node;
then {
Expand Down

0 comments on commit b153000

Please sign in to comment.