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