Skip to content

Commit ea72620

Browse files
committed
lint Option::get_or_insert and Result::map_or too
1 parent 9ed1843 commit ea72620

File tree

5 files changed

+124
-44
lines changed

5 files changed

+124
-44
lines changed

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub(super) fn check<'tcx>(
136136
fun_span: Option<Span>,
137137
) -> bool {
138138
// (path, fn_has_argument, methods, suffix)
139-
const KNOW_TYPES: [(Symbol, bool, &[Symbol], &str); 4] = [
139+
const KNOW_TYPES: [(Symbol, bool, &[Symbol], &str); 5] = [
140140
(sym::BTreeEntry, false, &[sym::or_insert], "with"),
141141
(sym::HashMapEntry, false, &[sym::or_insert], "with"),
142142
(
@@ -145,16 +145,17 @@ pub(super) fn check<'tcx>(
145145
&[sym::map_or, sym::ok_or, sym::or, sym::unwrap_or],
146146
"else",
147147
),
148-
(sym::Result, true, &[sym::or, sym::unwrap_or], "else"),
148+
(sym::Option, false, &[sym::get_or_insert], "with"),
149+
(sym::Result, true, &[sym::map_or, sym::or, sym::unwrap_or], "else"),
149150
];
150151

151152
if KNOW_TYPES.iter().any(|k| k.2.contains(&name))
152153
&& switch_to_lazy_eval(cx, arg)
153154
&& !contains_return(arg)
154155
&& let self_ty = cx.typeck_results().expr_ty(self_expr)
155-
&& let Some(&(_, fn_has_arguments, poss, suffix)) =
156-
KNOW_TYPES.iter().find(|&&i| is_type_diagnostic_item(cx, self_ty, i.0))
157-
&& poss.contains(&name)
156+
&& let Some(&(_, fn_has_arguments, _, suffix)) = KNOW_TYPES
157+
.iter()
158+
.find(|&&i| is_type_diagnostic_item(cx, self_ty, i.0) && i.2.contains(&name))
158159
{
159160
let ctxt = span.ctxt();
160161
let mut app = Applicability::HasPlaceholders;

clippy_utils/src/sym.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ generate! {
167167
futures_util,
168168
get,
169169
get_mut,
170+
get_or_insert,
170171
get_or_insert_with,
171172
get_unchecked,
172173
get_unchecked_mut,

tests/ui/or_fun_call.fixed

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
clippy::uninlined_format_args,
66
clippy::unnecessary_wraps,
77
clippy::unnecessary_literal_unwrap,
8+
clippy::unnecessary_result_map_or_else,
89
clippy::useless_vec
910
)]
1011

@@ -409,4 +410,33 @@ fn fn_call_in_nested_expr() {
409410
//~^ or_fun_call
410411
}
411412

413+
mod result_map_or {
414+
fn g() -> i32 {
415+
3
416+
}
417+
418+
fn f(n: i32) -> i32 {
419+
n
420+
}
421+
422+
fn test_map_or() {
423+
let x: Result<i32, ()> = Ok(4);
424+
let _ = x.map_or_else(|_| g(), |v| v);
425+
//~^ or_fun_call
426+
let _ = x.map_or_else(|_| g(), f);
427+
//~^ or_fun_call
428+
let _ = x.map_or(0, f);
429+
}
430+
}
431+
432+
fn test_option_get_or_insert() {
433+
// assume that this is slow call
434+
fn g() -> u8 {
435+
99
436+
}
437+
let mut x = Some(42_u8);
438+
let _ = x.get_or_insert_with(g);
439+
//~^ or_fun_call
440+
}
441+
412442
fn main() {}

tests/ui/or_fun_call.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
clippy::uninlined_format_args,
66
clippy::unnecessary_wraps,
77
clippy::unnecessary_literal_unwrap,
8+
clippy::unnecessary_result_map_or_else,
89
clippy::useless_vec
910
)]
1011

@@ -409,4 +410,33 @@ fn fn_call_in_nested_expr() {
409410
//~^ or_fun_call
410411
}
411412

413+
mod result_map_or {
414+
fn g() -> i32 {
415+
3
416+
}
417+
418+
fn f(n: i32) -> i32 {
419+
n
420+
}
421+
422+
fn test_map_or() {
423+
let x: Result<i32, ()> = Ok(4);
424+
let _ = x.map_or(g(), |v| v);
425+
//~^ or_fun_call
426+
let _ = x.map_or(g(), f);
427+
//~^ or_fun_call
428+
let _ = x.map_or(0, f);
429+
}
430+
}
431+
432+
fn test_option_get_or_insert() {
433+
// assume that this is slow call
434+
fn g() -> u8 {
435+
99
436+
}
437+
let mut x = Some(42_u8);
438+
let _ = x.get_or_insert(g());
439+
//~^ or_fun_call
440+
}
441+
412442
fn main() {}

0 commit comments

Comments
 (0)