Skip to content

Commit 1ea827f

Browse files
committed
Auto merge of rust-lang#13115 - tesuji:rm-dup-peels, r=dswij
Remove duplicated `peel_middle_ty_refs` TODO: Should we move `ty::peel_mid_ty_refs_is_mutable` to super module too? changelog: none
2 parents 1aa686b + a5fd2c9 commit 1ea827f

File tree

7 files changed

+22
-35
lines changed

7 files changed

+22
-35
lines changed

clippy_lints/src/dereference.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, span_lint_hir_and_then};
22
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
33
use clippy_utils::sugg::has_enclosing_paren;
4-
use clippy_utils::ty::{implements_trait, is_manually_drop, peel_mid_ty_refs};
4+
use clippy_utils::ty::{implements_trait, is_manually_drop};
55
use clippy_utils::{
66
expr_use_ctxt, get_parent_expr, is_block_like, is_lint_allowed, path_to_local, peel_middle_ty_refs, DefinedTy,
77
ExprUseNode,
@@ -947,7 +947,7 @@ fn report<'tcx>(
947947
let (expr_str, _expr_is_macro_call) =
948948
snippet_with_context(cx, expr.span, data.first_expr.span.ctxt(), "..", &mut app);
949949
let ty = typeck.expr_ty(expr);
950-
let (_, ref_count) = peel_mid_ty_refs(ty);
950+
let (_, ref_count) = peel_middle_ty_refs(ty);
951951
let deref_str = if ty_changed_count >= ref_count && ref_count != 0 {
952952
// a deref call changing &T -> &U requires two deref operators the first time
953953
// this occurs. One to remove the reference, a second to call the deref impl.

clippy_lints/src/matches/single_match.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::{expr_block, snippet, SpanRangeExt};
3-
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, peel_mid_ty_refs};
4-
use clippy_utils::{is_lint_allowed, is_unit_expr, is_wild, peel_blocks, peel_hir_pat_refs, peel_n_hir_expr_refs};
3+
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
4+
use clippy_utils::{
5+
is_lint_allowed, is_unit_expr, is_wild, peel_blocks, peel_hir_pat_refs, peel_middle_ty_refs, peel_n_hir_expr_refs,
6+
};
57
use core::cmp::max;
68
use rustc_errors::Applicability;
79
use rustc_hir::{Arm, BindingMode, Block, Expr, ExprKind, Pat, PatKind};
@@ -82,7 +84,7 @@ fn report_single_pattern(
8284

8385
let (pat, pat_ref_count) = peel_hir_pat_refs(arms[0].pat);
8486
let (msg, sugg) = if let PatKind::Path(_) | PatKind::Lit(_) = pat.kind
85-
&& let (ty, ty_ref_count) = peel_mid_ty_refs(cx.typeck_results().expr_ty(ex))
87+
&& let (ty, ty_ref_count) = peel_middle_ty_refs(cx.typeck_results().expr_ty(ex))
8688
&& let Some(spe_trait_id) = cx.tcx.lang_items().structural_peq_trait()
8789
&& let Some(pe_trait_id) = cx.tcx.lang_items().eq_trait()
8890
&& (ty.is_integral()

clippy_lints/src/methods/implicit_clone.rs

Lines changed: 3 additions & 3 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_with_context;
3-
use clippy_utils::ty::{implements_trait, peel_mid_ty_refs};
4-
use clippy_utils::{is_diag_item_method, is_diag_trait_item};
3+
use clippy_utils::ty::implements_trait;
4+
use clippy_utils::{is_diag_item_method, is_diag_trait_item, peel_middle_ty_refs};
55
use rustc_errors::Applicability;
66
use rustc_hir as hir;
77
use rustc_lint::LateContext;
@@ -14,7 +14,7 @@ pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv
1414
&& is_clone_like(cx, method_name, method_def_id)
1515
&& let return_type = cx.typeck_results().expr_ty(expr)
1616
&& let input_type = cx.typeck_results().expr_ty(recv)
17-
&& let (input_type, ref_count) = peel_mid_ty_refs(input_type)
17+
&& let (input_type, ref_count) = peel_middle_ty_refs(input_type)
1818
&& !(ref_count > 0 && is_diag_trait_item(cx, method_def_id, sym::ToOwned))
1919
&& let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did()))
2020
&& return_type == input_type

clippy_lints/src/methods/unnecessary_to_owned.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ use super::unnecessary_iter_cloned::{self, is_into_iter};
33
use clippy_config::msrvs::{self, Msrv};
44
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
55
use clippy_utils::source::{snippet, snippet_opt};
6-
use clippy_utils::ty::{
7-
get_iterator_item_ty, implements_trait, is_copy, is_type_diagnostic_item, is_type_lang_item, peel_mid_ty_refs,
8-
};
6+
use clippy_utils::ty::{get_iterator_item_ty, implements_trait, is_copy, is_type_diagnostic_item, is_type_lang_item};
97
use clippy_utils::visitors::find_all_ret_expressions;
108
use clippy_utils::{
11-
fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, match_def_path, paths, return_ty,
9+
fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, match_def_path, paths, peel_middle_ty_refs,
10+
return_ty,
1211
};
1312
use rustc_errors::Applicability;
1413
use rustc_hir::def::{DefKind, Res};
@@ -120,8 +119,8 @@ fn check_addr_of_expr(
120119
},
121120
] = adjustments[..]
122121
&& let receiver_ty = cx.typeck_results().expr_ty(receiver)
123-
&& let (target_ty, n_target_refs) = peel_mid_ty_refs(*target_ty)
124-
&& let (receiver_ty, n_receiver_refs) = peel_mid_ty_refs(receiver_ty)
122+
&& let (target_ty, n_target_refs) = peel_middle_ty_refs(*target_ty)
123+
&& let (receiver_ty, n_receiver_refs) = peel_middle_ty_refs(receiver_ty)
125124
// Only flag cases satisfying at least one of the following three conditions:
126125
// * the referent and receiver types are distinct
127126
// * the referent/receiver type is a copyable array
@@ -382,7 +381,7 @@ fn check_other_call_arg<'tcx>(
382381
&& let fn_sig = cx.tcx.fn_sig(callee_def_id).instantiate_identity().skip_binder()
383382
&& let Some(i) = recv.into_iter().chain(call_args).position(|arg| arg.hir_id == maybe_arg.hir_id)
384383
&& let Some(input) = fn_sig.inputs().get(i)
385-
&& let (input, n_refs) = peel_mid_ty_refs(*input)
384+
&& let (input, n_refs) = peel_middle_ty_refs(*input)
386385
&& let (trait_predicates, _) = get_input_traits_and_projections(cx, callee_def_id, input)
387386
&& let Some(sized_def_id) = cx.tcx.lang_items().sized_trait()
388387
&& let [trait_predicate] = trait_predicates

clippy_lints/src/redundant_slicing.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::get_parent_expr;
32
use clippy_utils::source::snippet_with_context;
4-
use clippy_utils::ty::{is_type_lang_item, peel_mid_ty_refs};
3+
use clippy_utils::ty::is_type_lang_item;
4+
use clippy_utils::{get_parent_expr, peel_middle_ty_refs};
55
use rustc_ast::util::parser::PREC_PREFIX;
66
use rustc_errors::Applicability;
77
use rustc_hir::{BorrowKind, Expr, ExprKind, LangItem, Mutability};
@@ -82,8 +82,8 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
8282
&& let ExprKind::Index(indexed, range, _) = addressee.kind
8383
&& is_type_lang_item(cx, cx.typeck_results().expr_ty_adjusted(range), LangItem::RangeFull)
8484
{
85-
let (expr_ty, expr_ref_count) = peel_mid_ty_refs(cx.typeck_results().expr_ty(expr));
86-
let (indexed_ty, indexed_ref_count) = peel_mid_ty_refs(cx.typeck_results().expr_ty(indexed));
85+
let (expr_ty, expr_ref_count) = peel_middle_ty_refs(cx.typeck_results().expr_ty(expr));
86+
let (indexed_ty, indexed_ref_count) = peel_middle_ty_refs(cx.typeck_results().expr_ty(indexed));
8787
let parent_expr = get_parent_expr(cx, expr);
8888
let needs_parens_for_prefix = parent_expr.map_or(false, |parent| parent.precedence().order() > PREC_PREFIX);
8989
let mut app = Applicability::MachineApplicable;

clippy_lints/src/size_of_ref.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::path_def_id;
3-
use clippy_utils::ty::peel_mid_ty_refs;
2+
use clippy_utils::{path_def_id, peel_middle_ty_refs};
43
use rustc_hir::{Expr, ExprKind};
54
use rustc_lint::{LateContext, LateLintPass};
65
use rustc_session::declare_lint_pass;
@@ -60,7 +59,7 @@ impl LateLintPass<'_> for SizeOfRef {
6059
&& let Some(def_id) = path_def_id(cx, path)
6160
&& cx.tcx.is_diagnostic_item(sym::mem_size_of_val, def_id)
6261
&& let arg_ty = cx.typeck_results().expr_ty(arg)
63-
&& peel_mid_ty_refs(arg_ty).1 > 1
62+
&& peel_middle_ty_refs(arg_ty).1 > 1
6463
{
6564
span_lint_and_help(
6665
cx,

clippy_utils/src/ty.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -525,19 +525,6 @@ pub fn needs_ordered_drop<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
525525
needs_ordered_drop_inner(cx, ty, &mut FxHashSet::default())
526526
}
527527

528-
/// Peels off all references on the type. Returns the underlying type and the number of references
529-
/// removed.
530-
pub fn peel_mid_ty_refs(ty: Ty<'_>) -> (Ty<'_>, usize) {
531-
fn peel(ty: Ty<'_>, count: usize) -> (Ty<'_>, usize) {
532-
if let ty::Ref(_, ty, _) = ty.kind() {
533-
peel(*ty, count + 1)
534-
} else {
535-
(ty, count)
536-
}
537-
}
538-
peel(ty, 0)
539-
}
540-
541528
/// Peels off all references on the type. Returns the underlying type, the number of references
542529
/// removed, and whether the pointer is ultimately mutable or not.
543530
pub fn peel_mid_ty_refs_is_mutable(ty: Ty<'_>) -> (Ty<'_>, usize, Mutability) {

0 commit comments

Comments
 (0)