Skip to content

Commit

Permalink
Auto merge of rust-lang#7234 - camsteffen:if-let-else-braces, r=flip1995
Browse files Browse the repository at this point in the history
Remove dead code after rust-lang#7216

changelog: none
  • Loading branch information
bors committed May 17, 2021
2 parents a3223af + ca0f000 commit 6fcdb8a
Showing 1 changed file with 3 additions and 42 deletions.
45 changes: 3 additions & 42 deletions clippy_lints/src/option_if_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::usage::contains_return_break_continue_macro;
use clippy_utils::{eager_or_lazy, get_enclosing_block, in_macro, is_else_clause, is_lang_ctor};
use clippy_utils::{eager_or_lazy, in_macro, is_else_clause, is_lang_ctor};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::LangItem::OptionSome;
Expand Down Expand Up @@ -81,7 +81,6 @@ struct OptionIfLetElseOccurence {
method_sugg: String,
some_expr: String,
none_expr: String,
wrap_braces: bool,
}

/// Extracts the body of a given arm. If the arm contains only an expression,
Expand All @@ -106,37 +105,6 @@ fn extract_body_from_arm<'a>(arm: &'a Arm<'a>) -> Option<&'a Expr<'a>> {
}
}

/// If this is the else body of an if/else expression, then we need to wrap
/// it in curly braces. Otherwise, we don't.
fn should_wrap_in_braces(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
get_enclosing_block(cx, expr.hir_id).map_or(false, |parent| {
let mut should_wrap = false;

if let Some(Expr {
kind:
ExprKind::Match(
_,
arms,
MatchSource::IfLetDesugar {
contains_else_clause: true,
},
),
..
}) = parent.expr
{
should_wrap = expr.hir_id == arms[1].body.hir_id;
} else if let Some(Expr {
kind: ExprKind::If(_, _, Some(else_clause)),
..
}) = parent.expr
{
should_wrap = expr.hir_id == else_clause.hir_id;
}

should_wrap
})
}

fn format_option_in_sugg(cx: &LateContext<'_>, cond_expr: &Expr<'_>, as_ref: bool, as_mut: bool) -> String {
format!(
"{}{}",
Expand Down Expand Up @@ -176,7 +144,6 @@ fn detect_option_if_let_else<'tcx>(
let none_body = extract_body_from_arm(&arms[1])?;
let method_sugg = if eager_or_lazy::is_eagerness_candidate(cx, none_body) { "map_or" } else { "map_or_else" };
let capture_name = id.name.to_ident_string();
let wrap_braces = should_wrap_in_braces(cx, expr);
let (as_ref, as_mut) = match &cond_expr.kind {
ExprKind::AddrOf(_, Mutability::Not, _) => (true, false),
ExprKind::AddrOf(_, Mutability::Mut, _) => (false, true),
Expand All @@ -192,7 +159,6 @@ fn detect_option_if_let_else<'tcx>(
method_sugg: method_sugg.to_string(),
some_expr: format!("|{}{}| {}", capture_mut, capture_name, Sugg::hir(cx, some_body, "..")),
none_expr: format!("{}{}", if method_sugg == "map_or" { "" } else { "|| " }, Sugg::hir(cx, none_body, "..")),
wrap_braces,
})
} else {
None
Expand All @@ -210,13 +176,8 @@ impl<'tcx> LateLintPass<'tcx> for OptionIfLetElse {
format!("use Option::{} instead of an if let/else", detection.method_sugg).as_str(),
"try",
format!(
"{}{}.{}({}, {}){}",
if detection.wrap_braces { "{ " } else { "" },
detection.option,
detection.method_sugg,
detection.none_expr,
detection.some_expr,
if detection.wrap_braces { " }" } else { "" },
"{}.{}({}, {})",
detection.option, detection.method_sugg, detection.none_expr, detection.some_expr,
),
Applicability::MaybeIncorrect,
);
Expand Down

0 comments on commit 6fcdb8a

Please sign in to comment.