@@ -182,20 +182,22 @@ fn clean_lib(
182182 ( Some ( path) , _) => package_root. join ( & path. 0 ) ,
183183 ( None , Some ( path) ) => path,
184184 ( 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) ) ) ;
186188 if edition == Edition :: Edition2015 && legacy_path. exists ( ) {
187189 warnings. push ( format ! (
188190 "path `{}` was erroneously implicitly accepted for library `{}`,\n \
189191 please rename the file to `src/lib.rs` or set lib.path in Cargo.toml",
190192 legacy_path. display( ) ,
191- lib . name ( )
193+ name_or_panic ( lib )
192194 ) ) ;
193195 legacy_path
194196 } else {
195197 anyhow:: bail!(
196198 "can't find library `{}`, \
197199 rename file to `src/lib.rs` or specify lib.path",
198- lib . name ( )
200+ name_or_panic ( lib )
199201 )
200202 }
201203 }
@@ -217,20 +219,20 @@ fn clean_lib(
217219 {
218220 anyhow:: bail!( format!(
219221 "library `{}` cannot set the crate type of both `dylib` and `cdylib`" ,
220- lib . name ( )
222+ name_or_panic ( lib )
221223 ) ) ;
222224 }
223225 ( Some ( kinds) , _, _) if kinds. contains ( & "proc-macro" . to_string ( ) ) => {
224226 if let Some ( true ) = lib. plugin {
225227 // This is a warning to retain backwards compatibility.
226228 warnings. push ( format ! (
227229 "proc-macro library `{}` should not specify `plugin = true`" ,
228- lib . name ( )
230+ name_or_panic ( lib )
229231 ) ) ;
230232 }
231233 warnings. push ( format ! (
232234 "library `{}` should only specify `proc-macro = true` instead of setting `crate-type`" ,
233- lib . name ( )
235+ name_or_panic ( lib )
234236 ) ) ;
235237 if kinds. len ( ) > 1 {
236238 anyhow:: bail!( "cannot mix `proc-macro` crate type with others" ) ;
@@ -246,7 +248,7 @@ fn clean_lib(
246248 ( None , _, _) => vec ! [ CrateType :: Lib ] ,
247249 } ;
248250
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) ;
250252 configure ( lib, & mut target) ?;
251253 Ok ( Some ( target) )
252254}
@@ -286,7 +288,7 @@ fn clean_bins(
286288
287289 validate_target_name ( bin, "binary" , "bin" , warnings) ?;
288290
289- let name = bin. name ( ) ;
291+ let name = name_or_panic ( bin) . to_owned ( ) ;
290292
291293 if let Some ( crate_types) = bin. crate_types ( ) {
292294 if !crate_types. is_empty ( ) {
@@ -321,12 +323,12 @@ fn clean_bins(
321323 let mut result = Vec :: new ( ) ;
322324 for bin in & bins {
323325 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) {
325327 warnings. push ( format ! (
326328 "path `{}` was erroneously implicitly accepted for binary `{}`,\n \
327329 please set bin.path in Cargo.toml",
328330 legacy_path. display( ) ,
329- bin . name ( )
331+ name_or_panic ( bin )
330332 ) ) ;
331333 Some ( legacy_path)
332334 } else {
@@ -339,7 +341,7 @@ fn clean_bins(
339341 } ;
340342
341343 let mut target = Target :: bin_target (
342- & bin . name ( ) ,
344+ name_or_panic ( bin ) ,
343345 bin. filename . clone ( ) ,
344346 path,
345347 bin. required_features . clone ( ) ,
@@ -406,7 +408,7 @@ fn clean_examples(
406408 } ;
407409
408410 let mut target = Target :: example_target (
409- & toml. name ( ) ,
411+ name_or_panic ( & toml) ,
410412 crate_types,
411413 path,
412414 toml. required_features . clone ( ) ,
@@ -444,8 +446,12 @@ fn clean_tests(
444446
445447 let mut result = Vec :: new ( ) ;
446448 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+ ) ;
449455 configure ( & toml, & mut target) ?;
450456 result. push ( target) ;
451457 }
@@ -465,14 +471,14 @@ fn clean_benches(
465471 let targets = {
466472 let mut legacy_bench_path = |bench : & TomlTarget | {
467473 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 ( ) ) {
469475 return None ;
470476 }
471477 legacy_warnings. push ( format ! (
472478 "path `{}` was erroneously implicitly accepted for benchmark `{}`,\n \
473479 please set bench.path in Cargo.toml",
474480 legacy_path. display( ) ,
475- bench . name ( )
481+ name_or_panic ( bench )
476482 ) ) ;
477483 Some ( legacy_path)
478484 } ;
@@ -498,8 +504,12 @@ fn clean_benches(
498504
499505 let mut result = Vec :: new ( ) ;
500506 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+ ) ;
503513 configure ( & toml, & mut target) ?;
504514 result. push ( target) ;
505515 }
@@ -785,8 +795,8 @@ fn validate_target_name(
785795/// Will check a list of toml targets, and make sure the target names are unique within a vector.
786796fn validate_unique_names ( targets : & [ TomlTarget ] , target_kind : & str ) -> CargoResult < ( ) > {
787797 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) {
790800 anyhow:: bail!(
791801 "found duplicate {target_kind} name {name}, \
792802 but all {target_kind} targets must have a unique name",
@@ -876,7 +886,7 @@ fn target_path_not_found_error_message(
876886 return [ target_path_file, target_path_subdir] ;
877887 }
878888
879- let target_name = target . name ( ) ;
889+ let target_name = name_or_panic ( target ) ;
880890 let commonly_wrong_paths = possible_target_paths ( & target_name, target_kind, true ) ;
881891 let possible_paths = possible_target_paths ( & target_name, target_kind, false ) ;
882892 let existing_wrong_path_index = match (
@@ -923,7 +933,7 @@ fn target_path(
923933 // Should we verify that this path exists here?
924934 return Ok ( package_root. join ( & path. 0 ) ) ;
925935 }
926- let name = target. name ( ) ;
936+ let name = name_or_panic ( target) . to_owned ( ) ;
927937
928938 let mut matching = inferred
929939 . iter ( )
@@ -956,7 +966,7 @@ fn target_path(
956966 "\
957967 cannot infer path for `{}` {}
958968Cargo doesn't know which to use because multiple target files found at `{}` and `{}`." ,
959- target . name ( ) ,
969+ name_or_panic ( target ) ,
960970 target_kind,
961971 p0. strip_prefix( package_root) . unwrap_or( & p0) . display( ) ,
962972 p1. strip_prefix( package_root) . unwrap_or( & p1) . display( ) ,
@@ -986,11 +996,18 @@ fn maybe_custom_build(build: &Option<StringOrBool>, package_root: &Path) -> Opti
986996 }
987997}
988998
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+
9891006fn validate_proc_macro ( target : & TomlTarget , kind : & str , warnings : & mut Vec < String > ) {
9901007 if target. proc_macro_raw . is_some ( ) && target. proc_macro_raw2 . is_some ( ) {
9911008 warn_on_deprecated (
9921009 "proc-macro" ,
993- target . name ( ) . as_str ( ) ,
1010+ name_or_panic ( target ) ,
9941011 format ! ( "{kind} target" ) . as_str ( ) ,
9951012 warnings,
9961013 ) ;
@@ -1001,7 +1018,7 @@ fn validate_crate_types(target: &TomlTarget, kind: &str, warnings: &mut Vec<Stri
10011018 if target. crate_type . is_some ( ) && target. crate_type2 . is_some ( ) {
10021019 warn_on_deprecated (
10031020 "crate-type" ,
1004- target . name ( ) . as_str ( ) ,
1021+ name_or_panic ( target ) ,
10051022 format ! ( "{kind} target" ) . as_str ( ) ,
10061023 warnings,
10071024 ) ;
0 commit comments