Add tests and docs

This adds test to make sure correct behavior of lint
- The first test's option variable is not a temporary variable
- The second test does not make usage of `take()`
- The third test makes usage of `take()` and uses a temporary variable
This commit is contained in:
infrandomness 2022-04-12 19:34:55 +02:00
parent 822993675f
commit 2903b56f17
3 changed files with 22 additions and 4 deletions

View File

@ -2169,11 +2169,13 @@ declare_clippy_lint! {
///
/// ### Example
/// ```rust
/// // example code where clippy issues a warning
/// let x = Some(3);
/// x.as_ref().take();
/// ```
/// Use instead:
/// ```rust
/// // example code which does not raise clippy warning
/// let x = Some(3);
/// x.as_ref();
/// ```
#[clippy::version = "1.61.0"]
pub NEEDLESS_OPTION_TAKE,

View File

@ -1,7 +1,15 @@
// run-rustfix
fn main() {
println!("Testing option_take_on_temporary");
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().take();
}

View File

@ -1,7 +1,15 @@
// run-rustfix
fn main() {
println!("Testing option_take_on_temporary");
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();
}