Skip to content

Commit 211789f

Browse files
committed
Rust: replace singleton vectors with Option
1 parent 4c45f87 commit 211789f

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

rust/extractor/src/crate_graph.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,9 @@ fn emit_function(
340340
trap: &mut TrapFile,
341341
function: ra_ap_hir_def::FunctionId,
342342
visibility: Visibility,
343-
) -> Vec<trap::Label<generated::Item>> {
344-
let mut items = Vec::new();
345-
if let Some(type_) = db.value_ty(function.into()) {
346-
items.push(const_or_function(db, name, trap, type_, visibility));
347-
}
348-
items
343+
) -> Option<trap::Label<generated::Item>> {
344+
db.value_ty(function.into())
345+
.map(|type_| const_or_function(db, name, trap, type_, visibility))
349346
}
350347

351348
fn emit_const(
@@ -354,8 +351,7 @@ fn emit_const(
354351
trap: &mut TrapFile,
355352
konst: ra_ap_hir_def::ConstId,
356353
visibility: Visibility,
357-
) -> Vec<trap::Label<generated::Item>> {
358-
let mut items = Vec::new();
354+
) -> Option<trap::Label<generated::Item>> {
359355
let type_ = db.value_ty(konst.into());
360356
let type_repr = type_.and_then(|type_| emit_hir_ty(trap, db, type_.skip_binders()));
361357
let name = Some(trap.emit(generated::Name {
@@ -364,7 +360,7 @@ fn emit_const(
364360
}));
365361
let konst = db.const_data(konst);
366362
let visibility = emit_visibility(db, trap, visibility);
367-
items.push(
363+
Some(
368364
trap.emit(generated::Const {
369365
id: trap::TrapId::Star,
370366
name,
@@ -376,8 +372,7 @@ fn emit_const(
376372
visibility,
377373
})
378374
.into(),
379-
);
380-
items
375+
)
381376
}
382377

383378
fn emit_static(
@@ -386,8 +381,7 @@ fn emit_static(
386381
trap: &mut TrapFile,
387382
statik: ra_ap_hir_def::StaticId,
388383
visibility: Visibility,
389-
) -> Vec<trap::Label<generated::Item>> {
390-
let mut items = Vec::new();
384+
) -> Option<trap::Label<generated::Item>> {
391385
let type_ = db.value_ty(statik.into());
392386
let type_repr = type_.and_then(|type_| emit_hir_ty(trap, db, type_.skip_binders()));
393387
let name = Some(trap.emit(generated::Name {
@@ -396,7 +390,7 @@ fn emit_static(
396390
}));
397391
let statik = db.static_data(statik);
398392
let visibility = emit_visibility(db, trap, visibility);
399-
items.push(
393+
Some(
400394
trap.emit(generated::Static {
401395
id: trap::TrapId::Star,
402396
name,
@@ -409,8 +403,7 @@ fn emit_static(
409403
is_unsafe: statik.has_unsafe_kw,
410404
})
411405
.into(),
412-
);
413-
items
406+
)
414407
}
415408

416409
fn emit_generic_param_list(
@@ -511,9 +504,7 @@ fn emit_adt(
511504
trap: &mut TrapFile,
512505
adt_id: ra_ap_hir_def::AdtId,
513506
visibility: Visibility,
514-
) -> Vec<trap::Label<generated::Item>> {
515-
let mut items = Vec::new();
516-
507+
) -> Option<trap::Label<generated::Item>> {
517508
match adt_id {
518509
ra_ap_hir_def::AdtId::StructId(struct_id) => {
519510
let name = Some(trap.emit(generated::Name {
@@ -523,7 +514,7 @@ fn emit_adt(
523514
let field_list = emit_variant_data(trap, db, struct_id.into()).into();
524515
let visibility = emit_visibility(db, trap, visibility);
525516
let generic_param_list = emit_generic_param_list(trap, db, adt_id.into());
526-
items.push(
517+
Some(
527518
trap.emit(generated::Struct {
528519
id: trap::TrapId::Star,
529520
name,
@@ -534,7 +525,7 @@ fn emit_adt(
534525
where_clause: None,
535526
})
536527
.into(),
537-
);
528+
)
538529
}
539530
ra_ap_hir_def::AdtId::EnumId(enum_id) => {
540531
let data = db.enum_variants(enum_id);
@@ -568,7 +559,7 @@ fn emit_adt(
568559
}));
569560
let visibility = emit_visibility(db, trap, visibility);
570561
let generic_param_list = emit_generic_param_list(trap, db, adt_id.into());
571-
items.push(
562+
Some(
572563
trap.emit(generated::Enum {
573564
id: trap::TrapId::Star,
574565
name,
@@ -579,7 +570,7 @@ fn emit_adt(
579570
where_clause: None,
580571
})
581572
.into(),
582-
);
573+
)
583574
}
584575
ra_ap_hir_def::AdtId::UnionId(union_id) => {
585576
let name = Some(trap.emit(generated::Name {
@@ -589,7 +580,7 @@ fn emit_adt(
589580
let struct_field_list = emit_variant_data(trap, db, union_id.into()).into();
590581
let visibility = emit_visibility(db, trap, visibility);
591582
let generic_param_list = emit_generic_param_list(trap, db, adt_id.into());
592-
items.push(
583+
Some(
593584
trap.emit(generated::Union {
594585
id: trap::TrapId::Star,
595586
name,
@@ -600,10 +591,9 @@ fn emit_adt(
600591
where_clause: None,
601592
})
602593
.into(),
603-
);
594+
)
604595
}
605596
}
606-
items
607597
}
608598

609599
fn emit_trait(
@@ -612,8 +602,7 @@ fn emit_trait(
612602
trap: &mut TrapFile,
613603
trait_id: ra_ap_hir_def::TraitId,
614604
visibility: Visibility,
615-
) -> Vec<trap::Label<generated::Item>> {
616-
let mut items = Vec::new();
605+
) -> Option<trap::Label<generated::Item>> {
617606
let trait_items = db.trait_items(trait_id);
618607
let assoc_items: Vec<trap::Label<generated::AssocItem>> = trait_items
619608
.items
@@ -690,7 +679,7 @@ fn emit_trait(
690679
}));
691680
let visibility = emit_visibility(db, trap, visibility);
692681
let generic_param_list = emit_generic_param_list(trap, db, trait_id.into());
693-
items.push(
682+
Some(
694683
trap.emit(generated::Trait {
695684
id: trap::TrapId::Star,
696685
name,
@@ -704,8 +693,7 @@ fn emit_trait(
704693
where_clause: None,
705694
})
706695
.into(),
707-
);
708-
items
696+
)
709697
}
710698

711699
fn emit_module_impls(

0 commit comments

Comments
 (0)