Skip to content

Commit

Permalink
Add tests and docs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
b-ncMN committed Apr 14, 2022
1 parent 8229936 commit 2903b56
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 4 additions & 2 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion tests/ui/needless_option_take.rs
Original file line number Diff line number Diff line change
@@ -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();
}
10 changes: 9 additions & 1 deletion tests/ui/option_take_on_temporary.fixed
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 2903b56

Please sign in to comment.