Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cc80c5b

Browse files
committed
remove unnecessary lazy evaluations
1 parent 7530d76 commit cc80c5b

File tree

31 files changed

+50
-51
lines changed

31 files changed

+50
-51
lines changed

crates/hir-def/src/item_scope.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,14 @@ impl ItemScope {
159159
pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility)> {
160160
let (def, mut iter) = match item {
161161
ItemInNs::Macros(def) => {
162-
return self
163-
.macros
164-
.iter()
165-
.find_map(|(name, &(other_def, vis))| (other_def == def).then(|| (name, vis)));
162+
return self.macros.iter().find_map(|(name, &(other_def, vis))| {
163+
(other_def == def).then_some((name, vis))
164+
});
166165
}
167166
ItemInNs::Types(def) => (def, self.types.iter()),
168167
ItemInNs::Values(def) => (def, self.values.iter()),
169168
};
170-
iter.find_map(|(name, &(other_def, vis))| (other_def == def).then(|| (name, vis)))
169+
iter.find_map(|(name, &(other_def, vis))| (other_def == def).then_some((name, vis)))
171170
}
172171

173172
pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {

crates/hir-def/src/macro_expansion_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
170170
}
171171
let pp = pretty_print_macro_expansion(
172172
parse.syntax_node(),
173-
show_token_ids.then(|| &*token_map),
173+
show_token_ids.then_some(&*token_map),
174174
);
175175
let indent = IndentLevel::from_node(call.syntax());
176176
let pp = reindent(indent, pp);

crates/hir-expand/src/eager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ fn eager_macro_recur(
208208
// Collect replacement
209209
for child in children {
210210
let def = match child.path().and_then(|path| ModPath::from_src(db, path, hygiene)) {
211-
Some(path) => macro_resolver(path.clone()).ok_or_else(|| UnresolvedMacro { path })?,
211+
Some(path) => macro_resolver(path.clone()).ok_or(UnresolvedMacro { path })?,
212212
None => {
213213
diagnostic_sink(ExpandError::Other("malformed macro invocation".into()));
214214
continue;

crates/hir-ty/src/layout/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn eval_goal(ra_fixture: &str, minicore: &str) -> Result<Layout, LayoutError> {
3737
hir_def::AdtId::UnionId(x) => db.union_data(x).name.to_smol_str(),
3838
hir_def::AdtId::EnumId(x) => db.enum_data(x).name.to_smol_str(),
3939
};
40-
(name == "Goal").then(|| x)
40+
(name == "Goal").then_some(x)
4141
}
4242
_ => None,
4343
})

crates/hir-ty/src/method_resolution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ fn lookup_impl_assoc_item_for_trait_ref(
714714
let impl_data = find_matching_impl(impls, table, trait_ref)?;
715715
impl_data.items.iter().find_map(|it| match it {
716716
AssocItemId::FunctionId(f) => {
717-
(db.function_data(*f).name == *name).then(|| AssocItemId::FunctionId(*f))
717+
(db.function_data(*f).name == *name).then_some(AssocItemId::FunctionId(*f))
718718
}
719719
AssocItemId::ConstId(c) => db
720720
.const_data(*c)

crates/hir-ty/src/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl TraitEnvironment {
6161
) -> impl Iterator<Item = TraitId> + 'a {
6262
self.traits_from_clauses
6363
.iter()
64-
.filter_map(move |(self_ty, trait_id)| (*self_ty == ty).then(|| *trait_id))
64+
.filter_map(move |(self_ty, trait_id)| (*self_ty == ty).then_some(*trait_id))
6565
}
6666
}
6767

crates/hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ impl Function {
15591559
}
15601560

15611561
pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
1562-
self.has_self_param(db).then(|| SelfParam { func: self.id })
1562+
self.has_self_param(db).then_some(SelfParam { func: self.id })
15631563
}
15641564

15651565
pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param> {

crates/hir/src/semantics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ impl<'db> SemanticsImpl<'db> {
795795
// requeue the tokens we got from mapping our current token down
796796
stack.extend(mapped_tokens);
797797
// if the length changed we have found a mapping for the token
798-
(stack.len() != len).then(|| ())
798+
(stack.len() != len).then_some(())
799799
};
800800

801801
// Remap the next token in the queue into a macro call its in, if it is not being remapped
@@ -1221,7 +1221,7 @@ impl<'db> SemanticsImpl<'db> {
12211221
krate
12221222
.dependencies(self.db)
12231223
.into_iter()
1224-
.find_map(|dep| (dep.name == name).then(|| dep.krate))
1224+
.find_map(|dep| (dep.name == name).then_some(dep.krate))
12251225
}
12261226

12271227
fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantId> {

crates/hir/src/source_analyzer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ fn resolve_hir_path_(
987987
db,
988988
def,
989989
res.in_type_ns()?,
990-
|name, id| (name == unresolved.name).then(|| id),
990+
|name, id| (name == unresolved.name).then_some(id),
991991
)
992992
})
993993
.map(TypeAlias::from)

crates/ide-assists/src/handlers/add_missing_match_arms.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl ExtendedEnum {
326326
fn resolve_enum_def(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> Option<ExtendedEnum> {
327327
sema.type_of_expr(expr)?.adjusted().autoderef(sema.db).find_map(|ty| match ty.as_adt() {
328328
Some(Adt::Enum(e)) => Some(ExtendedEnum::Enum(e)),
329-
_ => ty.is_bool().then(|| ExtendedEnum::Bool),
329+
_ => ty.is_bool().then_some(ExtendedEnum::Bool),
330330
})
331331
}
332332

@@ -344,7 +344,7 @@ fn resolve_tuple_of_enum_def(
344344
// For now we only handle expansion for a tuple of enums. Here
345345
// we map non-enum items to None and rely on `collect` to
346346
// convert Vec<Option<hir::Enum>> into Option<Vec<hir::Enum>>.
347-
_ => ty.is_bool().then(|| ExtendedEnum::Bool),
347+
_ => ty.is_bool().then_some(ExtendedEnum::Bool),
348348
})
349349
})
350350
.collect()

crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ fn validate_method_call_expr(
216216
let krate = module.krate();
217217

218218
let iter_trait = FamousDefs(sema, krate).core_iter_Iterator()?;
219-
it_type.impls_trait(sema.db, iter_trait, &[]).then(|| (expr, receiver))
219+
it_type.impls_trait(sema.db, iter_trait, &[]).then_some((expr, receiver))
220220
}
221221

222222
#[cfg(test)]

crates/ide-assists/src/handlers/extract_function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ impl FunctionBody {
588588
FunctionBody::Expr(expr) => Some(expr.clone()),
589589
FunctionBody::Span { parent, text_range } => {
590590
let tail_expr = parent.tail_expr()?;
591-
text_range.contains_range(tail_expr.syntax().text_range()).then(|| tail_expr)
591+
text_range.contains_range(tail_expr.syntax().text_range()).then_some(tail_expr)
592592
}
593593
}
594594
}

crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn extract_generic_params(
178178
.fold(false, |tagged, ty| tag_generics_in_variant(&ty, &mut generics) || tagged),
179179
};
180180

181-
let generics = generics.into_iter().filter_map(|(param, tag)| tag.then(|| param));
181+
let generics = generics.into_iter().filter_map(|(param, tag)| tag.then_some(param));
182182
tagged_one.then(|| make::generic_param_list(generics))
183183
}
184184

crates/ide-assists/src/handlers/generate_getter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn generate_getter_from_info(
271271
}}",
272272
vis,
273273
record_field_info.fn_name,
274-
info.mutable.then(|| "mut ").unwrap_or_default(),
274+
info.mutable.then_some("mut ").unwrap_or_default(),
275275
ty,
276276
body,
277277
);

crates/ide-assists/src/handlers/remove_dbg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn compute_dbg_replacement(macro_call: ast::MacroCall) -> Option<(TextRange, Str
6464
let input_expressions = mac_input.group_by(|tok| tok.kind() == T![,]);
6565
let input_expressions = input_expressions
6666
.into_iter()
67-
.filter_map(|(is_sep, group)| (!is_sep).then(|| group))
67+
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
6868
.map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join("")))
6969
.collect::<Option<Vec<ast::Expr>>>()?;
7070

crates/ide-assists/src/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ pub(crate) fn convert_reference_type(
613613
}
614614

615615
fn handle_copy(ty: &hir::Type, db: &dyn HirDatabase) -> Option<ReferenceConversionType> {
616-
ty.is_copy(db).then(|| ReferenceConversionType::Copy)
616+
ty.is_copy(db).then_some(ReferenceConversionType::Copy)
617617
}
618618

619619
fn handle_as_ref_str(
@@ -624,7 +624,7 @@ fn handle_as_ref_str(
624624
let str_type = hir::BuiltinType::str().ty(db);
625625

626626
ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[str_type])
627-
.then(|| ReferenceConversionType::AsRefStr)
627+
.then_some(ReferenceConversionType::AsRefStr)
628628
}
629629

