1 use crate::utils::paths::INTO;
2 use crate::utils::{match_def_path, span_lint_and_help};
3 use if_chain::if_chain;
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 /// **What it does:** Searches for implementations of the `Into<..>` trait and suggests to implement `From<..>` instead.
11 /// **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.
13 /// **Known problems:** None.
18 /// struct StringWrapper(String);
20 /// impl Into<StringWrapper> for String {
21 /// fn into(self) -> StringWrapper {
22 /// StringWrapper(self)
28 /// struct StringWrapper(String);
30 /// impl From<String> for StringWrapper {
31 /// fn from(s: String) -> StringWrapper {
38 "Warns on implementations of `Into<..>` to use `From<..>`"
41 declare_lint_pass!(FromOverInto => [FROM_OVER_INTO]);
43 impl LateLintPass<'_> for FromOverInto {
44 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
45 let impl_def_id = cx.tcx.hir().local_def_id(item.hir_id);
47 if let hir::ItemKind::Impl{ .. } = &item.kind;
48 if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id);
49 if match_def_path(cx, impl_trait_ref.def_id, &INTO);
56 "An implementation of From is preferred since it gives you Into<..> for free where the reverse isn't true.",
58 "consider implement From instead",