Skip to content

Commit 16441ca

Browse files
committed
or_fun_call: lint Option::get_or_insert too
1 parent de0ac50 commit 16441ca

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 5 additions & 4 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::Option, false, &[sym::get_or_insert], "with"),
148149
(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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ fn test_option_get_or_insert() {
435435
99
436436
}
437437
let mut x = Some(42_u8);
438-
let _ = x.get_or_insert(g());
438+
let _ = x.get_or_insert_with(g);
439+
//~^ or_fun_call
439440
}
440441

441442
fn main() {}

tests/ui/or_fun_call.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ fn test_option_get_or_insert() {
436436
}
437437
let mut x = Some(42_u8);
438438
let _ = x.get_or_insert(g());
439+
//~^ or_fun_call
439440
}
440441

441442
fn main() {}

tests/ui/or_fun_call.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,11 @@ error: function call inside of `map_or`
258258
LL | let _ = x.map_or(g(), f);
259259
| ^^^^^^^^^^^^^^ help: try: `map_or_else(|_| g(), f)`
260260

261-
error: aborting due to 40 previous errors
261+
error: function call inside of `get_or_insert`
262+
--> tests/ui/or_fun_call.rs:438:15
263+
|
264+
LL | let _ = x.get_or_insert(g());
265+
| ^^^^^^^^^^^^^^^^^^ help: try: `get_or_insert_with(g)`
266+
267+
error: aborting due to 41 previous errors
262268

0 commit comments

Comments
 (0)