Skip to content

Post-#112261 pointer += integer diagnostics suggest replacing with unused value #137391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nabijaczleweli opened this issue Feb 21, 2025 · 1 comment · Fixed by #140138
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-raw-pointers Area: raw pointers, MaybeUninit, NonNull A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nabijaczleweli
Copy link
Contributor

nabijaczleweli commented Feb 21, 2025

Code

fn main() {
	let ga_z = [];
	let mut known_rgbs_groups = 0 as *mut u32;
	known_rgbs_groups += ga_z.len();
}

Current output

stable:

error[E0368]: binary assignment operation `+=` cannot be applied to type `*mut u32`
 --> test.rs:4:2
  |
4 |     known_rgbs_groups += ga_z.len();
  |     -----------------^^^^^^^^^^^^^^
  |     |
  |     cannot use `+=` on type `*mut u32`
  |
help: consider using `wrapping_add` or `add` for pointer + {integer}
  |
4 |     known_rgbs_groups.wrapping_add(ga_z.len());
  |                      ~~~~~~~~~~~~~~          +

error: aborting due to 1 previous error

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

nightly:

error[E0368]: binary assignment operation `+=` cannot be applied to type `*mut u32`
 --> test.rs:4:2
  |
4 |     known_rgbs_groups += ga_z.len();
  |     -----------------^^^^^^^^^^^^^^
  |     |
  |     cannot use `+=` on type `*mut u32`
  |
help: consider using `wrapping_add` or `add` for pointer + {integer}
  |
4 -     known_rgbs_groups += ga_z.len();
4 +     known_rgbs_groups.wrapping_add(ga_z.len());
  |

error: aborting due to 1 previous error

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

Desired output

known_rgbs_groups = known_rgbs_groups.add(ga_z.len());

Rationale and extra context

known_rgbs_groups.add(ga_z.len()); doesn't do anything 💀

Rust Version

rustc 1.84.1 (e71f9a9a9 2025-01-27)
binary: rustc
commit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58
commit-date: 2025-01-27
host: x86_64-pc-windows-gnu
release: 1.84.1
LLVM version: 19.1.5
rustc 1.87.0-nightly (827a0d638 2025-02-18)
binary: rustc
commit-hash: 827a0d638dabc9a22c56f9c37a557568f86ac76c
commit-date: 2025-02-18
host: x86_64-pc-windows-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Anything else?

This is #112261, and in 20 comments and 4 review rounds no-one tried += lol

@nabijaczleweli nabijaczleweli added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 21, 2025
@fmease fmease added A-raw-pointers Area: raw pointers, MaybeUninit, NonNull D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Feb 21, 2025
@Kivooeo
Copy link
Contributor

Kivooeo commented Apr 20, 2025

Will look into it, thanks

bors added a commit to rust-lang-ci/rust that referenced this issue Apr 22, 2025
…enton

Rollup of 5 pull requests

Successful merges:

 - rust-lang#139981 (Don't compute name of associated item if it's an RPITIT)
 - rust-lang#140077 (Construct OutputType using macro and print [=FILENAME] help info)
 - rust-lang#140081 (Update `libc` to 0.2.172)
 - rust-lang#140094 (Improve diagnostics for pointer arithmetic += and -= (fixes rust-lang#137391))
 - rust-lang#140128 (Use correct annotation for CSS pseudo elements)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 8f42ac0 Apr 22, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 22, 2025
Rollup merge of rust-lang#140094 - Kivooeo:raw-pointer-assignment-suggestion, r=compiler-errors

Improve diagnostics for pointer arithmetic += and -= (fixes rust-lang#137391)

**Description**:

This PR improves the diagnostic message for cases where a binary assignment operation like `ptr += offset` or `ptr -= offset` is attempted on `*mut T`. These operations are not allowed, and the compiler previously suggested calling `.add()` or `.wrapping_add()`, which is misleading if not assigned.

This PR updates the diagnostics to suggest assigning the result of `.wrapping_add()` or `.wrapping_sub()` back to the pointer, e.g.:

**Examples**

For this code
```rust
let mut arr = [0u8; 10];
let mut ptr = arr.as_mut_ptr();

ptr += 2;
```
it will say:
```rust
10 |     ptr += 2;
   |     ---^^^^^
   |     |
   |     cannot use `+=` on type `*mut u8`
   |
help: consider replacing `ptr += offset` with `ptr = ptr.wrapping_add(offset)` or `ptr.add(offset)`
   |
10 -     ptr += 2;
10 +     ptr = ptr.wrapping_add(2);
```

**Related issue**: rust-lang#137391
cc `@nabijaczleweli` for context (issue author)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-raw-pointers Area: raw pointers, MaybeUninit, NonNull A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants