Skip to content

Commit

Permalink
Auto merge of rust-lang#3886 - phansch:rustfix_useless_asref, r=flip1995
Browse files Browse the repository at this point in the history
Enable rustfix for `useless_asref` lint tests

cc rust-lang#3630
  • Loading branch information
bors committed Mar 15, 2019
2 parents e780641 + 0019ca5 commit 54e2051
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 12 deletions.
136 changes: 136 additions & 0 deletions tests/ui/useless_asref.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// run-rustfix

#![deny(clippy::useless_asref)]
#![allow(clippy::trivially_copy_pass_by_ref)]

use std::fmt::Debug;

struct FakeAsRef;

#[allow(clippy::should_implement_trait)]
impl FakeAsRef {
fn as_ref(&self) -> &Self {
self
}
}

struct MoreRef;

impl<'a, 'b, 'c> AsRef<&'a &'b &'c MoreRef> for MoreRef {
fn as_ref(&self) -> &&'a &'b &'c MoreRef {
&&&&MoreRef
}
}

fn foo_rstr(x: &str) {
println!("{:?}", x);
}
fn foo_rslice(x: &[i32]) {
println!("{:?}", x);
}
fn foo_mrslice(x: &mut [i32]) {
println!("{:?}", x);
}
fn foo_rrrrmr(_: &&&&MoreRef) {
println!("so many refs");
}

fn not_ok() {
let rstr: &str = "hello";
let mut mrslice: &mut [i32] = &mut [1, 2, 3];

{
let rslice: &[i32] = &*mrslice;
foo_rstr(rstr);
foo_rstr(rstr);
foo_rslice(rslice);
foo_rslice(rslice);
}
{
foo_mrslice(mrslice);
foo_mrslice(mrslice);
foo_rslice(mrslice);
foo_rslice(mrslice);
}

{
let rrrrrstr = &&&&rstr;
let rrrrrslice = &&&&&*mrslice;
foo_rslice(rrrrrslice);
foo_rslice(rrrrrslice);
foo_rstr(rrrrrstr);
foo_rstr(rrrrrstr);
}
{
let mrrrrrslice = &mut &mut &mut &mut mrslice;
foo_mrslice(mrrrrrslice);
foo_mrslice(mrrrrrslice);
foo_rslice(mrrrrrslice);
foo_rslice(mrrrrrslice);
}
#[allow(unused_parens, clippy::double_parens)]
foo_rrrrmr((&&&&MoreRef));

generic_not_ok(mrslice);
generic_ok(mrslice);
}

fn ok() {
let string = "hello".to_owned();
let mut arr = [1, 2, 3];
let mut vec = vec![1, 2, 3];

{
foo_rstr(string.as_ref());
foo_rslice(arr.as_ref());
foo_rslice(vec.as_ref());
}
{
foo_mrslice(arr.as_mut());
foo_mrslice(vec.as_mut());
}

{
let rrrrstring = &&&&string;
let rrrrarr = &&&&arr;
let rrrrvec = &&&&vec;
foo_rstr(rrrrstring.as_ref());
foo_rslice(rrrrarr.as_ref());
foo_rslice(rrrrvec.as_ref());
}
{
let mrrrrarr = &mut &mut &mut &mut arr;
let mrrrrvec = &mut &mut &mut &mut vec;
foo_mrslice(mrrrrarr.as_mut());
foo_mrslice(mrrrrvec.as_mut());
}
FakeAsRef.as_ref();
foo_rrrrmr(MoreRef.as_ref());

generic_not_ok(arr.as_mut());
generic_ok(&mut arr);
}

fn foo_mrt<T: Debug + ?Sized>(t: &mut T) {
println!("{:?}", t);
}
fn foo_rt<T: Debug + ?Sized>(t: &T) {
println!("{:?}", t);
}

fn generic_not_ok<T: AsMut<T> + AsRef<T> + Debug + ?Sized>(mrt: &mut T) {
foo_mrt(mrt);
foo_mrt(mrt);
foo_rt(mrt);
foo_rt(mrt);
}

fn generic_ok<U: AsMut<T> + AsRef<T> + ?Sized, T: Debug + ?Sized>(mru: &mut U) {
foo_mrt(mru.as_mut());
foo_rt(mru.as_ref());
}

