Skip to content

Commit 5cfbedc

Browse files
committed
Rust: replace singleton vectors with Option
1 parent b24fbe8 commit 5cfbedc

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
@@ -346,12 +346,9 @@ fn emit_function(
346346
trap: &mut TrapFile,
347347
function: ra_ap_hir_def::FunctionId,
348348
visibility: Visibility,
349-
) -> Vec<trap::Label<generated::Item>> {
350-
let mut items = Vec::new();
351-
if let Some(type_) = db.value_ty(function.into()) {
352-
items.push(const_or_function(db, name, trap, type_, visibility));
353-
}
354-
items
349+
) -> Option<trap::Label<generated::Item>> {
350+
db.value_ty(function.into())
351+
.map(|type_| const_or_function(db, name, trap, type_, visibility))
355352
}
356353

357354
fn emit_const(
@@ -360,8 +357,7 @@ fn emit_const(
360357
trap: &mut TrapFile,
361358
konst: ra_ap_hir_def::ConstId,
362359
visibility: Visibility,
363-
) -> Vec<trap::Label<generated::Item>> {
364-
let mut items = Vec::new();
360+
) -> Option<trap::Label<generated::Item>> {
365361
let type_ = db.value_ty(konst.into());
366362
let type_repr = type_.and_then(|type_| emit_hir_ty(trap, db, type_.skip_binders()));
367363
let name = Some(trap.emit(generated::Name {
@@ -370,7 +366,7 @@ fn emit_const(
370366
}));
371367
let konst = db.const_data(konst);
372368
let visibility = emit_visibility(db, trap, visibility);
373-
items.push(
369+
Some(
374370
trap.emit(generated::Const {
375371
id: trap::TrapId::Star,
376372
name,
@@ -382,8 +378,7 @@ fn emit_const(
382378
visibility,
383379
})
384380
.into(),
385-
);
386-
items
381+
)
387382
}
388383

389384
fn emit_static(
@@ -392,8 +387,7 @@ fn emit_static(
392387
trap: &mut TrapFile,
393388
statik: ra_ap_hir_def::StaticId,
394389
visibility: Visibility,
395-
) -> Vec<trap::Label<generated::Item>> {
396-
let mut items = Vec::new();
390+
) -> Option<trap::Label<generated::Item>> {
397391
let type_ = db.value_ty(statik.into());
398392
let type_repr = type_.and_then(|type_| emit_hir_ty(trap, db, type_.skip_binders()));
399393
let name = Some(trap.emit(generated::Name {
@@ -402,7 +396,7 @@ fn emit_static(
402396
}));
403397
let statik = db.static_data(statik);
404398
let visibility = emit_visibility(db, trap, visibility);
405-
items.push(
399+
Some(
406400
trap.emit(generated::Static {
407401
id: trap::TrapId::Star,
408402
name,
@@ -415,8 +409,7 @@ fn emit_static(
415409
is_unsafe: statik.has_unsafe_kw(),
416410
})
417411
.into(),
418-
);
419-
items
412+
)
420413
}
421414

422415
fn emit_generic_param_list(
@@ -517,9 +510,7 @@ fn emit_adt(
517510
trap: &mut TrapFile,
518511
adt_id: ra_ap_hir_def::AdtId,
519512
visibility: Visibility,
520-
) -> Vec<trap::Label<generated::Item>> {
521-
let mut items = Vec::new();
522-
513+
) -> Option<trap::Label<generated::Item>> {
523514
match adt_id {
524515
ra_ap_hir_def::AdtId::StructId(struct_id) => {
525516
let name = Some(trap.emit(generated::Name {
@@ -529,7 +520,7 @@ fn emit_adt(
529520
let field_list = emit_variant_data(trap, db, struct_id.into()).into();
530521
let visibility = emit_visibility(db, trap, visibility);
531522
let generic_param_list = emit_generic_param_list(trap, db, adt_id.into());
532-
items.push(
523+
Some(
533524
trap.emit(generated::Struct {
534525
id: trap::TrapId::Star,
535526
name,
@@ -540,7 +531,7 @@ fn emit_adt(
540531
where_clause: None,
541532
})
542533
.into(),
543-
);
534+
)
544535
}
545536
ra_ap_hir_def::AdtId::EnumId(enum_id) => {
546537
let data = db.enum_variants(enum_id);
@@ -574,7 +565,7 @@ fn emit_adt(
574565
}));
575566
let visibility = emit_visibility(db, trap, visibility);
576567
let generic_param_list = emit_generic_param_list(trap, db, adt_id.into());
577-
items.push(
568+
Some(
578569
trap.emit(generated::Enum {
579570
id: trap::TrapId::Star,
580571
name,
@@ -585,7 +576,7 @@ fn emit_adt(
585576
where_clause: None,
586577
})
587578
.into(),
588-
);
579+
)
589580
}
590581
ra_ap_hir_def::AdtId::UnionId(union_id) => {
591582
let name = Some(trap.emit(generated::Name {
@@ -595,7 +586,7 @@ fn emit_adt(
595586
let struct_field_list = emit_variant_data(trap, db, union_id.into()).into();
596587
let visibility = emit_visibility(db, trap, visibility);
597588
let generic_param_list = emit_generic_param_list(trap, db, adt_id.into());
598-
items.push(
589+
Some(
599590
trap.emit(generated::Union {
600591
id: trap::TrapId::Star,
601592
name,
@@ -606,10 +597,9 @@ fn emit_adt(
606597
where_clause: None,
607598
})
608599
.into(),
609-
);
600+
)
610601
}
611602
}
612-
items
613603
}
614604

615605
fn emit_trait(
@@ -618,8 +608,7 @@ fn emit_trait(
618608
trap: &mut TrapFile,
619609
trait_id: ra_ap_hir_def::TraitId,
620610
visibility: Visibility,
621-
) -> Vec<trap::Label<generated::Item>> {
622-
let mut items = Vec::new();
611+
) -> Option<trap::Label<generated::Item>> {
623612
let trait_items = db.trait_items(trait_id);
624613
let assoc_items: Vec<trap::Label<generated::AssocItem>> = trait_items
625614
.items
@@ -696,7 +685,7 @@ fn emit_trait(
696685
}));
697686
let visibility = emit_visibility(db, trap, visibility);
698687
let generic_param_list = emit_generic_param_list(trap, db, trait_id.into());
699-
items.push(
688+
Some(
700689
trap.emit(generated::Trait {
701690
id: trap::TrapId::Star,
702691
name,
@@ -710,8 +699,7 @@ fn emit_trait(
710699
where_clause: None,
711700
})
712701
.into(),
713-
);
714-
items
702+
)
715703
}
716704

717705
fn emit_module_impls(

0 commit comments

Comments
 (0)