Skip to content

Commit d568387

Browse files
committed
Auto merge of rust-lang#7396 - ranweiler:zero-offset, r=Manishearth
Fix invocation of `zst_offset` lint The `zst_offset` lint was broken by a refactoring regression in 2108387. In the invocation of the `zst_offset` check [here](rust-lang/rust-clippy@2108387#diff-7f73f6fe28c04b654223c09c42fe02937d2351fc58a91d21ab812aaf6f9b185dR1927), we shadow the already-destructured receiver `recv`, and accidentally pass the first argument of the method as if it were the receiver. This was not caught because the UI test expectation was never correct (a bad cast obscured the actual test result). This PR: - Fixes the existing test expectation - Tests both `const` and `mut` raw pointers - Passes the actual receiver to the lint's implementation Fixes rust-lang#7395. changelog: Fix [`zst_offset`] invocation and test
2 parents 417401f + 642239c commit d568387

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
20362036
fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Option<&RustcVersion>) {
20372037
if let Some((name, [recv, args @ ..], span)) = method_call!(expr) {
20382038
match (name, args) {
2039-
("add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub", [recv, _]) => {
2039+
("add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub", [_arg]) => {
20402040
zst_offset::check(cx, expr, recv);
20412041
},
20422042
("and_then", [arg]) => {

tests/ui/zero_offset.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
fn main() {
22
unsafe {
3-
let x = &() as *const ();
4-
x.offset(0);
5-
x.wrapping_add(0);
6-
x.sub(0);
7-
x.wrapping_sub(0);
3+
let m = &mut () as *mut ();
4+
m.offset(0);
5+
m.wrapping_add(0);
6+
m.sub(0);
7+
m.wrapping_sub(0);
88

9-
let y = &1 as *const u8;
10-
y.offset(0);
9+
let c = &() as *const ();
10+
c.offset(0);
11+
c.wrapping_add(0);
12+
c.sub(0);
13+
c.wrapping_sub(0);
14+
15+
let sized = &1 as *const i32;
16+
sized.offset(0);
1117
}
1218
}

tests/ui/zero_offset.stderr

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,52 @@
1-
error[E0606]: casting `&i32` as `*const u8` is invalid
2-
--> $DIR/zero_offset.rs:9:17
1+
error: offset calculation on zero-sized value
2+
--> $DIR/zero_offset.rs:4:9
33
|
4-
LL | let y = &1 as *const u8;
5-
| ^^^^^^^^^^^^^^^
4+
LL | m.offset(0);
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `#[deny(clippy::zst_offset)]` on by default
8+
9+
error: offset calculation on zero-sized value
10+
--> $DIR/zero_offset.rs:5:9
11+
|
12+
LL | m.wrapping_add(0);
13+
| ^^^^^^^^^^^^^^^^^
14+
15+
error: offset calculation on zero-sized value
16+
--> $DIR/zero_offset.rs:6:9
17+
|
18+
LL | m.sub(0);
19+
| ^^^^^^^^
20+
21+
error: offset calculation on zero-sized value
22+
--> $DIR/zero_offset.rs:7:9
23+
|
24+
LL | m.wrapping_sub(0);
25+
| ^^^^^^^^^^^^^^^^^
26+
27+
error: offset calculation on zero-sized value
28+
--> $DIR/zero_offset.rs:10:9
29+
|
30+
LL | c.offset(0);
31+
| ^^^^^^^^^^^
32+
33+
error: offset calculation on zero-sized value
34+
--> $DIR/zero_offset.rs:11:9
35+
|
36+
LL | c.wrapping_add(0);
37+
| ^^^^^^^^^^^^^^^^^
38+
39+
error: offset calculation on zero-sized value
40+
--> $DIR/zero_offset.rs:12:9
41+
|
42+
LL | c.sub(0);
43+
| ^^^^^^^^
44+
45+
error: offset calculation on zero-sized value
46+
--> $DIR/zero_offset.rs:13:9
47+
|
48+
LL | c.wrapping_sub(0);
49+
| ^^^^^^^^^^^^^^^^^
650

7-
error: aborting due to previous error
51+
error: aborting due to 8 previous errors
852

9-
For more information about this error, try `rustc --explain E0606`.

0 commit comments

Comments
 (0)