From dc3b8bef8052a6dbd877cd9ecc4c26592685aa4c Mon Sep 17 00:00:00 2001 From: klensy Date: Tue, 17 Jun 2025 17:21:46 +0300 Subject: [PATCH 1/2] precommit test --- tests/ui/or_fun_call.fixed | 18 ++++++++++++++++++ tests/ui/or_fun_call.rs | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/tests/ui/or_fun_call.fixed b/tests/ui/or_fun_call.fixed index 34f3e0468419..bb46ce044661 100644 --- a/tests/ui/or_fun_call.fixed +++ b/tests/ui/or_fun_call.fixed @@ -439,4 +439,22 @@ fn test_option_get_or_insert() { //~^ or_fun_call } +fn test_option_and() { + // assume that this is slow call + fn g() -> Option { + Some(99) + } + let mut x = Some(42_u8); + let _ = x.and(g()); +} + +fn test_result_and() { + // assume that this is slow call + fn g() -> Result { + Ok(99) + } + let mut x: Result = Ok(42); + let _ = x.and(g()); +} + fn main() {} diff --git a/tests/ui/or_fun_call.rs b/tests/ui/or_fun_call.rs index dc57bd6060ac..68ef244a019b 100644 --- a/tests/ui/or_fun_call.rs +++ b/tests/ui/or_fun_call.rs @@ -439,4 +439,22 @@ fn test_option_get_or_insert() { //~^ or_fun_call } +fn test_option_and() { + // assume that this is slow call + fn g() -> Option { + Some(99) + } + let mut x = Some(42_u8); + let _ = x.and(g()); +} + +fn test_result_and() { + // assume that this is slow call + fn g() -> Result { + Ok(99) + } + let mut x: Result = Ok(42); + let _ = x.and(g()); +} + fn main() {} From a82c142f3e0be8f88e588a18bf897899d30f179d Mon Sep 17 00:00:00 2001 From: klensy Date: Tue, 17 Jun 2025 17:27:56 +0300 Subject: [PATCH 2/2] or_fun_call: lint Option/Result::and(_then) too --- clippy_lints/src/methods/or_fun_call.rs | 4 +++- tests/ui/or_fun_call.fixed | 6 ++++-- tests/ui/or_fun_call.rs | 2 ++ tests/ui/or_fun_call.stderr | 14 +++++++++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/methods/or_fun_call.rs b/clippy_lints/src/methods/or_fun_call.rs index 2139466ce746..6ce7dd3d4d0a 100644 --- a/clippy_lints/src/methods/or_fun_call.rs +++ b/clippy_lints/src/methods/or_fun_call.rs @@ -136,7 +136,7 @@ pub(super) fn check<'tcx>( fun_span: Option, ) -> bool { // (path, fn_has_argument, methods, suffix) - const KNOW_TYPES: [(Symbol, bool, &[Symbol], &str); 5] = [ + const KNOW_TYPES: [(Symbol, bool, &[Symbol], &str); 7] = [ (sym::BTreeEntry, false, &[sym::or_insert], "with"), (sym::HashMapEntry, false, &[sym::or_insert], "with"), ( @@ -146,7 +146,9 @@ pub(super) fn check<'tcx>( "else", ), (sym::Option, false, &[sym::get_or_insert], "with"), + (sym::Option, true, &[sym::and], "then"), (sym::Result, true, &[sym::map_or, sym::or, sym::unwrap_or], "else"), + (sym::Result, true, &[sym::and], "then"), ]; if KNOW_TYPES.iter().any(|k| k.2.contains(&name)) diff --git a/tests/ui/or_fun_call.fixed b/tests/ui/or_fun_call.fixed index bb46ce044661..bcd2602edb6a 100644 --- a/tests/ui/or_fun_call.fixed +++ b/tests/ui/or_fun_call.fixed @@ -445,7 +445,8 @@ fn test_option_and() { Some(99) } let mut x = Some(42_u8); - let _ = x.and(g()); + let _ = x.and_then(|_| g()); + //~^ or_fun_call } fn test_result_and() { @@ -454,7 +455,8 @@ fn test_result_and() { Ok(99) } let mut x: Result = Ok(42); - let _ = x.and(g()); + let _ = x.and_then(|_| g()); + //~^ or_fun_call } fn main() {} diff --git a/tests/ui/or_fun_call.rs b/tests/ui/or_fun_call.rs index 68ef244a019b..8d1202ebf914 100644 --- a/tests/ui/or_fun_call.rs +++ b/tests/ui/or_fun_call.rs @@ -446,6 +446,7 @@ fn test_option_and() { } let mut x = Some(42_u8); let _ = x.and(g()); + //~^ or_fun_call } fn test_result_and() { @@ -455,6 +456,7 @@ fn test_result_and() { } let mut x: Result = Ok(42); let _ = x.and(g()); + //~^ or_fun_call } fn main() {} diff --git a/tests/ui/or_fun_call.stderr b/tests/ui/or_fun_call.stderr index 0f159fe8bff4..585ee2d0e19d 100644 --- a/tests/ui/or_fun_call.stderr +++ b/tests/ui/or_fun_call.stderr @@ -264,5 +264,17 @@ error: function call inside of `get_or_insert` LL | let _ = x.get_or_insert(g()); | ^^^^^^^^^^^^^^^^^^ help: try: `get_or_insert_with(g)` -error: aborting due to 41 previous errors +error: function call inside of `and` + --> tests/ui/or_fun_call.rs:448:15 + | +LL | let _ = x.and(g()); + | ^^^^^^^^ help: try: `and_then(|_| g())` + +error: function call inside of `and` + --> tests/ui/or_fun_call.rs:458:15 + | +LL | let _ = x.and(g()); + | ^^^^^^^^ help: try: `and_then(|_| g())` + +error: aborting due to 43 previous errors