2018-11-27 14:14:15 -06:00
|
|
|
use crate::utils::{
|
|
|
|
in_macro, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then,
|
|
|
|
};
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::{opt_def_id, paths, resolve_node};
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc::{declare_tool_lint, lint_array};
|
|
|
|
use rustc_errors::Applicability;
|
2017-02-04 22:41:09 -06:00
|
|
|
|
2018-07-29 23:02:05 -05:00
|
|
|
/// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions.
|
2017-02-04 22:41:09 -06:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** Redundant code.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// // format!() returns a `String`
|
|
|
|
/// let s: String = format!("hello").into();
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2017-02-04 22:41:09 -06:00
|
|
|
pub IDENTITY_CONVERSION,
|
2018-03-28 08:24:26 -05:00
|
|
|
complexity,
|
2018-07-29 23:02:05 -05:00
|
|
|
"using always-identical `Into`/`From`/`IntoIter` conversions"
|
2017-02-04 22:41:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct IdentityConversion {
|
2019-02-24 12:43:15 -06:00
|
|
|
try_desugar_arm: Vec<HirId>,
|
2017-02-04 22:41:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for IdentityConversion {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(IDENTITY_CONVERSION)
|
|
|
|
}
|
2019-01-26 13:40:55 -06:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"IdentityConversion"
|
|
|
|
}
|
2017-02-04 22:41:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
|
|
|
|
if in_macro(e.span) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
match e.node {
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
|
2017-02-04 22:41:09 -06:00
|
|
|
let e = match arms[0].body.node {
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
|
2017-02-04 22:41:09 -06:00
|
|
|
_ => return,
|
|
|
|
};
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::Call(_, ref args) = e.node {
|
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
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::MethodCall(ref name, .., ref args) => {
|
2018-06-28 08:46:58 -05:00
|
|
|
if match_trait_method(cx, e, &paths::INTO[..]) && &*name.ident.as_str() == "into" {
|
2017-02-04 22:41:09 -06: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 04:01:27 -05:00
|
|
|
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
|
|
|
|
|
2017-02-04 22:41:09 -06:00
|
|
|
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
|
2019-01-27 06:33:56 -06:00
|
|
|
db.span_suggestion(
|
2018-09-18 10:07:54 -05:00
|
|
|
e.span,
|
|
|
|
"consider removing `.into()`",
|
|
|
|
sugg,
|
2018-09-18 12:01:17 -05:00
|
|
|
Applicability::MachineApplicable, // snippet
|
2018-09-18 10:07:54 -05:00
|
|
|
);
|
2017-02-04 22:41:09 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-07-29 23:02:05 -05:00
|
|
|
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
|
|
|
|
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 06:33:56 -06:00
|
|
|
db.span_suggestion(
|
2018-09-18 10:07:54 -05:00
|
|
|
e.span,
|
|
|
|
"consider removing `.into_iter()`",
|
|
|
|
sugg,
|
2018-09-18 12:01:17 -05:00
|
|
|
Applicability::MachineApplicable, // snippet
|
2018-09-18 10:07:54 -05:00
|
|
|
);
|
2018-07-29 23:02:05 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-02-04 22:41:09 -06:00
|
|
|
},
|
|
|
|
|
2018-11-27 14:14:15 -06:00
|
|
|
ExprKind::Call(ref path, ref args) => {
|
|
|
|
if let ExprKind::Path(ref qpath) = path.node {
|
|
|
|
if let Some(def_id) = opt_def_id(resolve_node(cx, qpath, path.hir_id)) {
|
|
|
|
if match_def_path(cx.tcx, def_id, &paths::FROM_FROM[..]) {
|
|
|
|
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 06:33:56 -06:00
|
|
|
db.span_suggestion(
|
2018-11-27 14:14:15 -06:00
|
|
|
e.span,
|
|
|
|
&sugg_msg,
|
|
|
|
sugg,
|
|
|
|
Applicability::MachineApplicable, // snippet
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2017-02-04 22:41:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr_post(&mut self, _: &LateContext<'a, '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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|