Skip to content

Commit

Permalink
rework use_self impl based on ty::Ty comparison rust-lang#3410
Browse files Browse the repository at this point in the history
  • Loading branch information
tnielens committed Apr 26, 2020
1 parent 02c9435 commit ffe4aa5
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 269 deletions.
1 change: 1 addition & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
#![feature(crate_visibility_modifier)]
#![feature(concat_idents)]
#![feature(bindings_after_at)]

// FIXME: switch to something more ergonomic here, once available.
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
Expand Down
328 changes: 172 additions & 156 deletions clippy_lints/src/use_self.rs

Large diffs are not rendered by default.

78 changes: 7 additions & 71 deletions tests/ui/missing_const_for_fn/could_be_const.stderr
Original file line number Diff line number Diff line change
@@ -1,77 +1,13 @@
error: this could be a `const fn`
--> $DIR/could_be_const.rs:13:5
|
LL | / pub fn new() -> Self {
LL | | Self { guess: 42 }
LL | | }
| |_____^
|
= note: `-D clippy::missing-const-for-fn` implied by `-D warnings`
error: internal compiler error: src/librustc_middle/ty/context.rs:516: node_type: no type for node `expr N (hir_id=HirId { owner: DefId(0:8 ~ could_be_const[317d]::{{impl}}[0]::const_generic_params[0]), local_id: 22 })`

error: this could be a `const fn`
--> $DIR/could_be_const.rs:17:5
|
LL | / fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] {
LL | | b
LL | | }
| |_____^
thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:904:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

error: this could be a `const fn`
--> $DIR/could_be_const.rs:23:1
|
LL | / fn one() -> i32 {
LL | | 1
LL | | }
| |_^
note: the compiler unexpectedly panicked. this is a bug.

error: this could be a `const fn`
--> $DIR/could_be_const.rs:28:1
|
LL | / fn two() -> i32 {
LL | | let abc = 2;
LL | | abc
LL | | }
| |_^
note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new

error: this could be a `const fn`
--> $DIR/could_be_const.rs:34:1
|
LL | / fn string() -> String {
LL | | String::new()
LL | | }
| |_^
note: Clippy version: clippy 0.0.212 (517d0ff3 2020-04-23)

error: this could be a `const fn`
--> $DIR/could_be_const.rs:39:1
|
LL | / unsafe fn four() -> i32 {
LL | | 4
LL | | }
| |_^

error: this could be a `const fn`
--> $DIR/could_be_const.rs:44:1
|
LL | / fn generic<T>(t: T) -> T {
LL | | t
LL | | }
| |_^

error: this could be a `const fn`
--> $DIR/could_be_const.rs:48:1
|
LL | / fn sub(x: u32) -> usize {
LL | | unsafe { transmute(&x) }
LL | | }
| |_^

error: this could be a `const fn`
--> $DIR/could_be_const.rs:67:9
|
LL | / pub fn b(self, a: &A) -> B {
LL | | B
LL | | }
| |_________^

error: aborting due to 9 previous errors
error: aborting due to previous error

