2022-10-23 13:18:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
|
|
|
use clippy_utils::macros::span_is_local;
|
2022-12-01 17:29:38 +00:00
|
|
|
use clippy_utils::msrvs::{self, Msrv};
|
|
|
|
use clippy_utils::path_def_id;
|
2022-10-23 13:18:45 +00:00
|
|
|
use clippy_utils::source::snippet_opt;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::intravisit::{walk_path, Visitor};
|
|
|
|
use rustc_hir::{
|
|
|
|
GenericArg, GenericArgs, HirId, Impl, ImplItemKind, ImplItemRef, Item, ItemKind, PatKind, Path, PathSegment, Ty,
|
|
|
|
TyKind,
|
|
|
|
};
|
2021-12-04 15:09:15 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2022-12-17 13:12:54 +00:00
|
|
|
use rustc_middle::{hir::nested_filter::OnlyBodies, ty};
|
2021-01-02 15:29:43 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2022-10-23 13:18:45 +00:00
|
|
|
use rustc_span::symbol::{kw, sym};
|
|
|
|
use rustc_span::{Span, Symbol};
|
2021-01-02 15:29:43 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Searches for implementations of the `Into<..>` trait and suggests to implement `From<..>` instead.
|
2021-01-02 15:29:43 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// According the std docs implementing `From<..>` is preferred since it gives you `Into<..>` for free where the reverse isn't true.
|
2021-01-02 15:29:43 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2021-01-02 15:29:43 +00:00
|
|
|
/// ```rust
|
|
|
|
/// struct StringWrapper(String);
|
|
|
|
///
|
|
|
|
/// impl Into<StringWrapper> for String {
|
|
|
|
/// fn into(self) -> StringWrapper {
|
|
|
|
/// StringWrapper(self)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// struct StringWrapper(String);
|
|
|
|
///
|
|
|
|
/// impl From<String> for StringWrapper {
|
|
|
|
/// fn from(s: String) -> StringWrapper {
|
|
|
|
/// StringWrapper(s)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.51.0"]
|
2021-01-02 15:29:43 +00:00
|
|
|
pub FROM_OVER_INTO,
|
|
|
|
style,
|
|
|
|
"Warns on implementations of `Into<..>` to use `From<..>`"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FromOverInto {
|
2022-12-01 17:29:38 +00:00
|
|
|
msrv: Msrv,
|
2021-01-02 15:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromOverInto {
|
|
|
|
#[must_use]
|
2022-12-01 17:29:38 +00:00
|
|
|
pub fn new(msrv: Msrv) -> Self {
|
2021-01-02 15:29:43 +00:00
|
|
|
FromOverInto { msrv }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(FromOverInto => [FROM_OVER_INTO]);
|
|
|
|
|
2022-01-13 12:18:19 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for FromOverInto {
|
2022-10-23 13:18:45 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
2022-12-01 17:29:38 +00:00
|
|
|
if !self.msrv.meets(msrvs::RE_REBALANCING_COHERENCE) || !span_is_local(item.span) {
|
2021-01-02 15:29:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-23 13:18:45 +00:00
|
|
|
if let ItemKind::Impl(Impl {
|
|
|
|
of_trait: Some(hir_trait_ref),
|
|
|
|
self_ty,
|
|
|
|
items: [impl_item_ref],
|
|
|
|
..
|
|
|
|
}) = item.kind
|
|
|
|
&& let Some(into_trait_seg) = hir_trait_ref.path.segments.last()
|
|
|
|
// `impl Into<target_ty> for self_ty`
|
|
|
|
&& let Some(GenericArgs { args: [GenericArg::Type(target_ty)], .. }) = into_trait_seg.args
|
2023-01-10 21:57:22 +00:00
|
|
|
&& let Some(middle_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id).map(ty::EarlyBinder::subst_identity)
|
2022-10-23 13:18:45 +00:00
|
|
|
&& cx.tcx.is_diagnostic_item(sym::Into, middle_trait_ref.def_id)
|
2022-12-17 13:12:54 +00:00
|
|
|
&& !matches!(middle_trait_ref.substs.type_at(1).kind(), ty::Alias(ty::Opaque, _))
|
2022-10-23 13:18:45 +00:00
|
|
|
{
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
FROM_OVER_INTO,
|
|
|
|
cx.tcx.sess.source_map().guess_head_span(item.span),
|
|
|
|
"an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true",
|
|
|
|
|diag| {
|
|
|
|
// If the target type is likely foreign mention the orphan rules as it's a common source of confusion
|
|
|
|
if path_def_id(cx, target_ty.peel_refs()).map_or(true, |id| !id.is_local()) {
|
|
|
|
diag.help(
|
|
|
|
"`impl From<Local> for Foreign` is allowed by the orphan rules, for more information see\n\
|
|
|
|
https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let message = format!("replace the `Into` implentation with `From<{}>`", middle_trait_ref.self_ty());
|
|
|
|
if let Some(suggestions) = convert_to_from(cx, into_trait_seg, target_ty, self_ty, impl_item_ref) {
|
|
|
|
diag.multipart_suggestion(message, suggestions, Applicability::MachineApplicable);
|
|
|
|
} else {
|
|
|
|
diag.help(message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-01-02 15:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extract_msrv_attr!(LateContext);
|
|
|
|
}
|
2022-10-23 13:18:45 +00:00
|
|
|
|
|
|
|
/// Finds the occurences of `Self` and `self`
|
|
|
|
struct SelfFinder<'a, 'tcx> {
|
|
|
|
cx: &'a LateContext<'tcx>,
|
|
|
|
/// Occurences of `Self`
|
|
|
|
upper: Vec<Span>,
|
|
|
|
/// Occurences of `self`
|
|
|
|
lower: Vec<Span>,
|
|
|
|
/// If any of the `self`/`Self` usages were from an expansion, or the body contained a binding
|
|
|
|
/// already named `val`
|
|
|
|
invalid: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for SelfFinder<'a, 'tcx> {
|
|
|
|
type NestedFilter = OnlyBodies;
|
|
|
|
|
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.cx.tcx.hir()
|
|
|
|
}
|
|
|
|
|
2022-11-25 08:26:36 +00:00
|
|
|
fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) {
|
2022-10-23 13:18:45 +00:00
|
|
|
for segment in path.segments {
|
|
|
|
match segment.ident.name {
|
|
|
|
kw::SelfLower => self.lower.push(segment.ident.span),
|
|
|
|
kw::SelfUpper => self.upper.push(segment.ident.span),
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.invalid |= path.span.from_expansion();
|
|
|
|
if !self.invalid {
|
|
|
|
walk_path(self, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_name(&mut self, name: Symbol) {
|
|
|
|
if name == sym::val {
|
|
|
|
self.invalid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn convert_to_from(
|
|
|
|
cx: &LateContext<'_>,
|
|
|
|
into_trait_seg: &PathSegment<'_>,
|
|
|
|
target_ty: &Ty<'_>,
|
|
|
|
self_ty: &Ty<'_>,
|
|
|
|
impl_item_ref: &ImplItemRef,
|
|
|
|
) -> Option<Vec<(Span, String)>> {
|
|
|
|
let impl_item = cx.tcx.hir().impl_item(impl_item_ref.id);
|
|
|
|
let ImplItemKind::Fn(ref sig, body_id) = impl_item.kind else { return None };
|
|
|
|
let body = cx.tcx.hir().body(body_id);
|
|
|
|
let [input] = body.params else { return None };
|
|
|
|
let PatKind::Binding(.., self_ident, None) = input.pat.kind else { return None };
|
|
|
|
|
|
|
|
let from = snippet_opt(cx, self_ty.span)?;
|
|
|
|
let into = snippet_opt(cx, target_ty.span)?;
|
|
|
|
|
|
|
|
let mut suggestions = vec![
|
|
|
|
// impl Into<T> for U -> impl From<T> for U
|
|
|
|
// ~~~~ ~~~~
|
|
|
|
(into_trait_seg.ident.span, String::from("From")),
|
|
|
|
// impl Into<T> for U -> impl Into<U> for U
|
|
|
|
// ~ ~
|
|
|
|
(target_ty.span, from.clone()),
|
|
|
|
// impl Into<T> for U -> impl Into<T> for T
|
|
|
|
// ~ ~
|
|
|
|
(self_ty.span, into),
|
|
|
|
// fn into(self) -> T -> fn from(self) -> T
|
|
|
|
// ~~~~ ~~~~
|
|
|
|
(impl_item.ident.span, String::from("from")),
|
|
|
|
// fn into([mut] self) -> T -> fn into([mut] v: T) -> T
|
|
|
|
// ~~~~ ~~~~
|
|
|
|
(self_ident.span, format!("val: {from}")),
|
|
|
|
// fn into(self) -> T -> fn into(self) -> Self
|
|
|
|
// ~ ~~~~
|
|
|
|
(sig.decl.output.span(), String::from("Self")),
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut finder = SelfFinder {
|
|
|
|
cx,
|
|
|
|
upper: Vec::new(),
|
|
|
|
lower: Vec::new(),
|
|
|
|
invalid: false,
|
|
|
|
};
|
|
|
|
finder.visit_expr(body.value);
|
|
|
|
|
|
|
|
if finder.invalid {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't try to replace e.g. `Self::default()` with `&[T]::default()`
|
|
|
|
if !finder.upper.is_empty() && !matches!(self_ty.kind, TyKind::Path(_)) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
for span in finder.upper {
|
|
|
|
suggestions.push((span, from.clone()));
|
|
|
|
}
|
|
|
|
for span in finder.lower {
|
|
|
|
suggestions.push((span, String::from("val")));
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(suggestions)
|
|
|
|
}
|