2019-05-12 03:40:05 +00:00
|
|
|
use crate::utils::{
|
2020-06-09 14:36:01 +00:00
|
|
|
is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet, snippet_with_macro_callsite,
|
2020-05-28 13:45:24 +00:00
|
|
|
span_lint_and_help, span_lint_and_sugg,
|
2019-05-12 03:40:05 +00:00
|
|
|
};
|
2020-05-28 13:45:24 +00:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-02-21 08:39:38 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-06-09 14:36:01 +00:00
|
|
|
use rustc_middle::ty::{self, TyS};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2017-02-05 04:41:09 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2020-05-28 13:45:24 +00:00
|
|
|
/// **What it does:** Checks for `Into`, `TryInto`, `From`, `TryFrom`,`IntoIter` calls
|
|
|
|
/// that useless converts to the same type as caller.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** Redundant code.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2020-05-17 15:36:26 +00:00
|
|
|
///
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2020-05-17 15:36:26 +00:00
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// // format!() returns a `String`
|
|
|
|
/// let s: String = format!("hello").into();
|
2020-05-17 15:36:26 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let s: String = format!("hello");
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2020-05-17 15:36:26 +00:00
|
|
|
pub USELESS_CONVERSION,
|
2018-03-28 13:24:26 +00:00
|
|
|
complexity,
|
2020-05-28 13:45:24 +00:00
|
|
|
"calls to `Into`, `TryInto`, `From`, `TryFrom`, `IntoIter` that performs useless conversions to the same type"
|
2017-02-05 04:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2020-05-17 15:36:26 +00:00
|
|
|
pub struct UselessConversion {
|
2019-02-24 18:43:15 +00:00
|
|
|
try_desugar_arm: Vec<HirId>,
|
2017-02-05 04:41:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 15:36:26 +00:00
|
|
|
impl_lint_pass!(UselessConversion => [USELESS_CONVERSION]);
|
2017-02-05 04:41:09 +00:00
|
|
|
|
2020-05-28 13:45:24 +00:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2020-05-17 15:36:26 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
|
2019-08-19 16:30:32 +00:00
|
|
|
if e.span.from_expansion() {
|
2017-02-05 04:41:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-24 18:43:15 +00:00
|
|
|
if Some(&e.hir_id) == self.try_desugar_arm.last() {
|
2017-02-05 04:41:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
match e.kind {
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
|
2019-09-27 15:16:06 +00:00
|
|
|
let e = match arms[0].body.kind {
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
|
2017-02-05 04:41:09 +00:00
|
|
|
_ => return,
|
|
|
|
};
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Call(_, ref args) = e.kind {
|
2019-02-24 18:43:15 +00:00
|
|
|
self.try_desugar_arm.push(args[0].hir_id);
|
2017-02-05 04:41:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::MethodCall(ref name, .., ref args) => {
|
2019-05-17 21:53:54 +00:00
|
|
|
if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
|
2017-02-05 04:41:09 +00:00
|
|
|
let a = cx.tables.expr_ty(e);
|
|
|
|
let b = cx.tables.expr_ty(&args[0]);
|
2020-06-09 14:36:01 +00:00
|
|
|
if TyS::same_type(a, b) {
|
2018-10-27 09:01:27 +00:00
|
|
|
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
|
2020-04-17 14:01:25 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-05-17 15:36:26 +00:00
|
|
|
USELESS_CONVERSION,
|
2020-04-17 14:01:25 +00:00
|
|
|
e.span,
|
2020-05-28 13:45:24 +00:00
|
|
|
"useless conversion to the same type",
|
2020-04-17 14:01:25 +00:00
|
|
|
"consider removing `.into()`",
|
|
|
|
sugg,
|
|
|
|
Applicability::MachineApplicable, // snippet
|
|
|
|
);
|
2017-02-05 04:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 21:53:54 +00:00
|
|
|
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
|
2018-07-30 04:02:05 +00:00
|
|
|
let a = cx.tables.expr_ty(e);
|
|
|
|
let b = cx.tables.expr_ty(&args[0]);
|
2020-06-09 14:36:01 +00:00
|
|
|
if TyS::same_type(a, b) {
|
2018-07-30 04:02:05 +00:00
|
|
|
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
|
2020-04-17 14:01:25 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-05-17 15:36:26 +00:00
|
|
|
USELESS_CONVERSION,
|
2020-04-17 14:01:25 +00:00
|
|
|
e.span,
|
2020-05-28 13:45:24 +00:00
|
|
|
"useless conversion to the same type",
|
2020-04-17 14:01:25 +00:00
|
|
|
"consider removing `.into_iter()`",
|
|
|
|
sugg,
|
|
|
|
Applicability::MachineApplicable, // snippet
|
|
|
|
);
|
2018-07-30 04:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 13:45:24 +00:00
|
|
|
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;
|
|
|
|
if let Some(a_type) = substs.types().next();
|
2020-06-09 14:36:01 +00:00
|
|
|
if TyS::same_type(a_type, b);
|
2020-05-28 13:45:24 +00:00
|
|
|
|
|
|
|
then {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
USELESS_CONVERSION,
|
|
|
|
e.span,
|
|
|
|
"useless conversion to the same type",
|
|
|
|
None,
|
|
|
|
"consider removing `.try_into()`",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 04:41:09 +00:00
|
|
|
},
|
|
|
|
|
2018-11-27 20:14:15 +00:00
|
|
|
ExprKind::Call(ref path, ref args) => {
|
2020-05-28 13:45:24 +00:00
|
|
|
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]);
|
|
|
|
|
|
|
|
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 let Some(a_type) = substs.types().next();
|
2020-06-09 14:36:01 +00:00
|
|
|
if TyS::same_type(a_type, b);
|
2020-05-28 13:45:24 +00:00
|
|
|
|
|
|
|
then {
|
|
|
|
let hint = format!("consider removing `{}()`", snippet(cx, path.span, "TryFrom::try_from"));
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
USELESS_CONVERSION,
|
|
|
|
e.span,
|
|
|
|
"useless conversion to the same type",
|
|
|
|
None,
|
|
|
|
&hint,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if_chain! {
|
|
|
|
if match_def_path(cx, def_id, &paths::FROM_FROM);
|
2020-06-09 14:36:01 +00:00
|
|
|
if TyS::same_type(a, b);
|
2020-05-28 13:45:24 +00:00
|
|
|
|
|
|
|
then {
|
2018-11-27 20:14:15 +00:00
|
|
|
let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
|
|
|
|
let sugg_msg =
|
|
|
|
format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
|
2020-04-17 14:01:25 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-05-17 15:36:26 +00:00
|
|
|
USELESS_CONVERSION,
|
2020-04-17 14:01:25 +00:00
|
|
|
e.span,
|
2020-05-28 13:45:24 +00:00
|
|
|
"useless conversion to the same type",
|
2020-04-17 14:01:25 +00:00
|
|
|
&sugg_msg,
|
|
|
|
sugg,
|
|
|
|
Applicability::MachineApplicable, // snippet
|
|
|
|
);
|
2018-11-27 20:14:15 +00:00
|
|
|
}
|
2017-02-05 04:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
|
2019-02-24 18:43:15 +00:00
|
|
|
if Some(&e.hir_id) == self.try_desugar_arm.last() {
|
2017-02-05 04:41:09 +00:00
|
|
|
self.try_desugar_arm.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|