2019-05-11 20:40:05 -07:00
|
|
|
use crate::utils::{
|
2019-10-10 20:00:43 +02:00
|
|
|
match_def_path, match_trait_method, paths, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then,
|
2019-05-11 20:40:05 -07:00
|
|
|
};
|
2020-01-11 20:37:08 +09:00
|
|
|
use rustc::lint::{LateContext, LateLintPass};
|
2018-12-29 16:04:45 +01:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-07 01:39:50 +09:00
|
|
|
use rustc_hir::*;
|
2020-01-11 20:37:08 +09:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2017-02-05 13:41:09 +09:00
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 11:50:33 -05:00
|
|
|
/// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Redundant code.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// // format!() returns a `String`
|
|
|
|
/// let s: String = format!("hello").into();
|
|
|
|
/// ```
|
2017-02-05 13:41:09 +09:00
|
|
|
pub IDENTITY_CONVERSION,
|
2018-03-28 15:24:26 +02:00
|
|
|
complexity,
|
2018-07-29 21:02:05 -07:00
|
|
|
"using always-identical `Into`/`From`/`IntoIter` conversions"
|
2017-02-05 13:41:09 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct IdentityConversion {
|
2019-02-24 19:43:15 +01:00
|
|
|
try_desugar_arm: Vec<HirId>,
|
2017-02-05 13:41:09 +09:00
|
|
|
}
|
|
|
|
|
2019-04-08 13:43:55 -07:00
|
|
|
impl_lint_pass!(IdentityConversion => [IDENTITY_CONVERSION]);
|
2017-02-05 13:41:09 +09:00
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
|
2019-12-27 16:12:26 +09:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, '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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-12 15:30:57 +08: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" {
|
2017-02-05 13:41:09 +09:00
|
|
|
let a = cx.tables.expr_ty(e);
|
|
|
|
let b = cx.tables.expr_ty(&args[0]);
|
|
|
|
if same_tys(cx, a, b) {
|
2018-10-27 11:01:27 +02:00
|
|
|
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
|
|
|
|
|
2017-02-05 13:41:09 +09:00
|
|
|
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
|
2019-01-27 13:33:56 +01:00
|
|
|
db.span_suggestion(
|
2018-09-18 18:07:54 +03:00
|
|
|
e.span,
|
|
|
|
"consider removing `.into()`",
|
|
|
|
sugg,
|
2018-09-18 20:01:17 +03:00
|
|
|
Applicability::MachineApplicable, // snippet
|
2018-09-18 18:07:54 +03:00
|
|
|
);
|
2017-02-05 13:41:09 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-05-17 23:53:54 +02:00
|
|
|
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
|
2018-07-29 21:02:05 -07:00
|
|
|
let a = cx.tables.expr_ty(e);
|
|
|
|
let b = cx.tables.expr_ty(&args[0]);
|
|
|
|
if same_tys(cx, a, b) {
|
|
|
|
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
|
|
|
|
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
|
2019-01-27 13:33:56 +01:00
|
|
|
db.span_suggestion(
|
2018-09-18 18:07:54 +03:00
|
|
|
e.span,
|
|
|
|
"consider removing `.into_iter()`",
|
|
|
|
sugg,
|
2018-09-18 20:01:17 +03:00
|
|
|
Applicability::MachineApplicable, // snippet
|
2018-09-18 18:07:54 +03:00
|
|
|
);
|
2018-07-29 21:02:05 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 13:41:09 +09:00
|
|
|
},
|
|
|
|
|
2018-11-27 21:14:15 +01:00
|
|
|
ExprKind::Call(ref path, ref args) => {
|
2019-09-27 17:16:06 +02:00
|
|
|
if let ExprKind::Path(ref qpath) = path.kind {
|
2019-10-10 20:00:43 +02:00
|
|
|
if let Some(def_id) = cx.tables.qpath_res(qpath, path.hir_id).opt_def_id() {
|
2019-05-17 23:53:54 +02:00
|
|
|
if match_def_path(cx, def_id, &paths::FROM_FROM) {
|
2018-11-27 21:14:15 +01:00
|
|
|
let a = cx.tables.expr_ty(e);
|
|
|
|
let b = cx.tables.expr_ty(&args[0]);
|
|
|
|
if same_tys(cx, a, b) {
|
|
|
|
let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
|
|
|
|
let sugg_msg =
|
|
|
|
format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));
|
|
|
|
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
|
2019-01-27 13:33:56 +01:00
|
|
|
db.span_suggestion(
|
2018-11-27 21:14:15 +01:00
|
|
|
e.span,
|
|
|
|
&sugg_msg,
|
|
|
|
sugg,
|
|
|
|
Applicability::MachineApplicable, // snippet
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2017-02-05 13:41:09 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 16:12:26 +09:00
|
|
|
fn check_expr_post(&mut self, _: &LateContext<'a, '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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|