@@ -619,24 +619,16 @@ fn generate_targets<'a>(
619
619
} => {
620
620
if lib {
621
621
let mut libs = Vec :: new ( ) ;
622
- for pkg in packages {
623
- for target in pkg. targets ( ) . iter ( ) . filter ( |t| t. is_lib ( ) ) {
624
- if build_config. mode == CompileMode :: Doctest && !target. doctestable ( ) {
625
- ws. config ( )
626
- . shell ( )
627
- . warn ( format ! (
628
- "doc tests are not supported for crate type(s) `{}` in package `{}`" ,
629
- target. rustc_crate_types( ) . join( ", " ) ,
630
- pkg. name( )
631
- ) ) ?;
632
- } else {
633
- libs. push ( Proposal {
634
- pkg,
635
- target,
636
- requires_features : false ,
637
- mode : build_config. mode ,
638
- } ) ;
639
- }
622
+ for proposal in filter_targets ( packages, Target :: is_lib, false , build_config. mode ) {
623
+ let Proposal { target, pkg, .. } = proposal;
624
+ if build_config. mode == CompileMode :: Doctest && !target. doctestable ( ) {
625
+ ws. config ( ) . shell ( ) . warn ( format ! (
626
+ "doc tests are not supported for crate type(s) `{}` in package `{}`" ,
627
+ target. rustc_crate_types( ) . join( ", " ) ,
628
+ pkg. name( )
629
+ ) ) ?;
630
+ } else {
631
+ libs. push ( proposal)
640
632
}
641
633
}
642
634
if !all_targets && libs. is_empty ( ) {
@@ -655,7 +647,7 @@ fn generate_targets<'a>(
655
647
656
648
// If --tests was specified, add all targets that would be
657
649
// generated by `cargo test`.
658
- let test_filter = match * tests {
650
+ let test_filter = match tests {
659
651
FilterRule :: All => Target :: tested,
660
652
FilterRule :: Just ( _) => Target :: is_test,
661
653
} ;
@@ -666,7 +658,7 @@ fn generate_targets<'a>(
666
658
} ;
667
659
// If --benches was specified, add all targets that would be
668
660
// generated by `cargo bench`.
669
- let bench_filter = match * benches {
661
+ let bench_filter = match benches {
670
662
FilterRule :: All => Target :: benched,
671
663
FilterRule :: Just ( _) => Target :: is_bench,
672
664
} ;
@@ -795,36 +787,22 @@ fn filter_default_targets(targets: &[Target], mode: CompileMode) -> Vec<&Target>
795
787
}
796
788
}
797
789
798
- /// Returns a list of targets based on command-line target selection flags.
799
- /// The return value is a list of `(Package, Target, bool, CompileMode)`
800
- /// tuples. The `bool` value indicates whether or not all required features
801
- /// *must* be present.
790
+ /// Returns a list of proposed targets based on command-line target selection flags.
802
791
fn list_rule_targets < ' a > (
803
792
packages : & [ & ' a Package ] ,
804
793
rule : & FilterRule ,
805
794
target_desc : & ' static str ,
806
795
is_expected_kind : fn ( & Target ) -> bool ,
807
796
mode : CompileMode ,
808
797
) -> CargoResult < Vec < Proposal < ' a > > > {
809
- let mut result = Vec :: new ( ) ;
810
- match * rule {
798
+ let mut proposals = Vec :: new ( ) ;
799
+ match rule {
811
800
FilterRule :: All => {
812
- for pkg in packages {
813
- for target in pkg. targets ( ) {
814
- if is_expected_kind ( target) {
815
- result. push ( Proposal {
816
- pkg,
817
- target,
818
- requires_features : false ,
819
- mode,
820
- } ) ;
821
- }
822
- }
823
- }
801
+ proposals. extend ( filter_targets ( packages, is_expected_kind, false , mode) )
824
802
}
825
- FilterRule :: Just ( ref names) => {
803
+ FilterRule :: Just ( names) => {
826
804
for name in names {
827
- result . extend ( find_named_targets (
805
+ proposals . extend ( find_named_targets (
828
806
packages,
829
807
name,
830
808
target_desc,
@@ -834,7 +812,7 @@ fn list_rule_targets<'a>(
834
812
}
835
813
}
836
814
}
837
- Ok ( result )
815
+ Ok ( proposals )
838
816
}
839
817
840
818
/// Find the targets for a specifically named target.
@@ -845,20 +823,9 @@ fn find_named_targets<'a>(
845
823
is_expected_kind : fn ( & Target ) -> bool ,
846
824
mode : CompileMode ,
847
825
) -> CargoResult < Vec < Proposal < ' a > > > {
848
- let mut result = Vec :: new ( ) ;
849
- for pkg in packages {
850
- for target in pkg. targets ( ) {
851
- if target. name ( ) == target_name && is_expected_kind ( target) {
852
- result. push ( Proposal {
853
- pkg,
854
- target,
855
- requires_features : true ,
856
- mode,
857
- } ) ;
858
- }
859
- }
860
- }
861
- if result. is_empty ( ) {
826
+ let filter = |t : & Target | t. name ( ) == target_name && is_expected_kind ( t) ;
827
+ let proposals = filter_targets ( packages, filter, true , mode) ;
828
+ if proposals. is_empty ( ) {
862
829
let suggestion = packages
863
830
. iter ( )
864
831
. flat_map ( |pkg| {
@@ -880,5 +847,25 @@ fn find_named_targets<'a>(
880
847
None => failure:: bail!( "no {} target named `{}`" , target_desc, target_name) ,
881
848
}
882
849
}
883
- Ok ( result)
850
+ Ok ( proposals)
851
+ }
852
+
853
+ fn filter_targets < ' a > (
854
+ packages : & [ & ' a Package ] ,
855
+ predicate : impl Fn ( & Target ) -> bool ,
856
+ requires_features : bool ,
857
+ mode : CompileMode ,
858
+ ) -> Vec < Proposal < ' a > > {
859
+ let mut proposals = Vec :: new ( ) ;
860
+ for pkg in packages {
861
+ for target in pkg. targets ( ) . iter ( ) . filter ( |t| predicate ( t) ) {
862
+ proposals. push ( Proposal {
863
+ pkg,
864
+ target,
865
+ requires_features,
866
+ mode,
867
+ } ) ;
868
+ }
869
+ }
870
+ proposals
884
871
}
0 commit comments