2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2021-07-29 10:16:06 +00:00
|
|
|
use clippy_utils::{meets_msrv, msrvs};
|
2021-01-02 15:29:43 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_semver::RustcVersion;
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-07-29 10:16:06 +00:00
|
|
|
use rustc_span::symbol::sym;
|
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 {
|
|
|
|
msrv: Option<RustcVersion>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromOverInto {
|
|
|
|
#[must_use]
|
|
|
|
pub fn new(msrv: Option<RustcVersion>) -> Self {
|
|
|
|
FromOverInto { msrv }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(FromOverInto => [FROM_OVER_INTO]);
|
|
|
|
|
2022-01-13 12:18:19 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for FromOverInto {
|
2021-01-02 15:29:43 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
|
2021-04-27 14:55:11 +00:00
|
|
|
if !meets_msrv(self.msrv.as_ref(), &msrvs::RE_REBALANCING_COHERENCE) {
|
2021-01-02 15:29:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if_chain! {
|
|
|
|
if let hir::ItemKind::Impl{ .. } = &item.kind;
|
2021-01-30 16:47:51 +00:00
|
|
|
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(item.def_id);
|
2021-10-02 23:51:01 +00:00
|
|
|
if cx.tcx.is_diagnostic_item(sym::Into, impl_trait_ref.def_id);
|
2021-01-02 15:29:43 +00:00
|
|
|
|
|
|
|
then {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
FROM_OVER_INTO,
|
2021-01-15 09:56:44 +00:00
|
|
|
cx.tcx.sess.source_map().guess_head_span(item.span),
|
2021-01-02 15:29:43 +00:00
|
|
|
"an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true",
|
|
|
|
None,
|
2021-04-22 09:31:13 +00:00
|
|
|
&format!("consider to implement `From<{}>` instead", impl_trait_ref.self_ty()),
|
2021-01-02 15:29:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extract_msrv_attr!(LateContext);
|
|
|
|
}
|