Skip to content

Commit

Permalink
get rid of require_semi function
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed Mar 9, 2022
1 parent d547843 commit 10f0b21
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use clippy_utils::higher;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{
eq_expr_value, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
eq_expr_value, get_parent_node, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id, peel_blocks,
peel_blocks_with_stmt,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr, ResultOk};
use rustc_hir::{BindingAnnotation, Expr, ExprKind, PatKind, PathSegment, QPath};
use rustc_hir::{BindingAnnotation, Expr, ExprKind, Node, PatKind, PathSegment, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::Ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -122,11 +123,12 @@ fn check_if_let_some_or_err_and_early_return(cx: &LateContext<'_>, expr: &Expr<'
let mut applicability = Applicability::MachineApplicable;
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
let by_ref = matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut);
let requires_semi = matches!(get_parent_node(cx.tcx, expr.hir_id), Some(Node::Stmt(_)));
let replacement = format!(
"{}{}?{}",
receiver_str,
if by_ref { ".as_ref()" } else { "" },
if requires_semi(if_then, if_else) { ";" } else { "" }
if requires_semi { ";" } else { "" }
);
offer_suggestion(cx, expr, replacement, applicability);
}
Expand Down Expand Up @@ -202,18 +204,6 @@ fn expr_return_none_or_err(
}
}

/// Check whether the fixed code needs semicolon after `?`
///
/// It will need a semicolon if all block(s) has one.
fn requires_semi(if_then: &Expr<'_>, if_else: Option<&Expr<'_>>) -> bool {
let if_then_kind = &peel_blocks_with_stmt(if_then).kind;
let if_then_is_ret_stmt = matches!(if_then_kind, ExprKind::Ret(..));
if if_else.is_none() || matches!(peel_blocks_with_stmt(if_else.unwrap()).kind, ExprKind::Ret(_)) {
return if_then_is_ret_stmt;
}
false
}

fn offer_suggestion(cx: &LateContext<'_>, expr: &Expr<'_>, suggestion: String, applicability: Applicability) {
if !suggestion.is_empty() {
span_lint_and_sugg(
Expand Down

0 comments on commit 10f0b21

Please sign in to comment.