-fn check_collapsible_maybe_if_let(cx: &EarlyContext, else_: &ast::Expr) {
- if_let_chain! {[
- let ast::ExprKind::Block(ref block) = else_.node,
- let Some(else_) = expr_block(block),
- !in_macro(else_.span),
- ], {
- match else_.node {
- ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {
- span_lint_and_then(cx,
- COLLAPSIBLE_IF,
- block.span,
- "this `else { if .. }` block can be collapsed", |db| {
- db.span_suggestion(block.span, "try", snippet_block(cx, else_.span, "..").into_owned());
- });
+fn block_starts_with_comment(cx: &EarlyContext<'_>, expr: &ast::Block) -> bool {
+ // We trim all opening braces and whitespaces and then check if the next string is a comment.
+ let trimmed_block_text = snippet_block(cx, expr.span, "..")
+ .trim_start_matches(|c: char| c.is_whitespace() || c == '{')
+ .to_owned();
+ trimmed_block_text.starts_with("//") || trimmed_block_text.starts_with("/*")
+}
+
+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 {
+ match else_.node {
+ ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {
+ let mut applicability = Applicability::MachineApplicable;
+ span_lint_and_sugg(
+ cx,
+ COLLAPSIBLE_IF,
+ block.span,
+ "this `else { if .. }` block can be collapsed",
+ "try",
+ snippet_block_with_applicability(cx, else_.span, "..", &mut applicability).into_owned(),
+ applicability,
+ );
+ }
+ _ => (),