mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
from_over_into: suggest a correct conversion to ()
This commit is contained in:
parent
a71211d0b5
commit
e025356969
4 changed files with 37 additions and 4 deletions
|
@ -6,8 +6,8 @@ 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,
|
||||
FnRetTy, GenericArg, GenericArgs, HirId, Impl, ImplItemKind, ImplItemRef, Item, ItemKind, PatKind, Path,
|
||||
PathSegment, Ty, TyKind,
|
||||
};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::hir::nested_filter::OnlyBodies;
|
||||
|
@ -181,6 +181,9 @@ fn convert_to_from(
|
|||
let from = snippet_opt(cx, self_ty.span)?;
|
||||
let into = snippet_opt(cx, target_ty.span)?;
|
||||
|
||||
let return_type = matches!(sig.decl.output, FnRetTy::Return(_))
|
||||
.then_some(String::from("Self"))
|
||||
.unwrap_or_default();
|
||||
let mut suggestions = vec![
|
||||
// impl Into<T> for U -> impl From<T> for U
|
||||
// ~~~~ ~~~~
|
||||
|
@ -199,7 +202,7 @@ fn convert_to_from(
|
|||
(self_ident.span, format!("val: {from}")),
|
||||
// fn into(self) -> T -> fn into(self) -> Self
|
||||
// ~ ~~~~
|
||||
(sig.decl.output.span(), String::from("Self")),
|
||||
(sig.decl.output.span(), return_type),
|
||||
];
|
||||
|
||||
let mut finder = SelfFinder {
|
||||
|
|
|
@ -89,4 +89,12 @@ fn msrv_1_41() {
|
|||
}
|
||||
}
|
||||
|
||||
fn issue_12138() {
|
||||
struct Hello;
|
||||
|
||||
impl From<Hello> for () {
|
||||
fn from(val: Hello) {}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -89,4 +89,12 @@ fn msrv_1_41() {
|
|||
}
|
||||
}
|
||||
|
||||
fn issue_12138() {
|
||||
struct Hello;
|
||||
|
||||
impl Into<()> for Hello {
|
||||
fn into(self) {}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -86,5 +86,19 @@ LL ~ fn from(val: Vec<T>) -> Self {
|
|||
LL ~ FromOverInto(val)
|
||||
|
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
|
||||
--> $DIR/from_over_into.rs:95:5
|
||||
|
|
||||
LL | impl Into<()> for Hello {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: `impl From<Local> for Foreign` is allowed by the orphan rules, for more information see
|
||||
https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence
|
||||
help: replace the `Into` implementation with `From<issue_12138::Hello>`
|
||||
|
|
||||
LL ~ impl From<Hello> for () {
|
||||
LL ~ fn from(val: Hello) {}
|
||||
|
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue