allow needless_option_take to report for more cases (#13684)
changelog: ``` changelog: [`needless_option_take`]: now lints for all temporary expressions of type Option<T>. ``` fixes #13671 In general, needless_option_take should report whenever take() is called on a temporary value, not just when the temporary value is created by as_ref(). Also, we stop emitting machine applicable fixes in these cases, since sometimes the intention of the user is to actually clear the original option, in cases such as `option.as_mut().take()`.
This commit is contained in:
commit
62407104fb
4 changed files with 146 additions and 40 deletions
|
|
@ -1,13 +0,0 @@
|
|||
fn main() {
|
||||
println!("Testing non erroneous option_take_on_temporary");
|
||||
let mut option = Some(1);
|
||||
let _ = Box::new(move || option.take().unwrap());
|
||||
|
||||
println!("Testing non erroneous option_take_on_temporary");
|
||||
let x = Some(3);
|
||||
x.as_ref();
|
||||
|
||||
println!("Testing erroneous option_take_on_temporary");
|
||||
let x = Some(3);
|
||||
x.as_ref();
|
||||
}
|
||||
|
|
@ -1,3 +1,15 @@
|
|||
struct MyStruct;
|
||||
|
||||
impl MyStruct {
|
||||
pub fn get_option() -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
fn return_option() -> Option<i32> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Testing non erroneous option_take_on_temporary");
|
||||
let mut option = Some(1);
|
||||
|
|
@ -7,7 +19,40 @@ fn main() {
|
|||
let x = Some(3);
|
||||
x.as_ref();
|
||||
|
||||
println!("Testing erroneous option_take_on_temporary");
|
||||
let x = Some(3);
|
||||
x.as_ref().take();
|
||||
//~^ ERROR: called `Option::take()` on a temporary value
|
||||
|
||||
println!("Testing non erroneous option_take_on_temporary");
|
||||
let mut x = Some(3);
|
||||
let y = x.as_mut();
|
||||
|
||||
let mut x = Some(3);
|
||||
let y = x.as_mut().take();
|
||||
//~^ ERROR: called `Option::take()` on a temporary value
|
||||
let y = x.replace(289).take();
|
||||
//~^ ERROR: called `Option::take()` on a temporary value
|
||||
|
||||
let y = Some(3).as_mut().take();
|
||||
//~^ ERROR: called `Option::take()` on a temporary value
|
||||
|
||||
let y = Option::as_mut(&mut x).take();
|
||||
//~^ ERROR: called `Option::take()` on a temporary value
|
||||
|
||||
let x = return_option();
|
||||
let x = return_option().take();
|
||||
//~^ ERROR: called `Option::take()` on a temporary value
|
||||
|
||||
let x = MyStruct::get_option();
|
||||
let x = MyStruct::get_option().take();
|
||||
//~^ ERROR: called `Option::take()` on a temporary value
|
||||
|
||||
let mut my_vec = vec![1, 2, 3];
|
||||
my_vec.push(4);
|
||||
let y = my_vec.first();
|
||||
let y = my_vec.first().take();
|
||||
//~^ ERROR: called `Option::take()` on a temporary value
|
||||
|
||||
let y = my_vec.first().take();
|
||||
//~^ ERROR: called `Option::take()` on a temporary value
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,76 @@
|
|||
error: called `Option::take()` on a temporary value
|
||||
--> tests/ui/needless_option_take.rs:12:5
|
||||
--> tests/ui/needless_option_take.rs:23:5
|
||||
|
|
||||
LL | x.as_ref().take();
|
||||
| ^^^^^^^^^^^^^^^^^ help: try: `x.as_ref()`
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `as_ref` creates a temporary value, so calling take() has no effect
|
||||
= note: `-D clippy::needless-option-take` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::needless_option_take)]`
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
error: called `Option::take()` on a temporary value
|
||||
--> tests/ui/needless_option_take.rs:31:13
|
||||
|
|
||||
LL | let y = x.as_mut().take();
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `as_mut` creates a temporary value, so calling take() has no effect
|
||||
|
||||
error: called `Option::take()` on a temporary value
|
||||
--> tests/ui/needless_option_take.rs:33:13
|
||||
|
|
||||
LL | let y = x.replace(289).take();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `replace` creates a temporary value, so calling take() has no effect
|
||||
|
||||
error: called `Option::take()` on a temporary value
|
||||
--> tests/ui/needless_option_take.rs:36:13
|
||||
|
|
||||
LL | let y = Some(3).as_mut().take();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `as_mut` creates a temporary value, so calling take() has no effect
|
||||
|
||||
error: called `Option::take()` on a temporary value
|
||||
--> tests/ui/needless_option_take.rs:39:13
|
||||
|
|
||||
LL | let y = Option::as_mut(&mut x).take();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `as_mut` creates a temporary value, so calling take() has no effect
|
||||
|
||||
error: called `Option::take()` on a temporary value
|
||||
--> tests/ui/needless_option_take.rs:43:13
|
||||
|
|
||||
LL | let x = return_option().take();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `return_option` creates a temporary value, so calling take() has no effect
|
||||
|
||||
error: called `Option::take()` on a temporary value
|
||||
--> tests/ui/needless_option_take.rs:47:13
|
||||
|
|
||||
LL | let x = MyStruct::get_option().take();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `get_option` creates a temporary value, so calling take() has no effect
|
||||
|
||||
error: called `Option::take()` on a temporary value
|
||||
--> tests/ui/needless_option_take.rs:53:13
|
||||
|
|
||||
LL | let y = my_vec.first().take();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `first` creates a temporary value, so calling take() has no effect
|
||||
|
||||
error: called `Option::take()` on a temporary value
|
||||
--> tests/ui/needless_option_take.rs:56:13
|
||||
|
|
||||
LL | let y = my_vec.first().take();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `first` creates a temporary value, so calling take() has no effect
|
||||
|
||||
error: aborting due to 9 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue