or_fun_call: lint Option/Result::and(_then) too

This commit is contained in:
klensy 2025-06-17 17:27:56 +03:00
parent dc3b8bef80
commit a82c142f3e
4 changed files with 22 additions and 4 deletions

View file

@ -136,7 +136,7 @@ pub(super) fn check<'tcx>(
fun_span: Option<Span>,
) -> 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))

View file

@ -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<u8, ()> = Ok(42);
let _ = x.and(g());
let _ = x.and_then(|_| g());
//~^ or_fun_call
}
fn main() {}

View file

@ -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<u8, ()> = Ok(42);
let _ = x.and(g());
//~^ or_fun_call
}
fn main() {}

View file

@ -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