Add diagnostics and suggestions for raw pointer arithmetic assignments
This commit is contained in:
parent
299877e280
commit
834e476a0c
4 changed files with 122 additions and 1 deletions
20
tests/ui/typeck/pointer-arith-assign.fixed
Normal file
20
tests/ui/typeck/pointer-arith-assign.fixed
Normal file
|
@ -0,0 +1,20 @@
|
|||
//@ run-rustfix
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_variables)]
|
||||
#![allow(unused_assignments)]
|
||||
|
||||
fn test_add_assign_raw_pointer() {
|
||||
let mut arr = [0u8; 10];
|
||||
let mut _ptr = arr.as_mut_ptr();
|
||||
|
||||
_ptr = _ptr.wrapping_add(2); //~ ERROR binary assignment operation `+=` cannot be applied to type `*mut u8` [E0368]
|
||||
}
|
||||
|
||||
fn test_sub_assign_raw_pointer() {
|
||||
let mut arr = [0u8; 10];
|
||||
let mut _ptr = arr.as_mut_ptr();
|
||||
|
||||
_ptr = _ptr.wrapping_sub(2); //~ ERROR binary assignment operation `-=` cannot be applied to type `*mut u8` [E0368]
|
||||
}
|
||||
|
||||
fn main() {}
|
20
tests/ui/typeck/pointer-arith-assign.rs
Normal file
20
tests/ui/typeck/pointer-arith-assign.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
//@ run-rustfix
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_variables)]
|
||||
#![allow(unused_assignments)]
|
||||
|
||||
fn test_add_assign_raw_pointer() {
|
||||
let mut arr = [0u8; 10];
|
||||
let mut _ptr = arr.as_mut_ptr();
|
||||
|
||||
_ptr += 2; //~ ERROR binary assignment operation `+=` cannot be applied to type `*mut u8` [E0368]
|
||||
}
|
||||
|
||||
fn test_sub_assign_raw_pointer() {
|
||||
let mut arr = [0u8; 10];
|
||||
let mut _ptr = arr.as_mut_ptr();
|
||||
|
||||
_ptr -= 2; //~ ERROR binary assignment operation `-=` cannot be applied to type `*mut u8` [E0368]
|
||||
}
|
||||
|
||||
fn main() {}
|
31
tests/ui/typeck/pointer-arith-assign.stderr
Normal file
31
tests/ui/typeck/pointer-arith-assign.stderr
Normal file
|
@ -0,0 +1,31 @@
|
|||
error[E0368]: binary assignment operation `+=` cannot be applied to type `*mut u8`
|
||||
--> $DIR/pointer-arith-assign.rs:10:5
|
||||
|
|
||||
LL | _ptr += 2;
|
||||
| ----^^^^^
|
||||
| |
|
||||
| cannot use `+=` on type `*mut u8`
|
||||
|
|
||||
help: consider using `add` or `wrapping_add` to do pointer arithmetic
|
||||
|
|
||||
LL - _ptr += 2;
|
||||
LL + _ptr = _ptr.wrapping_add(2);
|
||||
|
|
||||
|
||||
error[E0368]: binary assignment operation `-=` cannot be applied to type `*mut u8`
|
||||
--> $DIR/pointer-arith-assign.rs:17:5
|
||||
|
|
||||
LL | _ptr -= 2;
|
||||
| ----^^^^^
|
||||
| |
|
||||
| cannot use `-=` on type `*mut u8`
|
||||
|
|
||||
help: consider using `sub` or `wrapping_sub` to do pointer arithmetic
|
||||
|
|
||||
LL - _ptr -= 2;
|
||||
LL + _ptr = _ptr.wrapping_sub(2);
|
||||
|
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0368`.
|
Loading…
Add table
Add a link
Reference in a new issue