+use crate::utils::sugg::Sugg;
use crate::utils::{
- is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet, snippet_with_macro_callsite,
- span_lint_and_help, span_lint_and_sugg,
+ get_parent_expr, is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet,
+ snippet_with_macro_callsite, span_lint_and_help, span_lint_and_sugg,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self, TyS};
use rustc_session::{declare_tool_lint, impl_lint_pass};
+use rustc_span::sym;
declare_clippy_lint! {
- /// **What it does:** Checks for `Into`, `TryInto`, `From`, `TryFrom`,`IntoIter` calls
- /// that useless converts to the same type as caller.
+ /// **What it does:** Checks for `Into`, `TryInto`, `From`, `TryFrom`, or `IntoIter` calls
+ /// which uselessly convert to the same type.
///
/// **Why is this bad?** Redundant code.
///
/// ```
pub USELESS_CONVERSION,
complexity,
- "calls to `Into`, `TryInto`, `From`, `TryFrom`, `IntoIter` that performs useless conversions to the same type"
+ "calls to `Into`, `TryInto`, `From`, `TryFrom`, or `IntoIter` which perform useless conversions to the same type"
}
#[derive(Default)]
impl_lint_pass!(UselessConversion => [USELESS_CONVERSION]);
#[allow(clippy::too_many_lines)]
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
- fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
+impl<'tcx> LateLintPass<'tcx> for UselessConversion {
+ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
if e.span.from_expansion() {
return;
}
ExprKind::MethodCall(ref name, .., ref args, _) => {
if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
- let a = cx.tables().expr_ty(e);
- let b = cx.tables().expr_ty(&args[0]);
+ let a = cx.typeck_results().expr_ty(e);
+ let b = cx.typeck_results().expr_ty(&args[0]);
if TyS::same_type(a, b) {
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
span_lint_and_sugg(
cx,
USELESS_CONVERSION,
e.span,
- "useless conversion to the same type",
+ &format!("useless conversion to the same type: `{}`", b),
"consider removing `.into()`",
sugg,
Applicability::MachineApplicable, // snippet
);
}
}
- if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
- let a = cx.tables().expr_ty(e);
- let b = cx.tables().expr_ty(&args[0]);
+ if match_trait_method(cx, e, &paths::INTO_ITERATOR) && name.ident.name == sym::into_iter {
+ if let Some(parent_expr) = get_parent_expr(cx, e) {
+ if let ExprKind::MethodCall(ref parent_name, ..) = parent_expr.kind {
+ if parent_name.ident.name != sym::into_iter {
+ return;
+ }
+ }
+ }
+ let a = cx.typeck_results().expr_ty(e);
+ let b = cx.typeck_results().expr_ty(&args[0]);
if TyS::same_type(a, b) {
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
span_lint_and_sugg(
cx,
USELESS_CONVERSION,
e.span,
- "useless conversion to the same type",
+ &format!("useless conversion to the same type: `{}`", b),
"consider removing `.into_iter()`",
sugg,
Applicability::MachineApplicable, // snippet
}
if match_trait_method(cx, e, &paths::TRY_INTO_TRAIT) && &*name.ident.as_str() == "try_into" {
if_chain! {
- let a = cx.tables().expr_ty(e);
- let b = cx.tables().expr_ty(&args[0]);
- if is_type_diagnostic_item(cx, a, sym!(result_type));
- if let ty::Adt(_, substs) = a.kind;
+ let a = cx.typeck_results().expr_ty(e);
+ let b = cx.typeck_results().expr_ty(&args[0]);
+ if is_type_diagnostic_item(cx, a, sym::result_type);
+ if let ty::Adt(_, substs) = a.kind();
if let Some(a_type) = substs.types().next();
if TyS::same_type(a_type, b);
cx,
USELESS_CONVERSION,
e.span,
- "useless conversion to the same type",
+ &format!("useless conversion to the same type: `{}`", b),
None,
"consider removing `.try_into()`",
);
if_chain! {
if args.len() == 1;
if let ExprKind::Path(ref qpath) = path.kind;
- if let Some(def_id) = cx.tables().qpath_res(qpath, path.hir_id).opt_def_id();
- let a = cx.tables().expr_ty(e);
- let b = cx.tables().expr_ty(&args[0]);
+ if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
+ let a = cx.typeck_results().expr_ty(e);
+ let b = cx.typeck_results().expr_ty(&args[0]);
then {
if_chain! {
if match_def_path(cx, def_id, &paths::TRY_FROM);
- if is_type_diagnostic_item(cx, a, sym!(result_type));
- if let ty::Adt(_, substs) = a.kind;
+ if is_type_diagnostic_item(cx, a, sym::result_type);
+ if let ty::Adt(_, substs) = a.kind();
if let Some(a_type) = substs.types().next();
if TyS::same_type(a_type, b);
cx,
USELESS_CONVERSION,
e.span,
- "useless conversion to the same type",
+ &format!("useless conversion to the same type: `{}`", b),
None,
&hint,
);
if TyS::same_type(a, b);
then {
- let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
+ let sugg = Sugg::hir_with_macro_callsite(cx, &args[0], "<expr>").maybe_par();
let sugg_msg =
format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
span_lint_and_sugg(
cx,
USELESS_CONVERSION,
e.span,
- "useless conversion to the same type",
+ &format!("useless conversion to the same type: `{}`", b),
&sugg_msg,
- sugg,
+ sugg.to_string(),
Applicability::MachineApplicable, // snippet
);
}
}
}
- fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
+ fn check_expr_post(&mut self, _: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
if Some(&e.hir_id) == self.try_desugar_arm.last() {
self.try_desugar_arm.pop();
}