Skip to content

Commit 94a2be9

Browse files
committed
rustc_metadata: reduce repetition
1 parent 69be18d commit 94a2be9

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

Diff for: compiler/rustc_metadata/src/locator.rs

+29-15
Original file line numberDiff line numberDiff line change
@@ -726,22 +726,36 @@ impl<'a> CrateLocator<'a> {
726726
let Some(file) = loc_orig.file_name().and_then(|s| s.to_str()) else {
727727
return Err(CrateError::ExternLocationNotFile(self.crate_name, loc_orig.clone()));
728728
};
729-
if file.starts_with("lib") && (file.ends_with(".rlib") || file.ends_with(".rmeta"))
730-
|| file.starts_with(self.target.dll_prefix.as_ref())
731-
&& file.ends_with(self.target.dll_suffix.as_ref())
732-
{
733-
let loc_canon = loc_canon.clone();
734-
if file.ends_with(".rlib") {
735-
rlibs.insert(loc_canon, PathKind::ExternFlag);
736-
} else if file.ends_with(".rmeta") {
737-
rmetas.insert(loc_canon, PathKind::ExternFlag);
738-
} else {
739-
dylibs.insert(loc_canon, PathKind::ExternFlag);
729+
// FnMut cannot return reference to captured value, so references
730+
// must be taken outside the closure.
731+
let rlibs = &mut rlibs;
732+
let rmetas = &mut rmetas;
733+
let dylibs = &mut dylibs;
734+
let type_via_filename = (|| {
735+
if file.starts_with("lib") {
736+
if file.ends_with(".rlib") {
737+
return Some(rlibs);
738+
}
739+
if file.ends_with(".rmeta") {
740+
return Some(rmetas);
741+
}
742+
}
743+
let dll_prefix = self.target.dll_prefix.as_ref();
744+
let dll_suffix = self.target.dll_suffix.as_ref();
745+
if file.starts_with(dll_prefix) && file.ends_with(dll_suffix) {
746+
return Some(dylibs);
747+
}
748+
None
749+
})();
750+
match type_via_filename {
751+
Some(type_via_filename) => {
752+
type_via_filename.insert(loc_canon.clone(), PathKind::ExternFlag);
753+
}
754+
None => {
755+
self.crate_rejections
756+
.via_filename
757+
.push(CrateMismatch { path: loc_orig.clone(), got: String::new() });
740758
}
741-
} else {
742-
self.crate_rejections
743-
.via_filename
744-
.push(CrateMismatch { path: loc_orig.clone(), got: String::new() });
745759
}
746760
}
747761

0 commit comments

Comments
 (0)