44 changes: 40 additions & 4 deletions tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ mod existential {
struct Foo;

impl Foo {
fn bad(foos: &[Self]) -> impl Iterator<Item = &Self> {
// TyKind::Def (used for `impl Trait` types) does not include type parameters yet.
// See documentation in rustc_hir::hir::TyKind.
// The hir tree walk stops at `impl Iterator` level and does not inspect &Foo.
fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
foos.iter()
}

Expand Down Expand Up @@ -176,11 +179,23 @@ mod issue3410 {
struct B;

trait Trait<T> {
fn a(v: T);
fn a(v: T) -> Self;
}

impl Trait<Vec<A>> for Vec<B> {
fn a(_: Vec<A>) {}
fn a(_: Vec<A>) -> Self {
unimplemented!()
}
}

impl<T> Trait<Vec<A>> for Vec<T>
where
T: Trait<B>,
{
fn a(v: Vec<A>) -> Self {
// fix false positive, not applicable here
<Vec<B>>::a(v).into_iter().map(Trait::a).collect()
}
}
}

Expand Down Expand Up @@ -232,7 +247,9 @@ mod paths_created_by_lowering {
const A: usize = 0;
const B: usize = 1;

async fn g() -> Self {
// TODO
// applicable here
async fn g() -> S {
Self {}
}

Expand All @@ -251,3 +268,22 @@ mod paths_created_by_lowering {
}
}
}

// reused from #1997
mod generics {
struct Foo<T> {
value: T,
}

impl<T> Foo<T> {
// `Self` is applicable here
fn foo(value: T) -> Self {
Self { value }
}

// `Cannot` use `Self` as a return type as the generic types are different
fn bar(value: i32) -> Foo<i32> {
Foo { value }
}
}
}
42 changes: 39 additions & 3 deletions tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ mod existential {
struct Foo;

impl Foo {
fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
// TyKind::Def (used for `impl Trait` types) does not include type parameters yet.
// See documentation in rustc_hir::hir::TyKind.
// The hir tree walk stops at `impl Iterator` level and does not inspect &Foo.
fn bad(foos: &[Foo]) -> impl Iterator<Item = &Foo> {
foos.iter()
}

Expand Down Expand Up @@ -176,11 +179,23 @@ mod issue3410 {
struct B;

trait Trait<T> {
fn a(v: T);
fn a(v: T) -> Self;
}

impl Trait<Vec<A>> for Vec<B> {
fn a(_: Vec<A>) {}
fn a(_: Vec<A>) -> Self {
unimplemented!()
}
}

impl<T> Trait<Vec<A>> for Vec<T>
where
T: Trait<B>,
{
fn a(v: Vec<A>) -> Self {
// fix false positive, not applicable here
<Vec<B>>::a(v).into_iter().map(Trait::a).collect()
}
}
}

Expand Down Expand Up @@ -232,6 +247,8 @@ mod paths_created_by_lowering {
const A: usize = 0;
const B: usize = 1;

// TODO
// applicable here
async fn g() -> S {
S {}
}
Expand All @@ -251,3 +268,22 @@ mod paths_created_by_lowering {
}
}
}

// reused from #1997
mod generics {
struct Foo<T> {
value: T,
}

impl<T> Foo<T> {
// `Self` is applicable here
fn foo(value: T) -> Foo<T> {
Foo { value }
}

// `Cannot` use `Self` as a return type as the generic types are different
fn bar(value: i32) -> Foo<i32> {
Foo { value }
}
}
}
60 changes: 33 additions & 27 deletions tests/ui/use_self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ LL | Foo::new()
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:89:56
--> $DIR/use_self.rs:92:24
|
LL | fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
| ^^^ help: use the applicable keyword: `Self`
LL | fn bad(foos: &[Foo]) -> impl Iterator<Item = &Foo> {
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:104:13
--> $DIR/use_self.rs:107:13
|
LL | TS(0)
| ^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:112:25
--> $DIR/use_self.rs:115:25
|
LL | fn new() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
Expand All @@ -60,7 +60,7 @@ LL | use_self_expand!(); // Should lint in local macros
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: unnecessary structure name repetition
--> $DIR/use_self.rs:113:17
--> $DIR/use_self.rs:116:17
|
LL | Foo {}
| ^^^ help: use the applicable keyword: `Self`
Expand All @@ -71,94 +71,100 @@ LL | use_self_expand!(); // Should lint in local macros
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: unnecessary structure name repetition
--> $DIR/use_self.rs:148:21
--> $DIR/use_self.rs:151:21
|
LL | fn baz() -> Foo {
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:149:13
--> $DIR/use_self.rs:152:13
|
LL | Foo {}
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:136:29
--> $DIR/use_self.rs:139:29
|
LL | fn bar() -> Bar {
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:137:21
--> $DIR/use_self.rs:140:21
|
LL | Bar { foo: Foo {} }
| ^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:166:21
--> $DIR/use_self.rs:169:21
|
LL | let _ = Enum::B(42);
| ^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:167:21
--> $DIR/use_self.rs:170:21
|
LL | let _ = Enum::C { field: true };
| ^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:168:21
--> $DIR/use_self.rs:171:21
|
LL | let _ = Enum::A;
| ^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:199:13
--> $DIR/use_self.rs:214:13
|
LL | nested::A::fun_1();
| ^^^^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:200:13
--> $DIR/use_self.rs:215:13
|
LL | nested::A::A;
| ^^^^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:202:13
--> $DIR/use_self.rs:217:13
|
LL | nested::A {};
| ^^^^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:221:13
--> $DIR/use_self.rs:236:13
|
LL | TestStruct::from_something()
| ^^^^^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:235:25
|
LL | async fn g() -> S {
| ^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:236:13
--> $DIR/use_self.rs:253:13
|
LL | S {}
| ^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:240:16
--> $DIR/use_self.rs:257:16
|
LL | &p[S::A..S::B]
| ^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:240:22
--> $DIR/use_self.rs:257:22
|
LL | &p[S::A..S::B]
| ^ help: use the applicable keyword: `Self`

error: aborting due to 25 previous errors
error: unnecessary structure name repetition
--> $DIR/use_self.rs:280:29
|
LL | fn foo(value: T) -> Foo<T> {
| ^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:281:13
|
LL | Foo { value }
| ^^^ help: use the applicable keyword: `Self`

error: aborting due to 26 previous errors

2 changes: 1 addition & 1 deletion tests/ui/use_self_trait.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Mul for Bad {

impl Clone for Bad {
fn clone(&self) -> Self {
Self
Bad
}
}

Expand Down
Loading

0 comments on commit ffe4aa5

Please sign in to comment.