diff --git a/tests/ui/mem_discriminant.fixed b/tests/ui/mem_discriminant.fixed new file mode 100644 index 000000000..69a8f286d --- /dev/null +++ b/tests/ui/mem_discriminant.fixed @@ -0,0 +1,45 @@ +// run-rustfix + +#![deny(clippy::mem_discriminant_non_enum)] + +use std::mem; + +enum Foo { + One(usize), + Two(u8), +} + +fn main() { + // bad + mem::discriminant(&Some(2)); + mem::discriminant(&None::); + mem::discriminant(&Foo::One(5)); + mem::discriminant(&Foo::Two(5)); + + let ro = &Some(3); + let rro = &ro; + mem::discriminant(ro); + mem::discriminant(*rro); + mem::discriminant(*rro); + + macro_rules! mem_discriminant_but_in_a_macro { + ($param:expr) => { + mem::discriminant($param) + }; + } + + mem_discriminant_but_in_a_macro!(*rro); + + let rrrrro = &&&rro; + mem::discriminant(****rrrrro); + mem::discriminant(****rrrrro); + + // ok + mem::discriminant(&Some(2)); + mem::discriminant(&None::); + mem::discriminant(&Foo::One(5)); + mem::discriminant(&Foo::Two(5)); + mem::discriminant(ro); + mem::discriminant(*rro); + mem::discriminant(****rrrrro); +} diff --git a/tests/ui/mem_discriminant.rs b/tests/ui/mem_discriminant.rs index 81f162886..55db50fcd 100644 --- a/tests/ui/mem_discriminant.rs +++ b/tests/ui/mem_discriminant.rs @@ -1,3 +1,5 @@ +// run-rustfix + #![deny(clippy::mem_discriminant_non_enum)] use std::mem; @@ -7,16 +9,12 @@ enum Foo { Two(u8), } -struct A(Foo); - fn main() { // bad - mem::discriminant(&"hello"); mem::discriminant(&&Some(2)); mem::discriminant(&&None::); mem::discriminant(&&Foo::One(5)); mem::discriminant(&&Foo::Two(5)); - mem::discriminant(&A(Foo::One(0))); let ro = &Some(3); let rro = &ro; diff --git a/tests/ui/mem_discriminant.stderr b/tests/ui/mem_discriminant.stderr index 295545406..0420c82ab 100644 --- a/tests/ui/mem_discriminant.stderr +++ b/tests/ui/mem_discriminant.stderr @@ -1,25 +1,19 @@ -error: calling `mem::discriminant` on non-enum type `&str` - --> $DIR/mem_discriminant.rs:14:5 - | -LL | mem::discriminant(&"hello"); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -note: lint level defined here - --> $DIR/mem_discriminant.rs:1:9 - | -LL | #![deny(clippy::mem_discriminant_non_enum)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - error: calling `mem::discriminant` on non-enum type `&std::option::Option` - --> $DIR/mem_discriminant.rs:15:5 + --> $DIR/mem_discriminant.rs:14:5 | LL | mem::discriminant(&&Some(2)); | ^^^^^^^^^^^^^^^^^^---------^ | | | help: try dereferencing: `&Some(2)` + | +note: lint level defined here + --> $DIR/mem_discriminant.rs:3:9 + | +LL | #![deny(clippy::mem_discriminant_non_enum)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: calling `mem::discriminant` on non-enum type `&std::option::Option` - --> $DIR/mem_discriminant.rs:16:5 + --> $DIR/mem_discriminant.rs:15:5 | LL | mem::discriminant(&&None::); | ^^^^^^^^^^^^^^^^^^------------^ @@ -27,7 +21,7 @@ LL | mem::discriminant(&&None::); | help: try dereferencing: `&None::` error: calling `mem::discriminant` on non-enum type `&Foo` - --> $DIR/mem_discriminant.rs:17:5 + --> $DIR/mem_discriminant.rs:16:5 | LL | mem::discriminant(&&Foo::One(5)); | ^^^^^^^^^^^^^^^^^^-------------^ @@ -35,21 +29,15 @@ LL | mem::discriminant(&&Foo::One(5)); | help: try dereferencing: `&Foo::One(5)` error: calling `mem::discriminant` on non-enum type `&Foo` - --> $DIR/mem_discriminant.rs:18:5 + --> $DIR/mem_discriminant.rs:17:5 | LL | mem::discriminant(&&Foo::Two(5)); | ^^^^^^^^^^^^^^^^^^-------------^ | | | help: try dereferencing: `&Foo::Two(5)` -error: calling `mem::discriminant` on non-enum type `A` - --> $DIR/mem_discriminant.rs:19:5 - | -LL | mem::discriminant(&A(Foo::One(0))); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - error: calling `mem::discriminant` on non-enum type `&std::option::Option` - --> $DIR/mem_discriminant.rs:23:5 + --> $DIR/mem_discriminant.rs:21:5 | LL | mem::discriminant(&ro); | ^^^^^^^^^^^^^^^^^^---^ @@ -57,7 +45,7 @@ LL | mem::discriminant(&ro); | help: try dereferencing: `ro` error: calling `mem::discriminant` on non-enum type `&std::option::Option` - --> $DIR/mem_discriminant.rs:24:5 + --> $DIR/mem_discriminant.rs:22:5 | LL | mem::discriminant(rro); | ^^^^^^^^^^^^^^^^^^---^ @@ -65,7 +53,7 @@ LL | mem::discriminant(rro); | help: try dereferencing: `*rro` error: calling `mem::discriminant` on non-enum type `&&std::option::Option` - --> $DIR/mem_discriminant.rs:25:5 + --> $DIR/mem_discriminant.rs:23:5 | LL | mem::discriminant(&rro); | ^^^^^^^^^^^^^^^^^^----^ @@ -73,7 +61,7 @@ LL | mem::discriminant(&rro); | help: try dereferencing: `*rro` error: calling `mem::discriminant` on non-enum type `&&std::option::Option` - --> $DIR/mem_discriminant.rs:29:13 + --> $DIR/mem_discriminant.rs:27:13 | LL | mem::discriminant($param) | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -85,7 +73,7 @@ LL | mem_discriminant_but_in_a_macro!(&rro); | in this macro invocation error: calling `mem::discriminant` on non-enum type `&&&&&std::option::Option` - --> $DIR/mem_discriminant.rs:36:5 + --> $DIR/mem_discriminant.rs:34:5 | LL | mem::discriminant(&rrrrro); | ^^^^^^^^^^^^^^^^^^-------^ @@ -93,12 +81,12 @@ LL | mem::discriminant(&rrrrro); | help: try dereferencing: `****rrrrro` error: calling `mem::discriminant` on non-enum type `&&&std::option::Option` - --> $DIR/mem_discriminant.rs:37:5 + --> $DIR/mem_discriminant.rs:35:5 | LL | mem::discriminant(*rrrrro); | ^^^^^^^^^^^^^^^^^^-------^ | | | help: try dereferencing: `****rrrrro` -error: aborting due to 12 previous errors +error: aborting due to 10 previous errors diff --git a/tests/ui/mem_discriminant_unfixable.rs b/tests/ui/mem_discriminant_unfixable.rs new file mode 100644 index 000000000..e245d3257 --- /dev/null +++ b/tests/ui/mem_discriminant_unfixable.rs @@ -0,0 +1,16 @@ +#![deny(clippy::mem_discriminant_non_enum)] + +use std::mem; + +enum Foo { + One(usize), + Two(u8), +} + +struct A(Foo); + +fn main() { + // bad + mem::discriminant(&"hello"); + mem::discriminant(&A(Foo::One(0))); +} diff --git a/tests/ui/mem_discriminant_unfixable.stderr b/tests/ui/mem_discriminant_unfixable.stderr new file mode 100644 index 000000000..f12a9b2d9 --- /dev/null +++ b/tests/ui/mem_discriminant_unfixable.stderr @@ -0,0 +1,20 @@ +error: calling `mem::discriminant` on non-enum type `&str` + --> $DIR/mem_discriminant_unfixable.rs:14:5 + | +LL | mem::discriminant(&"hello"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: lint level defined here + --> $DIR/mem_discriminant_unfixable.rs:1:9 + | +LL | #![deny(clippy::mem_discriminant_non_enum)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: calling `mem::discriminant` on non-enum type `A` + --> $DIR/mem_discriminant_unfixable.rs:15:5 + | +LL | mem::discriminant(&A(Foo::One(0))); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors +