include a ref if argument is not just a numeric literal

This commit is contained in:
y21 2023-06-17 20:48:56 +02:00
parent 3217f8aeaa
commit 6331c943ff
4 changed files with 84 additions and 9 deletions

View file

@ -69,4 +69,17 @@ fn main() {
let _ = some_vec[0..1].to_vec();
let _ = some_vec[0..1].to_vec();
}
issue9909();
}
fn issue9909() {
let f = &[1, 2, 3];
let _x: &i32 = &f[1 + 2];
// ^ include a borrow in the suggestion, even if the argument is not just a numeric literal
let _x = f[1 + 2].to_string();
// ^ don't include a borrow here
let _x = f[1 + 2].abs();
// ^ don't include a borrow here
}

View file

@ -69,4 +69,17 @@ fn main() {
let _ = some_vec.get(0..1).unwrap().to_vec();
let _ = some_vec.get_mut(0..1).unwrap().to_vec();
}
issue9909();
}
fn issue9909() {
let f = &[1, 2, 3];
let _x: &i32 = f.get(1 + 2).unwrap();
// ^ include a borrow in the suggestion, even if the argument is not just a numeric literal
let _x = f.get(1 + 2).unwrap().to_string();
// ^ don't include a borrow here
let _x = f.get(1 + 2).unwrap().abs();
// ^ don't include a borrow here
}

View file

@ -187,5 +187,47 @@ LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec();
|
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message
error: aborting due to 26 previous errors
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:77:20
|
LL | let _x: &i32 = f.get(1 + 2).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `&f[1 + 2]`
error: used `unwrap()` on an `Option` value
--> $DIR/get_unwrap.rs:77:20
|
LL | let _x: &i32 = f.get(1 + 2).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:80:14
|
LL | let _x = f.get(1 + 2).unwrap().to_string();
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `f[1 + 2]`
error: used `unwrap()` on an `Option` value
--> $DIR/get_unwrap.rs:80:14
|
LL | let _x = f.get(1 + 2).unwrap().to_string();
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:83:14
|
LL | let _x = f.get(1 + 2).unwrap().abs();
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `f[1 + 2]`
error: used `unwrap()` on an `Option` value
--> $DIR/get_unwrap.rs:83:14
|
LL | let _x = f.get(1 + 2).unwrap().abs();
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message
error: aborting due to 32 previous errors