Skip to content

Commit 897b446

Browse files
committed
Move some functions into clippy_utils::ty
1 parent 0bf39bf commit 897b446

File tree

4 files changed

+312
-283
lines changed

4 files changed

+312
-283
lines changed

clippy_lints/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ extern crate rustc_hir;
4242
extern crate rustc_hir_analysis;
4343
extern crate rustc_hir_pretty;
4444
extern crate rustc_hir_typeck;
45-
extern crate rustc_index;
4645
extern crate rustc_infer;
4746
extern crate rustc_lexer;
4847
extern crate rustc_lint;

clippy_lints/src/methods/unnecessary_to_owned.rs

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use super::unnecessary_iter_cloned::{self, is_into_iter};
33
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
44
use clippy_utils::msrvs::{self, Msrv};
55
use clippy_utils::source::{SpanRangeExt, snippet};
6-
use clippy_utils::ty::{get_iterator_item_ty, implements_trait, is_copy, is_type_diagnostic_item, is_type_lang_item};
6+
use clippy_utils::ty::{
7+
get_callee_generic_args_and_args, get_iterator_item_ty, implements_trait, is_copy, is_to_string_on_string_like,
8+
is_type_diagnostic_item, is_type_lang_item,
9+
};
710
use clippy_utils::visitors::find_all_ret_expressions;
811
use clippy_utils::{
912
fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, is_expr_temporary_value, peel_middle_ty_refs,
@@ -18,7 +21,7 @@ use rustc_lint::LateContext;
1821
use rustc_middle::mir::Mutability;
1922
use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
2023
use rustc_middle::ty::{
21-
self, ClauseKind, GenericArg, GenericArgKind, GenericArgsRef, ParamTy, ProjectionPredicate, TraitPredicate, Ty,
24+
self, ClauseKind, GenericArg, GenericArgKind, ParamTy, ProjectionPredicate, TraitPredicate, Ty,
2225
};
2326
use rustc_span::Symbol;
2427
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
@@ -443,33 +446,6 @@ fn skip_addr_of_ancestors<'tcx>(
443446
None
444447
}
445448

446-
/// Checks whether an expression is a function or method call and, if so, returns its `DefId`,
447-
/// `GenericArgs`, and arguments.
448-
fn get_callee_generic_args_and_args<'tcx>(
449-
cx: &LateContext<'tcx>,
450-
expr: &'tcx Expr<'tcx>,
451-
) -> Option<(
452-
DefId,
453-
GenericArgsRef<'tcx>,
454-
Option<&'tcx Expr<'tcx>>,
455-
&'tcx [Expr<'tcx>],
456-
)> {
457-
if let ExprKind::Call(callee, args) = expr.kind
458-
&& let callee_ty = cx.typeck_results().expr_ty(callee)
459-
&& let ty::FnDef(callee_def_id, _) = callee_ty.kind()
460-
{
461-
let generic_args = cx.typeck_results().node_args(callee.hir_id);
462-
return Some((*callee_def_id, generic_args, None, args));
463-
}
464-
if let ExprKind::MethodCall(_, recv, args, _) = expr.kind
465-
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
466-
{
467-
let generic_args = cx.typeck_results().node_args(expr.hir_id);
468-
return Some((method_def_id, generic_args, Some(recv), args));
469-
}
470-
None
471-
}
472-
473449
/// Returns the `TraitPredicate`s and `ProjectionPredicate`s for a function's input type.
474450
fn get_input_traits_and_projections<'tcx>(
475451
cx: &LateContext<'tcx>,
@@ -623,40 +599,14 @@ fn is_cloned_or_copied(cx: &LateContext<'_>, method_name: Symbol, method_def_id:
623599
fn is_to_owned_like<'a>(cx: &LateContext<'a>, call_expr: &Expr<'a>, method_name: Symbol, method_def_id: DefId) -> bool {
624600
is_cow_into_owned(cx, method_name, method_def_id)
625601
|| (method_name != sym::to_string && is_clone_like(cx, method_name, method_def_id))
626-
|| is_to_string_on_string_like(cx, call_expr, method_name, method_def_id)
602+
|| is_to_string_on_string_like(cx, call_expr, method_def_id)
627603
}
628604

629605
/// Returns true if the named method is `Cow::into_owned`.
630606
fn is_cow_into_owned(cx: &LateContext<'_>, method_name: Symbol, method_def_id: DefId) -> bool {
631607
method_name == sym::into_owned && is_diag_item_method(cx, method_def_id, sym::Cow)
632608
}
633609

634-
/// Returns true if the named method is `ToString::to_string` and it's called on a type that
635-
/// is string-like i.e. implements `AsRef<str>` or `Deref<Target = str>`.
636-
fn is_to_string_on_string_like<'a>(
637-
cx: &LateContext<'_>,
638-
call_expr: &'a Expr<'a>,
639-
method_name: Symbol,
640-
method_def_id: DefId,
641-
) -> bool {
642-
if method_name != sym::to_string || !is_diag_trait_item(cx, method_def_id, sym::ToString) {
643-
return false;
644-
}
645-
646-
if let Some(args) = cx.typeck_results().node_args_opt(call_expr.hir_id)
647-
&& let [generic_arg] = args.as_slice()
648-
&& let GenericArgKind::Type(ty) = generic_arg.kind()
649-
&& let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref)
650-
&& let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef)
651-
&& (cx.get_associated_type(ty, deref_trait_id, sym::Target) == Some(cx.tcx.types.str_)
652-
|| implements_trait(cx, ty, as_ref_trait_id, &[cx.tcx.types.str_.into()]))
653-
{
654-
true
655-
} else {
656-
false
657-
}
658-
}
659-
660610
fn std_map_key<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
661611
match ty.kind() {
662612
ty::Adt(adt, args)

0 commit comments

Comments
 (0)