630630
fn handle_as_ref_slice(
@@ -636,7 +636,7 @@ fn handle_as_ref_slice(
636636
let slice_type = hir::Type::new_slice(type_argument);
637637

638638
ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[slice_type])
639-
.then(|| ReferenceConversionType::AsRefSlice)
639+
.then_some(ReferenceConversionType::AsRefSlice)
640640
}
641641

642642
fn handle_dereferenced(
@@ -647,7 +647,7 @@ fn handle_dereferenced(
647647
let type_argument = ty.type_arguments().next()?;
648648

649649
ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[type_argument])
650-
.then(|| ReferenceConversionType::Dereferenced)
650+
.then_some(ReferenceConversionType::Dereferenced)
651651
}
652652

653653
fn handle_option_as_ref(

crates/ide-completion/src/completions/attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn parse_comma_sep_expr(input: ast::TokenTree) -> Option<Vec<ast::Expr>> {
357357
Some(
358358
input_expressions
359359
.into_iter()
360-
.filter_map(|(is_sep, group)| (!is_sep).then(|| group))
360+
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
361361
.filter_map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join("")))
362362
.collect::<Vec<ast::Expr>>(),
363363
)

crates/ide-db/src/imports/merge_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn recursive_merge(lhs: &ast::UseTree, rhs: &ast::UseTree, merge: MergeBehavior)
9191
.flat_map(|list| list.use_trees())
9292
// We use Option here to early return from this function(this is not the
9393
// same as a `filter` op).
94-
.map(|tree| merge.is_tree_allowed(&tree).then(|| tree))
94+
.map(|tree| merge.is_tree_allowed(&tree).then_some(tree))
9595
.collect::<Option<_>>()?;
9696
use_trees.sort_unstable_by(|a, b| path_cmp_for_sort(a.path(), b.path()));
9797
for rhs_t in rhs.use_tree_list().into_iter().flat_map(|list| list.use_trees()) {

crates/ide-db/src/search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ impl<'a> FindUsages<'a> {
608608
let reference = FileReference {
609609
range,
610610
name: ast::NameLike::NameRef(name_ref.clone()),
611-
category: is_name_ref_in_import(name_ref).then(|| ReferenceCategory::Import),
611+
category: is_name_ref_in_import(name_ref).then_some(ReferenceCategory::Import),
612612
};
613613
sink(file_id, reference)
614614
}
@@ -787,7 +787,7 @@ impl ReferenceCategory {
787787
fn new(def: &Definition, r: &ast::NameRef) -> Option<ReferenceCategory> {
788788
// Only Locals and Fields have accesses for now.
789789
if !matches!(def, Definition::Local(_) | Definition::Field(_)) {
790-
return is_name_ref_in_import(r).then(|| ReferenceCategory::Import);
790+
return is_name_ref_in_import(r).then_some(ReferenceCategory::Import);
791791
}
792792

793793
let mode = r.syntax().ancestors().find_map(|node| {

crates/ide-db/src/syntax_helpers/node_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ pub fn parse_tt_as_comma_sep_paths(input: ast::TokenTree) -> Option<Vec<ast::Pat
449449
let input_expressions = tokens.group_by(|tok| tok.kind() == T![,]);
450450
let paths = input_expressions
451451
.into_iter()
452-
.filter_map(|(is_sep, group)| (!is_sep).then(|| group))
452+
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
453453
.filter_map(|mut tokens| {
454454
syntax::hacks::parse_expr_from_str(&tokens.join("")).and_then(|expr| match expr {
455455
ast::Expr::PathExpr(it) => it.path(),

crates/ide/src/doc_links.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl DocCommentToken {
273273
let (in_expansion_range, link, ns) =
274274
extract_definitions_from_docs(&docs).into_iter().find_map(|(range, link, ns)| {
275275
let mapped = doc_mapping.map(range)?;
276-
(mapped.value.contains(abs_in_expansion_offset)).then(|| (mapped.value, link, ns))
276+
(mapped.value.contains(abs_in_expansion_offset)).then_some((mapped.value, link, ns))
277277
})?;
278278
// get the relative range to the doc/attribute in the expansion
279279
let in_expansion_relative_range = in_expansion_range - descended_prefix_len - token_start;

crates/ide/src/extend_selection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fn extend_single_word_in_comment_or_string(
205205
}
206206

207207
let start_idx = before.rfind(non_word_char)? as u32;
208-
let end_idx = after.find(non_word_char).unwrap_or_else(|| after.len()) as u32;
208+
let end_idx = after.find(non_word_char).unwrap_or(after.len()) as u32;
209209

210210
let from: TextSize = (start_idx + 1).into();
211211
let to: TextSize = (cursor_position + end_idx).into();

crates/ide/src/goto_implementation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn impls_for_trait_item(
110110
.filter_map(|imp| {
111111
let item = imp.items(sema.db).iter().find_map(|itm| {
112112
let itm_name = itm.name(sema.db)?;
113-
(itm_name == fun_name).then(|| *itm)
113+
(itm_name == fun_name).then_some(*itm)
114114
})?;
115115
item.try_to_nav(sema.db)
116116
})

crates/ide/src/highlight_related.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn highlight_references(
110110
.and_then(|decl| decl.focus_range)
111111
.map(|range| {
112112
let category =
113-
references::decl_mutability(&def, node, range).then(|| ReferenceCategory::Write);
113+
references::decl_mutability(&def, node, range).then_some(ReferenceCategory::Write);
114114
HighlightedRange { range, category }
115115
});
116116
if let Some(hl_range) = hl_range {
@@ -365,7 +365,7 @@ mod tests {
365365

366366
let mut expected = annotations
367367
.into_iter()
368-
.map(|(r, access)| (r.range, (!access.is_empty()).then(|| access)))
368+
.map(|(r, access)| (r.range, (!access.is_empty()).then_some(access)))
369369
.collect::<Vec<_>>();
370370

371371
let mut actual = hls

crates/ide/src/inlay_hints/bind_pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn is_named_constructor(
167167
ast::PathSegmentKind::Type { type_ref: Some(ty), trait_ref: None } => ty.to_string(),
168168
_ => return None,
169169
};
170-
(ctor_name == ty_name).then(|| ())
170+
(ctor_name == ty_name).then_some(())
171171
}
172172

173173
fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &hir::Type) -> bool {

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn punctuation(
111111
let is_raw_ptr = (|| {
112112
let prefix_expr = parent.and_then(ast::PrefixExpr::cast)?;
113113
let expr = prefix_expr.expr()?;
114-
sema.type_of_expr(&expr)?.original.is_raw_ptr().then(|| ())
114+
sema.type_of_expr(&expr)?.original.is_raw_ptr().then_some(())
115115
})();
116116
if let Some(()) = is_raw_ptr {
117117
HlTag::Operator(HlOperator::Other) | HlMod::Unsafe

crates/mbe/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl Shift {
140140
| tt::Leaf::Punct(tt::Punct { id, .. })
141141
| tt::Leaf::Literal(tt::Literal { id, .. })) = leaf;
142142

143-
(id != tt::TokenId::unspecified()).then(|| id.0)
143+
(id != tt::TokenId::unspecified()).then_some(id.0)
144144
}
145145
};
146146
subtree.token_trees.iter().filter_map(filter).max()

crates/mbe/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ fn parse_repeat(src: &mut TtIter<'_>) -> Result<(Option<Separator>, RepeatKind),
273273
_ => return Err(ParseError::InvalidRepeat),
274274
},
275275
};
276-
return Ok((has_sep.then(|| separator), repeat_kind));
276+
return Ok((has_sep.then_some(separator), repeat_kind));
277277
}
278278
}
279279
}

crates/rust-analyzer/src/to_proto.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ fn completion_item(
228228
max_relevance: u32,
229229
item: CompletionItem,
230230
) {
231-
let insert_replace_support = config.insert_replace_support().then(|| tdpp.position);
231+
let insert_replace_support = config.insert_replace_support().then_some(tdpp.position);
232232
let mut additional_text_edits = Vec::new();
233233

234234
// LSP does not allow arbitrary edits in completion, so we have to do a
@@ -258,7 +258,7 @@ fn completion_item(
258258
text_edit.unwrap()
259259
};
260260

261-
let insert_text_format = item.is_snippet().then(|| lsp_types::InsertTextFormat::SNIPPET);
261+
let insert_text_format = item.is_snippet().then_some(lsp_types::InsertTextFormat::SNIPPET);
262262
let tags = item.deprecated().then(|| vec![lsp_types::CompletionItemTag::DEPRECATED]);
263263
let command = if item.trigger_call_info() && config.client_commands().trigger_parameter_hints {
264264
Some(command::trigger_parameter_hints())

0 commit comments

Comments
 (0)