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