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