Don't lint from_over_into for opaque types

This commit is contained in:
Jason Newcomb 2022-11-29 00:20:53 -05:00
parent d822110d3b
commit 6ecdff07e5
4 changed files with 21 additions and 6 deletions

View File

@ -10,7 +10,7 @@ use rustc_hir::{
TyKind,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter::OnlyBodies;
use rustc_middle::{hir::nested_filter::OnlyBodies, ty};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::symbol::{kw, sym};
use rustc_span::{Span, Symbol};
@ -78,6 +78,7 @@ impl<'tcx> LateLintPass<'tcx> for FromOverInto {
&& let Some(GenericArgs { args: [GenericArg::Type(target_ty)], .. }) = into_trait_seg.args
&& let Some(middle_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id)
&& cx.tcx.is_diagnostic_item(sym::Into, middle_trait_ref.def_id)
&& !matches!(middle_trait_ref.substs.type_at(1).kind(), ty::Opaque(..))
{
span_lint_and_then(
cx,

View File

@ -1,5 +1,6 @@
// run-rustfix
#![feature(type_alias_impl_trait)]
#![warn(clippy::from_over_into)]
#![allow(unused)]
@ -81,4 +82,10 @@ fn msrv_1_41() {
}
}
type Opaque = impl Sized;
struct IntoOpaque;
impl Into<Opaque> for IntoOpaque {
fn into(self) -> Opaque {}
}
fn main() {}

View File

@ -1,5 +1,6 @@
// run-rustfix
#![feature(type_alias_impl_trait)]
#![warn(clippy::from_over_into)]
#![allow(unused)]
@ -81,4 +82,10 @@ fn msrv_1_41() {
}
}
type Opaque = impl Sized;
struct IntoOpaque;
impl Into<Opaque> for IntoOpaque {
fn into(self) -> Opaque {}
}
fn main() {}

View File

@ -1,5 +1,5 @@
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:9:1
--> $DIR/from_over_into.rs:10:1
|
LL | impl Into<StringWrapper> for String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -13,7 +13,7 @@ LL ~ StringWrapper(val)
|
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:17:1
--> $DIR/from_over_into.rs:18:1
|
LL | impl Into<SelfType> for String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -26,7 +26,7 @@ LL ~ SelfType(String::new())
|
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:32:1
--> $DIR/from_over_into.rs:33:1
|
LL | impl Into<SelfKeywords> for X {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -41,7 +41,7 @@ LL ~ let _: X = val;
|
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:44:1
--> $DIR/from_over_into.rs:45:1
|
LL | impl core::convert::Into<bool> for crate::ExplicitPaths {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -59,7 +59,7 @@ LL ~ val.0
|
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:77:5
--> $DIR/from_over_into.rs:78:5
|
LL | impl<T> Into<FromOverInto<T>> for Vec<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^