@@ -182,20 +182,22 @@ fn clean_lib(
182
182
( Some ( path) , _) => package_root. join ( & path. 0 ) ,
183
183
( None , Some ( path) ) => path,
184
184
( None , None ) => {
185
- let legacy_path = package_root. join ( "src" ) . join ( format ! ( "{}.rs" , lib. name( ) ) ) ;
185
+ let legacy_path = package_root
186
+ . join ( "src" )
187
+ . join ( format ! ( "{}.rs" , name_or_panic( lib) ) ) ;
186
188
if edition == Edition :: Edition2015 && legacy_path. exists ( ) {
187
189
warnings. push ( format ! (
188
190
"path `{}` was erroneously implicitly accepted for library `{}`,\n \
189
191
please rename the file to `src/lib.rs` or set lib.path in Cargo.toml",
190
192
legacy_path. display( ) ,
191
- lib . name ( )
193
+ name_or_panic ( lib )
192
194
) ) ;
193
195
legacy_path
194
196
} else {
195
197
anyhow:: bail!(
196
198
"can't find library `{}`, \
197
199
rename file to `src/lib.rs` or specify lib.path",
198
- lib . name ( )
200
+ name_or_panic ( lib )
199
201
)
200
202
}
201
203
}
@@ -217,20 +219,20 @@ fn clean_lib(
217
219
{
218
220
anyhow:: bail!( format!(
219
221
"library `{}` cannot set the crate type of both `dylib` and `cdylib`" ,
220
- lib . name ( )
222
+ name_or_panic ( lib )
221
223
) ) ;
222
224
}
223
225
( Some ( kinds) , _, _) if kinds. contains ( & "proc-macro" . to_string ( ) ) => {
224
226
if let Some ( true ) = lib. plugin {
225
227
// This is a warning to retain backwards compatibility.
226
228
warnings. push ( format ! (
227
229
"proc-macro library `{}` should not specify `plugin = true`" ,
228
- lib . name ( )
230
+ name_or_panic ( lib )
229
231
) ) ;
230
232
}
231
233
warnings. push ( format ! (
232
234
"library `{}` should only specify `proc-macro = true` instead of setting `crate-type`" ,
233
- lib . name ( )
235
+ name_or_panic ( lib )
234
236
) ) ;
235
237
if kinds. len ( ) > 1 {
236
238
anyhow:: bail!( "cannot mix `proc-macro` crate type with others" ) ;
@@ -246,7 +248,7 @@ fn clean_lib(
246
248
( None , _, _) => vec ! [ CrateType :: Lib ] ,
247
249
} ;
248
250
249
- let mut target = Target :: lib_target ( & lib . name ( ) , crate_types, path, edition) ;
251
+ let mut target = Target :: lib_target ( name_or_panic ( lib ) , crate_types, path, edition) ;
250
252
configure ( lib, & mut target) ?;
251
253
Ok ( Some ( target) )
252
254
}
@@ -286,7 +288,7 @@ fn clean_bins(
286
288
287
289
validate_target_name ( bin, "binary" , "bin" , warnings) ?;
288
290
289
- let name = bin. name ( ) ;
291
+ let name = name_or_panic ( bin) . to_owned ( ) ;
290
292
291
293
if let Some ( crate_types) = bin. crate_types ( ) {
292
294
if !crate_types. is_empty ( ) {
@@ -321,12 +323,12 @@ fn clean_bins(
321
323
let mut result = Vec :: new ( ) ;
322
324
for bin in & bins {
323
325
let path = target_path ( bin, & inferred, "bin" , package_root, edition, & mut |_| {
324
- if let Some ( legacy_path) = legacy_bin_path ( package_root, & bin . name ( ) , has_lib) {
326
+ if let Some ( legacy_path) = legacy_bin_path ( package_root, name_or_panic ( bin ) , has_lib) {
325
327
warnings. push ( format ! (
326
328
"path `{}` was erroneously implicitly accepted for binary `{}`,\n \
327
329
please set bin.path in Cargo.toml",
328
330
legacy_path. display( ) ,
329
- bin . name ( )
331
+ name_or_panic ( bin )
330
332
) ) ;
331
333
Some ( legacy_path)
332
334
} else {
@@ -339,7 +341,7 @@ fn clean_bins(
339
341
} ;
340
342
341
343
let mut target = Target :: bin_target (
342
- & bin . name ( ) ,
344
+ name_or_panic ( bin ) ,
343
345
bin. filename . clone ( ) ,
344
346
path,
345
347
bin. required_features . clone ( ) ,
@@ -406,7 +408,7 @@ fn clean_examples(
406
408
} ;
407
409
408
410
let mut target = Target :: example_target (
409
- & toml. name ( ) ,
411
+ name_or_panic ( & toml) ,
410
412
crate_types,
411
413
path,
412
414
toml. required_features . clone ( ) ,
@@ -444,8 +446,12 @@ fn clean_tests(
444
446
445
447
let mut result = Vec :: new ( ) ;
446
448
for ( path, toml) in targets {
447
- let mut target =
448
- Target :: test_target ( & toml. name ( ) , path, toml. required_features . clone ( ) , edition) ;
449
+ let mut target = Target :: test_target (
450
+ name_or_panic ( & toml) ,
451
+ path,
452
+ toml. required_features . clone ( ) ,
453
+ edition,
454
+ ) ;
449
455
configure ( & toml, & mut target) ?;
450
456
result. push ( target) ;
451
457
}
@@ -465,14 +471,14 @@ fn clean_benches(
465
471
let targets = {
466
472
let mut legacy_bench_path = |bench : & TomlTarget | {
467
473
let legacy_path = package_root. join ( "src" ) . join ( "bench.rs" ) ;
468
- if !( bench . name ( ) == "bench" && legacy_path. exists ( ) ) {
474
+ if !( name_or_panic ( bench ) == "bench" && legacy_path. exists ( ) ) {
469
475
return None ;
470
476
}
471
477
legacy_warnings. push ( format ! (
472
478
"path `{}` was erroneously implicitly accepted for benchmark `{}`,\n \
473
479
please set bench.path in Cargo.toml",
474
480
legacy_path. display( ) ,
475
- bench . name ( )
481
+ name_or_panic ( bench )
476
482
) ) ;
477
483
Some ( legacy_path)
478
484
} ;
@@ -498,8 +504,12 @@ fn clean_benches(
498
504
499
505
let mut result = Vec :: new ( ) ;
500
506
for ( path, toml) in targets {
501
- let mut target =
502
- Target :: bench_target ( & toml. name ( ) , path, toml. required_features . clone ( ) , edition) ;
507
+ let mut target = Target :: bench_target (
508
+ name_or_panic ( & toml) ,
509
+ path,
510
+ toml. required_features . clone ( ) ,
511
+ edition,
512
+ ) ;
503
513
configure ( & toml, & mut target) ?;
504
514
result. push ( target) ;
505
515
}
@@ -785,8 +795,8 @@ fn validate_target_name(
785
795
/// Will check a list of toml targets, and make sure the target names are unique within a vector.
786
796
fn validate_unique_names ( targets : & [ TomlTarget ] , target_kind : & str ) -> CargoResult < ( ) > {
787
797
let mut seen = HashSet :: new ( ) ;
788
- for name in targets. iter ( ) . map ( |e| e . name ( ) ) {
789
- if !seen. insert ( name. clone ( ) ) {
798
+ for name in targets. iter ( ) . map ( |e| name_or_panic ( e ) ) {
799
+ if !seen. insert ( name) {
790
800
anyhow:: bail!(
791
801
"found duplicate {target_kind} name {name}, \
792
802
but all {target_kind} targets must have a unique name",
@@ -876,7 +886,7 @@ fn target_path_not_found_error_message(
876
886
return [ target_path_file, target_path_subdir] ;
877
887
}
878
888
879
- let target_name = target . name ( ) ;
889
+ let target_name = name_or_panic ( target ) ;
880
890
let commonly_wrong_paths = possible_target_paths ( & target_name, target_kind, true ) ;
881
891
let possible_paths = possible_target_paths ( & target_name, target_kind, false ) ;
882
892
let existing_wrong_path_index = match (
@@ -923,7 +933,7 @@ fn target_path(
923
933
// Should we verify that this path exists here?
924
934
return Ok ( package_root. join ( & path. 0 ) ) ;
925
935
}
926
- let name = target. name ( ) ;
936
+ let name = name_or_panic ( target) . to_owned ( ) ;
927
937
928
938
let mut matching = inferred
929
939
. iter ( )
@@ -956,7 +966,7 @@ fn target_path(
956
966
"\
957
967
cannot infer path for `{}` {}
958
968
Cargo doesn't know which to use because multiple target files found at `{}` and `{}`." ,
959
- target . name ( ) ,
969
+ name_or_panic ( target ) ,
960
970
target_kind,
961
971
p0. strip_prefix( package_root) . unwrap_or( & p0) . display( ) ,
962
972
p1. strip_prefix( package_root) . unwrap_or( & p1) . display( ) ,
@@ -986,11 +996,18 @@ fn maybe_custom_build(build: &Option<StringOrBool>, package_root: &Path) -> Opti
986
996
}
987
997
}
988
998
999
+ fn name_or_panic ( target : & TomlTarget ) -> & str {
1000
+ target
1001
+ . name
1002
+ . as_deref ( )
1003
+ . unwrap_or_else ( || panic ! ( "target name is required" ) )
1004
+ }
1005
+
989
1006
fn validate_proc_macro ( target : & TomlTarget , kind : & str , warnings : & mut Vec < String > ) {
990
1007
if target. proc_macro_raw . is_some ( ) && target. proc_macro_raw2 . is_some ( ) {
991
1008
warn_on_deprecated (
992
1009
"proc-macro" ,
993
- target . name ( ) . as_str ( ) ,
1010
+ name_or_panic ( target ) ,
994
1011
format ! ( "{kind} target" ) . as_str ( ) ,
995
1012
warnings,
996
1013
) ;
@@ -1001,7 +1018,7 @@ fn validate_crate_types(target: &TomlTarget, kind: &str, warnings: &mut Vec<Stri
1001
1018
if target. crate_type . is_some ( ) && target. crate_type2 . is_some ( ) {
1002
1019
warn_on_deprecated (
1003
1020
"crate-type" ,
1004
- target . name ( ) . as_str ( ) ,
1021
+ name_or_panic ( target ) ,
1005
1022
format ! ( "{kind} target" ) . as_str ( ) ,
1006
1023
warnings,
1007
1024
) ;
0 commit comments