fn main() {
not_ok();
ok();
}
4 changes: 4 additions & 0 deletions tests/ui/useless_asref.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// run-rustfix

#![deny(clippy::useless_asref)]
#![allow(clippy::trivially_copy_pass_by_ref)]

use std::fmt::Debug;

struct FakeAsRef;
Expand Down Expand Up @@ -65,6 +68,7 @@ fn not_ok() {
foo_rslice(mrrrrrslice.as_ref());
foo_rslice(mrrrrrslice);
}
#[allow(unused_parens, clippy::double_parens)]
foo_rrrrmr((&&&&MoreRef).as_ref());

generic_not_ok(mrslice);
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/useless_asref.stderr
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
error: this call to `as_ref` does nothing
--> $DIR/useless_asref.rs:41:18
--> $DIR/useless_asref.rs:44:18
|
LL | foo_rstr(rstr.as_ref());
| ^^^^^^^^^^^^^ help: try this: `rstr`
|
note: lint level defined here
--> $DIR/useless_asref.rs:1:9
--> $DIR/useless_asref.rs:3:9
|
LL | #![deny(clippy::useless_asref)]
| ^^^^^^^^^^^^^^^^^^^^^

error: this call to `as_ref` does nothing
--> $DIR/useless_asref.rs:43:20
--> $DIR/useless_asref.rs:46:20
|
LL | foo_rslice(rslice.as_ref());
| ^^^^^^^^^^^^^^^ help: try this: `rslice`

error: this call to `as_mut` does nothing
--> $DIR/useless_asref.rs:47:21
--> $DIR/useless_asref.rs:50:21
|
LL | foo_mrslice(mrslice.as_mut());
| ^^^^^^^^^^^^^^^^ help: try this: `mrslice`

error: this call to `as_ref` does nothing
--> $DIR/useless_asref.rs:49:20
--> $DIR/useless_asref.rs:52:20
|
LL | foo_rslice(mrslice.as_ref());
| ^^^^^^^^^^^^^^^^ help: try this: `mrslice`

error: this call to `as_ref` does nothing
--> $DIR/useless_asref.rs:56:20
--> $DIR/useless_asref.rs:59:20
|
LL | foo_rslice(rrrrrslice.as_ref());
| ^^^^^^^^^^^^^^^^^^^ help: try this: `rrrrrslice`

error: this call to `as_ref` does nothing
--> $DIR/useless_asref.rs:58:18
--> $DIR/useless_asref.rs:61:18
|
LL | foo_rstr(rrrrrstr.as_ref());
| ^^^^^^^^^^^^^^^^^ help: try this: `rrrrrstr`

error: this call to `as_mut` does nothing
--> $DIR/useless_asref.rs:63:21
--> $DIR/useless_asref.rs:66:21
|
LL | foo_mrslice(mrrrrrslice.as_mut());
| ^^^^^^^^^^^^^^^^^^^^ help: try this: `mrrrrrslice`

error: this call to `as_ref` does nothing
--> $DIR/useless_asref.rs:65:20
--> $DIR/useless_asref.rs:68:20
|
LL | foo_rslice(mrrrrrslice.as_ref());
| ^^^^^^^^^^^^^^^^^^^^ help: try this: `mrrrrrslice`

error: this call to `as_ref` does nothing
--> $DIR/useless_asref.rs:68:16
--> $DIR/useless_asref.rs:72:16
|
LL | foo_rrrrmr((&&&&MoreRef).as_ref());
| ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&&&&MoreRef)`

error: this call to `as_mut` does nothing
--> $DIR/useless_asref.rs:118:13
--> $DIR/useless_asref.rs:122:13
|
LL | foo_mrt(mrt.as_mut());
| ^^^^^^^^^^^^ help: try this: `mrt`

error: this call to `as_ref` does nothing
--> $DIR/useless_asref.rs:120:12
--> $DIR/useless_asref.rs:124:12
|
LL | foo_rt(mrt.as_ref());
| ^^^^^^^^^^^^ help: try this: `mrt`
Expand Down

0 comments on commit 54e2051

Please sign in to comment.