split into borrow_deref_ref.rs and borrow_deref_ref_unfixable.rs

This commit is contained in:
lengyijun 2022-06-01 15:32:26 +00:00
parent 8430fa2a82
commit 202fdb9c53
6 changed files with 92 additions and 22 deletions

View file

@ -346,6 +346,7 @@ fn compile_test() {
const RUSTFIX_COVERAGE_KNOWN_EXCEPTIONS: &[&str] = &[
"assign_ops2.rs",
"borrow_deref_ref_unfixable.rs",
"cast_size_32bit.rs",
"char_lit_as_u8.rs",
"cmp_owned/without_suggestion.rs",

View file

@ -0,0 +1,59 @@
// run-rustfix
#![allow(dead_code, unused_variables)]
fn main() {}
mod should_lint {
fn one_help() {
let a = &12;
let b = a;
let b = &mut bar(&12);
}
fn bar(x: &u32) -> &u32 {
x
}
}
// this mod explains why we should not lint `&mut &* (&T)`
mod should_not_lint1 {
fn foo(x: &mut &u32) {
*x = &1;
}
fn main() {
let mut x = &0;
foo(&mut &*x); // should not lint
assert_eq!(*x, 0);
foo(&mut x);
assert_eq!(*x, 1);
}
}
// similar to should_not_lint1
mod should_not_lint2 {
struct S<'a> {
a: &'a u32,
b: u32,
}
fn main() {
let s = S { a: &1, b: 1 };
let x = &mut &*s.a;
*x = &2;
}
}
// this mod explains why we should not lint `& &* (&T)`
mod false_negative {
fn foo() {
let x = &12;
let addr_x = &x as *const _ as usize;
let addr_y = &x as *const _ as usize; // assert ok
// let addr_y = &x as *const _ as usize; // assert fail
assert_ne!(addr_x, addr_y);
}
}

View file

@ -5,13 +5,10 @@
fn main() {}
mod should_lint {
fn foo() {
fn one_help() {
let a = &12;
let b = &*a;
let s = &String::new();
let x: &str = &*s;
let b = &mut &*bar(&12);
}

View file

@ -7,31 +7,16 @@ LL | let b = &*a;
= note: `-D clippy::borrow-deref-ref` implied by `-D warnings`
error: deref on an immutable reference
--> $DIR/borrow_deref_ref.rs:13:23
|
LL | let x: &str = &*s;
| ^^^
|
help: if you would like to reborrow, try removing `&*`
|
LL | let x: &str = s;
| ~
help: if you would like to deref, try using `&**`
|
LL | let x: &str = &**s;
| ~~~~
error: deref on an immutable reference
--> $DIR/borrow_deref_ref.rs:15:22
--> $DIR/borrow_deref_ref.rs:12:22
|
LL | let b = &mut &*bar(&12);
| ^^^^^^^^^^ help: if you would like to reborrow, try removing `&*`: `bar(&12)`
error: deref on an immutable reference
--> $DIR/borrow_deref_ref.rs:58:23
--> $DIR/borrow_deref_ref.rs:55:23
|
LL | let addr_y = &&*x as *const _ as usize; // assert ok
| ^^^ help: if you would like to reborrow, try removing `&*`: `x`
error: aborting due to 4 previous errors
error: aborting due to 3 previous errors

View file

@ -0,0 +1,10 @@
#![allow(dead_code, unused_variables)]
fn main() {}
mod should_lint {
fn two_helps() {
let s = &String::new();
let x: &str = &*s;
}
}

View file

@ -0,0 +1,18 @@
error: deref on an immutable reference
--> $DIR/borrow_deref_ref_unfixable.rs:8:23
|
LL | let x: &str = &*s;
| ^^^
|
= note: `-D clippy::borrow-deref-ref` implied by `-D warnings`
help: if you would like to reborrow, try removing `&*`
|
LL | let x: &str = s;
| ~
help: if you would like to deref, try using `&**`
|
LL | let x: &str = &**s;
| ~~~~
error: aborting due to previous error