Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions clippy_utils/src/eager_or_lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,15 @@ impl ops::BitOrAssign for EagernessSuggestion {

/// Determine the eagerness of the given function call.
fn fn_eagerness(cx: &LateContext<'_>, fn_id: DefId, name: Symbol, have_one_arg: bool) -> EagernessSuggestion {
use EagernessSuggestion::{Eager, Lazy, NoChange};
use EagernessSuggestion::{Lazy, NoChange};

let ty = match cx.tcx.impl_of_assoc(fn_id) {
Some(id) => cx.tcx.type_of(id).instantiate_identity(),
None => return Lazy,
};

if (matches!(name, sym::is_empty | sym::len) || name.as_str().starts_with("as_")) && have_one_arg {
if matches!(
cx.tcx.crate_name(fn_id.krate),
sym::std | sym::core | sym::alloc | sym::proc_macro
) {
Eager
} else {
NoChange
}
NoChange
} else if let ty::Adt(def, subs) = ty.kind() {
// Types where the only fields are generic types (or references to) with no trait bounds other
// than marker traits.
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/option_if_let_else.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn main() {

let s = String::new();
// Lint, both branches immutably borrow `s`.
let _ = Some(0).map_or(s.len(), |x| s.len() + x);
let _ = Some(0).map_or_else(|| s.len(), |x| s.len() + x);
//~^ option_if_let_else

let s = String::new();
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/option_if_let_else.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ LL + }
LL ~ });
|

error: use Option::map_or instead of an if let/else
error: use Option::map_or_else instead of an if let/else
--> tests/ui/option_if_let_else.rs:197:13
|
LL | let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or(s.len(), |x| s.len() + x)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or_else(|| s.len(), |x| s.len() + x)`

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:202:13
Expand Down
Loading