Skip to content

Commit 284b63a

Browse files
committed
Rename remove_blocks to peel_blocks
1 parent 9e08527 commit 284b63a

File tree

9 files changed

+38
-34
lines changed

9 files changed

+38
-34
lines changed

clippy_lints/src/bytecount.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::ty::match_type;
44
use clippy_utils::visitors::is_local_used;
5-
use clippy_utils::{path_to_local_id, paths, peel_ref_operators, remove_blocks, strip_pat_refs};
5+
use clippy_utils::{path_to_local_id, paths, peel_blocks, peel_ref_operators, strip_pat_refs};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{BinOpKind, Expr, ExprKind, PatKind};
@@ -55,7 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for ByteCount {
5555
cx.typeck_results().expr_ty(filter_recv).peel_refs(),
5656
&paths::SLICE_ITER);
5757
let operand_is_arg = |expr| {
58-
let expr = peel_ref_operators(cx, remove_blocks(expr));
58+
let expr = peel_ref_operators(cx, peel_blocks(expr));
5959
path_to_local_id(expr, arg_id)
6060
};
6161
let needle = if operand_is_arg(l) {

clippy_lints/src/derivable_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::{is_automatically_derived, is_default_equivalent, remove_blocks};
2+
use clippy_utils::{is_automatically_derived, is_default_equivalent, peel_blocks};
33
use rustc_hir::{
44
def::{DefKind, Res},
55
Body, Expr, ExprKind, GenericArg, Impl, ImplItemKind, Item, ItemKind, Node, PathSegment, QPath, TyKind,
@@ -95,7 +95,7 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
9595
}
9696
}
9797
}
98-
let should_emit = match remove_blocks(func_expr).kind {
98+
let should_emit = match peel_blocks(func_expr).kind {
9999
ExprKind::Tup(fields) => fields.iter().all(|e| is_default_equivalent(cx, e)),
100100
ExprKind::Call(callee, args)
101101
if is_path_self(callee) => args.iter().all(|e| is_default_equivalent(cx, e)),

clippy_lints/src/map_clone.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::is_trait_method;
3-
use clippy_utils::remove_blocks;
42
use clippy_utils::source::snippet_with_applicability;
53
use clippy_utils::ty::{is_copy, is_type_diagnostic_item};
4+
use clippy_utils::{is_trait_method, peel_blocks};
65
use if_chain::if_chain;
76
use rustc_errors::Applicability;
87
use rustc_hir as hir;
@@ -60,7 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
6059
if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].kind;
6160
then {
6261
let closure_body = cx.tcx.hir().body(body_id);
63-
let closure_expr = remove_blocks(&closure_body.value);
62+
let closure_expr = peel_blocks(&closure_body.value);
6463
match closure_body.params[0].pat.kind {
6564
hir::PatKind::Ref(inner, hir::Mutability::Not) => if let hir::PatKind::Binding(
6665
hir::BindingAnnotation::Unannotated, .., name, None

clippy_lints/src/matches.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, match_type, pe
99
use clippy_utils::visitors::is_local_used;
1010
use clippy_utils::{
1111
get_parent_expr, is_expn_of, is_lang_ctor, is_lint_allowed, is_refutable, is_unit_expr, is_wild, meets_msrv, msrvs,
12-
path_to_local, path_to_local_id, peel_hir_pat_refs, peel_n_hir_expr_refs, recurse_or_patterns, remove_blocks,
12+
path_to_local, path_to_local_id, peel_blocks, peel_hir_pat_refs, peel_n_hir_expr_refs, recurse_or_patterns,
1313
strip_pat_refs,
1414
};
1515
use clippy_utils::{paths, search_same, SpanlessEq, SpanlessHash};
@@ -659,7 +659,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
659659
QPath::Resolved(None, variant_name), args, _) = arms[0].pat.kind;
660660
if args.len() == 1;
661661
if let PatKind::Binding(_, arg, ..) = strip_pat_refs(&args[0]).kind;
662-
let body = remove_blocks(arms[0].body);
662+
let body = peel_blocks(arms[0].body);
663663
if path_to_local_id(body, arg);
664664

665665
then {
@@ -724,7 +724,7 @@ fn check_single_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], exp
724724
return;
725725
}
726726
let els = arms[1].body;
727-
let els = if is_unit_expr(remove_blocks(els)) {
727+
let els = if is_unit_expr(peel_blocks(els)) {
728728
None
729729
} else if let ExprKind::Block(Block { stmts, expr: block_expr, .. }, _) = els.kind {
730730
if stmts.len() == 1 && block_expr.is_none() || stmts.is_empty() && block_expr.is_some() {
@@ -1482,7 +1482,7 @@ fn check_match_single_binding<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[A
14821482

14831483
let matched_vars = ex.span;
14841484
let bind_names = arms[0].pat.span;
1485-
let match_body = remove_blocks(arms[0].body);
1485+
let match_body = peel_blocks(arms[0].body);
14861486
let mut snippet_body = if match_body.span.from_expansion() {
14871487
Sugg::hir_with_macro_callsite(cx, match_body, "..").to_string()
14881488
} else {
@@ -1679,7 +1679,7 @@ fn is_ref_some_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> Option<BindingAnnotat
16791679
if is_lang_ctor(cx, qpath, OptionSome);
16801680
if let PatKind::Binding(rb, .., ident, _) = first_pat.kind;
16811681
if rb == BindingAnnotation::Ref || rb == BindingAnnotation::RefMut;
1682-
if let ExprKind::Call(e, args) = remove_blocks(arm.body).kind;
1682+
if let ExprKind::Call(e, args) = peel_blocks(arm.body).kind;
16831683
if let ExprKind::Path(ref some_path) = e.kind;
16841684
if is_lang_ctor(cx, some_path, OptionSome) && args.len() == 1;
16851685
if let ExprKind::Path(QPath::Resolved(_, path2)) = args[0].kind;

clippy_lints/src/methods/bind_instead_of_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{contains_return, BIND_INSTEAD_OF_MAP};
22
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
4-
use clippy_utils::{remove_blocks, visitors::find_all_ret_expressions};
4+
use clippy_utils::{peel_blocks, visitors::find_all_ret_expressions};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
@@ -152,7 +152,7 @@ pub(crate) trait BindInsteadOfMap {
152152
match arg.kind {
153153
hir::ExprKind::Closure(_, _, body_id, closure_args_span, _) => {
154154
let closure_body = cx.tcx.hir().body(body_id);
155-
let closure_expr = remove_blocks(&closure_body.value);
155+
let closure_expr = peel_blocks(&closure_body.value);
156156

157157
if Self::lint_closure_autofixable(cx, expr, recv, closure_expr, closure_args_span) {
158158
true

clippy_lints/src/methods/filter_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::{indent_of, reindent_multiline, snippet};
33
use clippy_utils::ty::is_type_diagnostic_item;
4-
use clippy_utils::{is_trait_method, path_to_local_id, remove_blocks, SpanlessEq};
4+
use clippy_utils::{is_trait_method, path_to_local_id, peel_blocks, SpanlessEq};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
@@ -25,7 +25,7 @@ fn is_method<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, method_name: Sy
2525
},
2626
hir::ExprKind::Closure(_, _, c, _, _) => {
2727
let body = cx.tcx.hir().body(*c);
28-
let closure_expr = remove_blocks(&body.value);
28+
let closure_expr = peel_blocks(&body.value);
2929
let arg_id = body.params[0].pat.hir_id;
3030
match closure_expr.kind {
3131
hir::ExprKind::MethodCall(hir::PathSegment { ident, .. }, _, args, _) => {

clippy_lints/src/methods/option_as_ref_deref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet;
33
use clippy_utils::ty::is_type_diagnostic_item;
4-
use clippy_utils::{match_def_path, meets_msrv, msrvs, path_to_local_id, paths, remove_blocks};
4+
use clippy_utils::{match_def_path, meets_msrv, msrvs, path_to_local_id, paths, peel_blocks};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
@@ -53,7 +53,7 @@ pub(super) fn check<'tcx>(
5353
}),
5454
hir::ExprKind::Closure(_, _, body_id, _, _) => {
5555
let closure_body = cx.tcx.hir().body(body_id);
56-
let closure_expr = remove_blocks(&closure_body.value);
56+
let closure_expr = peel_blocks(&closure_body.value);
5757

5858
match &closure_expr.kind {
5959
hir::ExprKind::MethodCall(_, _, args, _) => {

clippy_lints/src/methods/unnecessary_fold.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
3-
use clippy_utils::{is_trait_method, path_to_local_id, remove_blocks, strip_pat_refs};
3+
use clippy_utils::{is_trait_method, path_to_local_id, peel_blocks, strip_pat_refs};
44
use if_chain::if_chain;
55
use rustc_ast::ast;
66
use rustc_errors::Applicability;
@@ -31,7 +31,7 @@ pub(super) fn check(
3131
// Extract the body of the closure passed to fold
3232
if let hir::ExprKind::Closure(_, _, body_id, _, _) = acc.kind;
3333
let closure_body = cx.tcx.hir().body(body_id);
34-
let closure_expr = remove_blocks(&closure_body.value);
34+
let closure_expr = peel_blocks(&closure_body.value);
3535

3636
// Check if the closure body is of the form `acc <op> some_expr(x)`
3737
if let hir::ExprKind::Binary(ref bin_op, left_expr, right_expr) = closure_expr.kind;

clippy_utils/src/lib.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,25 @@ pub fn get_parent_as_impl(tcx: TyCtxt<'_>, id: HirId) -> Option<&Impl<'_>> {
12241224
}
12251225
}
12261226

1227+
/// Removes blocks around an expression, only if the block contains just one expression
1228+
/// and no statements.
1229+
///
1230+
/// Examples:
1231+
/// * `{}` -> `{}`
1232+
/// * `{ x }` -> `x`
1233+
/// * `{{ x }}` -> `x`
1234+
/// * `{ x; }` -> `{ x; }`
1235+
/// * `{ x; y }` -> `{ x; y }`
1236+
pub fn peel_blocks<'tcx>(mut expr: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
1237+
while let ExprKind::Block(block, ..) = expr.kind {
1238+
match (block.stmts.is_empty(), block.expr.as_ref()) {
1239+
(true, Some(e)) => expr = e,
1240+
_ => break,
1241+
}
1242+
}
1243+
expr
1244+
}
1245+
12271246
/// Checks if the given expression is the else clause of either an `if` or `if let` expression.
12281247
pub fn is_else_clause(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
12291248
let mut iter = tcx.hir().parent_iter(expr.hir_id);
@@ -1405,20 +1424,6 @@ pub fn is_automatically_derived(attrs: &[ast::Attribute]) -> bool {
14051424
has_attr(attrs, sym::automatically_derived)
14061425
}
14071426

1408-
/// Remove blocks around an expression.
1409-
///
1410-
/// Ie. `x`, `{ x }` and `{{{{ x }}}}` all give `x`. `{ x; y }` and `{}` return
1411-
/// themselves.
1412-
pub fn remove_blocks<'tcx>(mut expr: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
1413-
while let ExprKind::Block(block, ..) = expr.kind {
1414-
match (block.stmts.is_empty(), block.expr.as_ref()) {
1415-
(true, Some(e)) => expr = e,
1416-
_ => break,
1417-
}
1418-
}
1419-
expr
1420-
}
1421-
14221427
pub fn is_self(slf: &Param<'_>) -> bool {
14231428
if let PatKind::Binding(.., name, _) = slf.pat.kind {
14241429
name.name == kw::SelfLower

0 commit comments

Comments
 (0)