rust-clippy/clippy_lints/src/identity_conversion.rs

124 lines
4.9 KiB
Rust
Raw Normal View History

2019-05-12 03:40:05 +00:00
use crate::utils::{
2019-05-14 08:06:21 +00:00
in_macro_or_desugar, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite,
span_lint_and_then,
2019-05-12 03:40:05 +00:00
};
use crate::utils::{paths, resolve_node};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2019-04-08 20:43:55 +00:00
use rustc::{declare_tool_lint, impl_lint_pass};
use rustc_errors::Applicability;
2018-03-28 13:24:26 +00:00
declare_clippy_lint! {
/// **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();
/// ```
pub IDENTITY_CONVERSION,
2018-03-28 13:24:26 +00:00
complexity,
"using always-identical `Into`/`From`/`IntoIter` conversions"
}
#[derive(Default)]
pub struct IdentityConversion {
2019-02-24 18:43:15 +00:00
try_desugar_arm: Vec<HirId>,
}
2019-04-08 20:43:55 +00:00
impl_lint_pass!(IdentityConversion => [IDENTITY_CONVERSION]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
2019-05-12 03:40:05 +00:00
if in_macro_or_desugar(e.span) {
return;
}
2019-02-24 18:43:15 +00:00
if Some(&e.hir_id) == self.try_desugar_arm.last() {
return;
}
match e.node {
2018-07-12 07:30:57 +00:00
ExprKind::Match(_, ref arms, MatchSource::TryDesugar) => {
let e = match arms[0].body.node {
2018-07-12 07:30:57 +00:00
ExprKind::Ret(Some(ref e)) | ExprKind::Break(_, Some(ref e)) => e,
_ => return,
};
2018-07-12 07:30:57 +00:00
if let ExprKind::Call(_, ref args) = e.node {
2019-02-24 18:43:15 +00:00
self.try_desugar_arm.push(args[0].hir_id);
}
},
2018-07-12 07:30:57 +00:00
ExprKind::MethodCall(ref name, .., ref args) => {
2019-05-17 21:53:54 +00:00
if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
let a = cx.tables.expr_ty(e);
let b = cx.tables.expr_ty(&args[0]);
if same_tys(cx, a, b) {
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
db.span_suggestion(
2018-09-18 15:07:54 +00:00
e.span,
"consider removing `.into()`",
sugg,
Applicability::MachineApplicable, // snippet
2018-09-18 15:07:54 +00:00
);
});
}
}
2019-05-17 21:53:54 +00: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| {
db.span_suggestion(
2018-09-18 15:07:54 +00:00
e.span,
"consider removing `.into_iter()`",
sugg,
Applicability::MachineApplicable, // snippet
2018-09-18 15:07:54 +00:00
);
});
}
}
},
2018-11-27 20:14:15 +00:00
ExprKind::Call(ref path, ref args) => {
if let ExprKind::Path(ref qpath) = path.node {
if let Some(def_id) = resolve_node(cx, qpath, path.hir_id).opt_def_id() {
2019-05-17 21:53:54 +00:00
if match_def_path(cx, def_id, &paths::FROM_FROM) {
2018-11-27 20:14:15 +00: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| {
db.span_suggestion(
2018-11-27 20:14:15 +00:00
e.span,
&sugg_msg,
sugg,
Applicability::MachineApplicable, // snippet
);
});
}
}
}
}
},
_ => {},
}
}
fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
2019-02-24 18:43:15 +00:00
if Some(&e.hir_id) == self.try_desugar_arm.last() {
self.try_desugar_arm.pop();
}
}
}