From d2087ad00d5443f906a3603a1ba2718e87458239 Mon Sep 17 00:00:00 2001 From: Joe Ranweiler Date: Tue, 22 Jun 2021 18:44:04 -0700 Subject: [PATCH 1/3] Remove bad cast in test, cover more cases --- tests/ui/zero_offset.rs | 18 ++++++++---- tests/ui/zero_offset.stderr | 55 +++++++++++++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/tests/ui/zero_offset.rs b/tests/ui/zero_offset.rs index 2de904376ad4..d3f8c21937e9 100644 --- a/tests/ui/zero_offset.rs +++ b/tests/ui/zero_offset.rs @@ -1,12 +1,18 @@ fn main() { unsafe { - let x = &() as *const (); - x.offset(0); - x.wrapping_add(0); - x.sub(0); - x.wrapping_sub(0); + let m = &mut () as *mut (); + m.offset(0); + m.wrapping_add(0); + m.sub(0); + m.wrapping_sub(0); - let y = &1 as *const u8; + let c = &() as *const (); + c.offset(0); + c.wrapping_add(0); + c.sub(0); + c.wrapping_sub(0); + + let y = &1 as *const i32; y.offset(0); } } diff --git a/tests/ui/zero_offset.stderr b/tests/ui/zero_offset.stderr index cfcd7de2b3d2..b12c8e9a73c6 100644 --- a/tests/ui/zero_offset.stderr +++ b/tests/ui/zero_offset.stderr @@ -1,9 +1,52 @@ -error[E0606]: casting `&i32` as `*const u8` is invalid - --> $DIR/zero_offset.rs:9:17 +error: offset calculation on zero-sized value + --> $DIR/zero_offset.rs:4:9 | -LL | let y = &1 as *const u8; - | ^^^^^^^^^^^^^^^ +LL | m.offset(0); + | ^^^^^^^^^^^ + | + = note: `#[deny(clippy::zst_offset)]` on by default + +error: offset calculation on zero-sized value + --> $DIR/zero_offset.rs:5:9 + | +LL | m.wrapping_add(0); + | ^^^^^^^^^^^^^^^^^ + +error: offset calculation on zero-sized value + --> $DIR/zero_offset.rs:6:9 + | +LL | m.sub(0); + | ^^^^^^^^ + +error: offset calculation on zero-sized value + --> $DIR/zero_offset.rs:7:9 + | +LL | m.wrapping_sub(0); + | ^^^^^^^^^^^^^^^^^ + +error: offset calculation on zero-sized value + --> $DIR/zero_offset.rs:10:9 + | +LL | c.offset(0); + | ^^^^^^^^^^^ + +error: offset calculation on zero-sized value + --> $DIR/zero_offset.rs:11:9 + | +LL | c.wrapping_add(0); + | ^^^^^^^^^^^^^^^^^ + +error: offset calculation on zero-sized value + --> $DIR/zero_offset.rs:12:9 + | +LL | c.sub(0); + | ^^^^^^^^ + +error: offset calculation on zero-sized value + --> $DIR/zero_offset.rs:13:9 + | +LL | c.wrapping_sub(0); + | ^^^^^^^^^^^^^^^^^ -error: aborting due to previous error +error: aborting due to 8 previous errors -For more information about this error, try `rustc --explain E0606`. From 10122e4a48c3a31276f8e117af66c949c8f962a3 Mon Sep 17 00:00:00 2001 From: Joe Ranweiler Date: Tue, 22 Jun 2021 18:45:12 -0700 Subject: [PATCH 2/3] Remove shadowed receiver in check invocation --- clippy_lints/src/methods/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index c6bef7bc5d5e..21585543b0ac 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -2036,7 +2036,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Option<&RustcVersion>) { if let Some((name, [recv, args @ ..], span)) = method_call!(expr) { match (name, args) { - ("add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub", [recv, _]) => { + ("add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub", [_arg]) => { zst_offset::check(cx, expr, recv); }, ("and_then", [arg]) => { From 642239c857124b109f43f7223baddbe8694c84ee Mon Sep 17 00:00:00 2001 From: Joe Ranweiler Date: Tue, 22 Jun 2021 19:02:34 -0700 Subject: [PATCH 3/3] Update var name in test --- tests/ui/zero_offset.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ui/zero_offset.rs b/tests/ui/zero_offset.rs index d3f8c21937e9..6c190a4c86c4 100644 --- a/tests/ui/zero_offset.rs +++ b/tests/ui/zero_offset.rs @@ -12,7 +12,7 @@ fn main() { c.sub(0); c.wrapping_sub(0); - let y = &1 as *const i32; - y.offset(0); + let sized = &1 as *const i32; + sized.offset(0); } }