Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dead code after #7216 #7234

Merged
merged 1 commit into from
May 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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