-fn check_to_string(cx: &EarlyContext, e: &ast::Expr) -> Cow<'static, str> {
- if requires_brackets(e) {
- format!("({})", snippet(cx, e.span, "..")).into()
- } else {
- snippet(cx, e.span, "..")
+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 {
+ if expr.span.ctxt() != inner.span.ctxt() {
+ return;
+ }
+ span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this if statement can be collapsed", |db| {
+ let lhs = Sugg::ast(cx, check, "..");
+ let rhs = Sugg::ast(cx, check_inner, "..");
+ db.span_suggestion_with_applicability(
+ expr.span,
+ "try",
+ format!(
+ "if {} {}",
+ lhs.and(&rhs),
+ snippet_block(cx, content.span, ".."),
+ ),
+ Applicability::MachineApplicable, // snippet
+ );
+ });
+ }