Skip to content

Commit 10f0b21

Browse files
committed
get rid of require_semi function
1 parent d547843 commit 10f0b21

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ use clippy_utils::higher;
33
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use clippy_utils::{
6-
eq_expr_value, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
6+
eq_expr_value, get_parent_node, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id, peel_blocks,
7+
peel_blocks_with_stmt,
78
};
89
use if_chain::if_chain;
910
use rustc_errors::Applicability;
1011
use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr, ResultOk};
11-
use rustc_hir::{BindingAnnotation, Expr, ExprKind, PatKind, PathSegment, QPath};
12+
use rustc_hir::{BindingAnnotation, Expr, ExprKind, Node, PatKind, PathSegment, QPath};
1213
use rustc_lint::{LateContext, LateLintPass};
1314
use rustc_middle::ty::Ty;
1415
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -122,11 +123,12 @@ fn check_if_let_some_or_err_and_early_return(cx: &LateContext<'_>, expr: &Expr<'
122123
let mut applicability = Applicability::MachineApplicable;
123124
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
124125
let by_ref = matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut);
126+
let requires_semi = matches!(get_parent_node(cx.tcx, expr.hir_id), Some(Node::Stmt(_)));
125127
let replacement = format!(
126128
"{}{}?{}",
127129
receiver_str,
128130
if by_ref { ".as_ref()" } else { "" },
129-
if requires_semi(if_then, if_else) { ";" } else { "" }
131+
if requires_semi { ";" } else { "" }
130132
);
131133
offer_suggestion(cx, expr, replacement, applicability);
132134
}
@@ -202,18 +204,6 @@ fn expr_return_none_or_err(
202204
}
203205
}
204206

205-
/// Check whether the fixed code needs semicolon after `?`
206-
///
207-
/// It will need a semicolon if all block(s) has one.
208-
fn requires_semi(if_then: &Expr<'_>, if_else: Option<&Expr<'_>>) -> bool {
209-
let if_then_kind = &peel_blocks_with_stmt(if_then).kind;
210-
let if_then_is_ret_stmt = matches!(if_then_kind, ExprKind::Ret(..));
211-
if if_else.is_none() || matches!(peel_blocks_with_stmt(if_else.unwrap()).kind, ExprKind::Ret(_)) {
212-
return if_then_is_ret_stmt;
213-
}
214-
false
215-
}
216-
217207
fn offer_suggestion(cx: &LateContext<'_>, expr: &Expr<'_>, suggestion: String, applicability: Applicability) {
218208
if !suggestion.is_empty() {
219209
span_lint_and_sugg(

0 commit comments

Comments
 (0)