mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 11:48:30 +00:00
![]() Improve diagnostics for pointer arithmetic += and -= (fixes #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**: #137391 cc `@nabijaczleweli` for context (issue author) |
||
---|---|---|
.. | ||
assembly | ||
auxiliary | ||
codegen | ||
codegen-units | ||
coverage | ||
coverage-run-rustdoc | ||
crashes | ||
debuginfo | ||
incremental | ||
mir-opt | ||
pretty | ||
run-make | ||
rustdoc | ||
rustdoc-gui | ||
rustdoc-js | ||
rustdoc-js-std | ||
rustdoc-json | ||
rustdoc-ui | ||
ui | ||
ui-fulldeps | ||
COMPILER_TESTS.md |