Skip to content

Commit 1aad754

Browse files
committed
Auto merge of rust-lang#7223 - ThibsG:FpUselessConversion7205, r=camsteffen
Fix FPs about generic args Fix 2 false positives in [`use_self`] and [`useless_conversion`] lints, by taking into account generic args and comparing them. Fixes: rust-lang#7205 Fixes: rust-lang#7206 changelog: Fix FPs about generic args in [`use_self`] and [`useless_conversion`] lints
2 parents cec8d95 + 2fb35ce commit 1aad754

File tree

9 files changed

+161
-14
lines changed

9 files changed

+161
-14
lines changed

clippy_lints/src/use_self.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::ty::same_type_and_consts;
34
use clippy_utils::{in_macro, meets_msrv, msrvs};
45
use if_chain::if_chain;
56
use rustc_errors::Applicability;
6-
use rustc_hir as hir;
7-
use rustc_hir::def::DefKind;
87
use rustc_hir::{
9-
def,
8+
self as hir,
9+
def::{self, DefKind},
1010
def_id::LocalDefId,
1111
intravisit::{walk_ty, NestedVisitorMap, Visitor},
1212
Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Node, Path, PathSegment,
1313
QPath, TyKind,
1414
};
1515
use rustc_lint::{LateContext, LateLintPass, LintContext};
1616
use rustc_middle::hir::map::Map;
17-
use rustc_middle::ty::{AssocKind, Ty, TyS};
17+
use rustc_middle::ty::{AssocKind, Ty};
1818
use rustc_semver::RustcVersion;
1919
use rustc_session::{declare_tool_lint, impl_lint_pass};
2020
use rustc_span::{BytePos, Span};
@@ -459,7 +459,7 @@ fn in_impl(cx: &LateContext<'tcx>, hir_ty: &hir::Ty<'_>) -> bool {
459459

460460
fn should_lint_ty(hir_ty: &hir::Ty<'_>, ty: Ty<'_>, self_ty: Ty<'_>) -> bool {
461461
if_chain! {
462-
if TyS::same_type(ty, self_ty);
462+
if same_type_and_consts(ty, self_ty);
463463
if let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind;
464464
then {
465465
!matches!(path.res, def::Res::SelfTy(..))

clippy_lints/src/useless_conversion.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
22
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
33
use clippy_utils::sugg::Sugg;
4-
use clippy_utils::ty::is_type_diagnostic_item;
4+
use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
55
use clippy_utils::{get_parent_expr, match_def_path, match_trait_method, paths};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
99
use rustc_lint::{LateContext, LateLintPass};
10-
use rustc_middle::ty::{self, TyS};
10+
use rustc_middle::ty;
1111
use rustc_session::{declare_tool_lint, impl_lint_pass};
1212
use rustc_span::sym;
1313

@@ -67,7 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
6767
if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
6868
let a = cx.typeck_results().expr_ty(e);
6969
let b = cx.typeck_results().expr_ty(&args[0]);
70-
if TyS::same_type(a, b) {
70+
if same_type_and_consts(a, b) {
7171
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
7272
span_lint_and_sugg(
7373
cx,
@@ -90,7 +90,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
9090
}
9191
let a = cx.typeck_results().expr_ty(e);
9292
let b = cx.typeck_results().expr_ty(&args[0]);
93-
if TyS::same_type(a, b) {
93+
if same_type_and_consts(a, b) {
9494
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
9595
span_lint_and_sugg(
9696
cx,
@@ -110,7 +110,8 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
110110
if is_type_diagnostic_item(cx, a, sym::result_type);
111111
if let ty::Adt(_, substs) = a.kind();
112112
if let Some(a_type) = substs.types().next();
113-
if TyS::same_type(a_type, b);
113+
if same_type_and_consts(a_type, b);
114+
114115
then {
115116
span_lint_and_help(
116117
cx,
@@ -137,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
137138
if is_type_diagnostic_item(cx, a, sym::result_type);
138139
if let ty::Adt(_, substs) = a.kind();
139140
if let Some(a_type) = substs.types().next();
140-
if TyS::same_type(a_type, b);
141+
if same_type_and_consts(a_type, b);
141142

142143
then {
143144
let hint = format!("consider removing `{}()`", snippet(cx, path.span, "TryFrom::try_from"));
@@ -154,7 +155,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
154155

155156
if_chain! {
156157
if match_def_path(cx, def_id, &paths::FROM_FROM);
157-
if TyS::same_type(a, b);
158+
if same_type_and_consts(a, b);
158159

159160
then {
160161
let sugg = Sugg::hir_with_macro_callsite(cx, &args[0], "<expr>").maybe_par();

clippy_utils/src/ty.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,27 @@ pub fn walk_ptrs_ty_depth(ty: Ty<'_>) -> (Ty<'_>, usize) {
322322
}
323323
inner(ty, 0)
324324
}
325+
326+
/// Returns `true` if types `a` and `b` are same types having same `Const` generic args,
327+
/// otherwise returns `false`
328+
pub fn same_type_and_consts(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
329+
match (&a.kind(), &b.kind()) {
330+
(&ty::Adt(did_a, substs_a), &ty::Adt(did_b, substs_b)) => {
331+
if did_a != did_b {
332+
return false;
333+
}
334+
335+
substs_a
336+
.iter()
337+
.zip(substs_b.iter())
338+
.all(|(arg_a, arg_b)| match (arg_a.unpack(), arg_b.unpack()) {
339+
(GenericArgKind::Const(inner_a), GenericArgKind::Const(inner_b)) => inner_a == inner_b,
340+
(GenericArgKind::Type(type_a), GenericArgKind::Type(type_b)) => {
341+
same_type_and_consts(type_a, type_b)
342+
},
343+
_ => true,
344+
})
345+
},
346+
_ => a == b,
347+
}
348+
}

tests/ui/use_self.fixed

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,33 @@ mod issue6818 {
462462
a: i32,
463463
}
464464
}
465+
466+
mod issue7206 {
467+
struct MyStruct<const C: char>;
468+
impl From<MyStruct<'a'>> for MyStruct<'b'> {
469+
fn from(_s: MyStruct<'a'>) -> Self {
470+
Self
471+
}
472+
}
473+
474+
// keep linting non-`Const` generic args
475+
struct S<'a> {
476+
inner: &'a str,
477+
}
478+
479+
struct S2<T> {
480+
inner: T,
481+
}
482+
483+
impl<T> S2<T> {
484+
fn new() -> Self {
485+
unimplemented!();
486+
}
487+
}
488+
489+
impl<'a> S2<S<'a>> {
490+
fn new_again() -> Self {
491+
Self::new()
492+
}
493+
}
494+
}

tests/ui/use_self.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,33 @@ mod issue6818 {
462462
a: i32,
463463
}
464464
}
465+
466+
mod issue7206 {
467+
struct MyStruct<const C: char>;
468+
impl From<MyStruct<'a'>> for MyStruct<'b'> {
469+
fn from(_s: MyStruct<'a'>) -> Self {
470+
Self
471+
}
472+
}
473+
474+
// keep linting non-`Const` generic args
475+
struct S<'a> {
476+
inner: &'a str,
477+
}
478+
479+
struct S2<T> {
480+
inner: T,
481+
}
482+
483+
impl<T> S2<T> {
484+
fn new() -> Self {
485+
unimplemented!();
486+
}
487+
}
488+
489+
impl<'a> S2<S<'a>> {
490+
fn new_again() -> Self {
491+
S2::new()
492+
}
493+
}
494+
}

tests/ui/use_self.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,11 @@ error: unnecessary structure name repetition
162162
LL | A::new::<submod::B>(submod::B {})
163163
| ^ help: use the applicable keyword: `Self`
164164

165-
error: aborting due to 27 previous errors
165+
error: unnecessary structure name repetition
166+
--> $DIR/use_self.rs:491:13
167+
|
168+
LL | S2::new()
169+
| ^^ help: use the applicable keyword: `Self`
170+
171+
error: aborting due to 28 previous errors
166172

tests/ui/useless_conversion.fixed

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,23 @@ fn main() {
7070
let a: i32 = 1;
7171
let b: i32 = 1;
7272
let _ = (a + b) * 3;
73+
74+
// see #7205
75+
let s: Foo<'a'> = Foo;
76+
let _: Foo<'b'> = s.into();
77+
let s2: Foo<'a'> = Foo;
78+
let _: Foo<'a'> = s2;
79+
let s3: Foo<'a'> = Foo;
80+
let _ = s3;
81+
let s4: Foo<'a'> = Foo;
82+
let _ = vec![s4, s4, s4].into_iter();
83+
}
84+
85+
#[derive(Copy, Clone)]
86+
struct Foo<const C: char>;
87+
88+
impl From<Foo<'a'>> for Foo<'b'> {
89+
fn from(_s: Foo<'a'>) -> Self {
90+
Foo
91+
}
7392
}

tests/ui/useless_conversion.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,23 @@ fn main() {
7070
let a: i32 = 1;
7171
let b: i32 = 1;
7272
let _ = i32::from(a + b) * 3;
73+
74+
// see #7205
75+
let s: Foo<'a'> = Foo;
76+
let _: Foo<'b'> = s.into();
77+
let s2: Foo<'a'> = Foo;
78+
let _: Foo<'a'> = s2.into();
79+
let s3: Foo<'a'> = Foo;
80+
let _ = Foo::<'a'>::from(s3);
81+
let s4: Foo<'a'> = Foo;
82+
let _ = vec![s4, s4, s4].into_iter().into_iter();
83+
}
84+
85+
#[derive(Copy, Clone)]
86+
struct Foo<const C: char>;
87+
88+
impl From<Foo<'a'>> for Foo<'b'> {
89+
fn from(_s: Foo<'a'>) -> Self {
90+
Foo
91+
}
7392
}

tests/ui/useless_conversion.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,23 @@ error: useless conversion to the same type: `i32`
7070
LL | let _ = i32::from(a + b) * 3;
7171
| ^^^^^^^^^^^^^^^^ help: consider removing `i32::from()`: `(a + b)`
7272

73-
error: aborting due to 11 previous errors
73+
error: useless conversion to the same type: `Foo<'a'>`
74+
--> $DIR/useless_conversion.rs:78:23
75+
|
76+
LL | let _: Foo<'a'> = s2.into();
77+
| ^^^^^^^^^ help: consider removing `.into()`: `s2`
78+
79+
error: useless conversion to the same type: `Foo<'a'>`
80+
--> $DIR/useless_conversion.rs:80:13
81+
|
82+
LL | let _ = Foo::<'a'>::from(s3);
83+
| ^^^^^^^^^^^^^^^^^^^^ help: consider removing `Foo::<'a'>::from()`: `s3`
84+
85+
error: useless conversion to the same type: `std::vec::IntoIter<Foo<'a'>>`
86+
--> $DIR/useless_conversion.rs:82:13
87+
|
88+
LL | let _ = vec![s4, s4, s4].into_iter().into_iter();
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![s4, s4, s4].into_iter()`
90+
91+
error: aborting due to 14 previous errors
7492

0 commit comments

Comments
 (0)