diff options
| author | infrandomness <infrandomness@gmail.com> | 2022-04-12 19:34:55 +0200 |
|---|---|---|
| committer | infrandomness <infrandomness@gmail.com> | 2022-04-14 13:16:46 +0200 |
| commit | 2903b56f177b2b23db62d6bf2d500837d704fb4e (patch) | |
| tree | 096e2993bbd14f6218c1d833262ef6c816d1fee5 | |
| parent | 822993675ff45d5b4b2f0a310c65d4abd1aef20a (diff) | |
| download | rust-2903b56f177b2b23db62d6bf2d500837d704fb4e.tar.gz rust-2903b56f177b2b23db62d6bf2d500837d704fb4e.zip | |
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
| -rw-r--r-- | clippy_lints/src/methods/mod.rs | 6 | ||||
| -rw-r--r-- | tests/ui/needless_option_take.rs | 10 | ||||
| -rw-r--r-- | tests/ui/option_take_on_temporary.fixed | 10 |
3 files changed, 22 insertions, 4 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 36844b3609b..48fa30ba929 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -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, diff --git a/tests/ui/needless_option_take.rs b/tests/ui/needless_option_take.rs index 27cc9a2e61e..9f4109eb463 100644 --- a/tests/ui/needless_option_take.rs +++ b/tests/ui/needless_option_take.rs @@ -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(); } diff --git a/tests/ui/option_take_on_temporary.fixed b/tests/ui/option_take_on_temporary.fixed index 7e293076682..29691e81666 100644 --- a/tests/ui/option_take_on_temporary.fixed +++ b/tests/ui/option_take_on_temporary.fixed @@ -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(); } |
