use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self, TyS};
use rustc_session::{declare_tool_lint, impl_lint_pass};
+use rustc_span::sym;
declare_clippy_lint! {
- /// **What it does:** Checks for `Into`, `TryInto`, `From`, `TryFrom`,`IntoIter` calls
- /// that useless converts to the same type as caller.
+ /// **What it does:** Checks for `Into`, `TryInto`, `From`, `TryFrom`, or `IntoIter` calls
+ /// which uselessly convert to the same type.
///
/// **Why is this bad?** Redundant code.
///
/// ```
pub USELESS_CONVERSION,
complexity,
- "calls to `Into`, `TryInto`, `From`, `TryFrom`, `IntoIter` that performs useless conversions to the same type"
+ "calls to `Into`, `TryInto`, `From`, `TryFrom`, or `IntoIter` which perform useless conversions to the same type"
}
#[derive(Default)]
cx,
USELESS_CONVERSION,
e.span,
- "useless conversion to the same type",
+ &format!("useless conversion to the same type: `{}`", b),
"consider removing `.into()`",
sugg,
Applicability::MachineApplicable, // snippet
);
}
}
- if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
+ if match_trait_method(cx, e, &paths::INTO_ITERATOR) && name.ident.name == sym::into_iter {
if let Some(parent_expr) = get_parent_expr(cx, e) {
if let ExprKind::MethodCall(ref parent_name, ..) = parent_expr.kind {
- if &*parent_name.ident.as_str() != "into_iter" {
+ if parent_name.ident.name != sym::into_iter {
return;
}
}
cx,
USELESS_CONVERSION,
e.span,
- "useless conversion to the same type",
+ &format!("useless conversion to the same type: `{}`", b),
"consider removing `.into_iter()`",
sugg,
Applicability::MachineApplicable, // snippet
if_chain! {
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 is_type_diagnostic_item(cx, a, sym::result_type);
if let ty::Adt(_, substs) = a.kind();
if let Some(a_type) = substs.types().next();
if TyS::same_type(a_type, b);
cx,
USELESS_CONVERSION,
e.span,
- "useless conversion to the same type",
+ &format!("useless conversion to the same type: `{}`", b),
None,
"consider removing `.try_into()`",
);
then {
if_chain! {
if match_def_path(cx, def_id, &paths::TRY_FROM);
- if is_type_diagnostic_item(cx, a, sym!(result_type));
+ 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();
if TyS::same_type(a_type, b);
cx,
USELESS_CONVERSION,
e.span,
- "useless conversion to the same type",
+ &format!("useless conversion to the same type: `{}`", b),
None,
&hint,
);
cx,
USELESS_CONVERSION,
e.span,
- "useless conversion to the same type",
+ &format!("useless conversion to the same type: `{}`", b),
&sugg_msg,
sugg.to_string(),
Applicability::MachineApplicable, // snippet