Auto merge of #52805 - ljedrz:format_str_literal, r=petrochenkov
Don't format!() string literals
Prefer `to_string()` to `format!()` take 2, this time targetting string literals. In some cases (`&format!("...")` -> `"..."`) also removes allocations. Occurences of `format!("")` are changed to `String::new()`.
This commit is contained in:
commit
7bbcd005b3
42 changed files with 168 additions and 172 deletions
|
|
@ -108,37 +108,37 @@ pub struct DecodedBytecode<'a> {
|
|||
impl<'a> DecodedBytecode<'a> {
|
||||
pub fn new(data: &'a [u8]) -> Result<DecodedBytecode<'a>, String> {
|
||||
if !data.starts_with(RLIB_BYTECODE_OBJECT_MAGIC) {
|
||||
return Err(format!("magic bytecode prefix not found"))
|
||||
return Err("magic bytecode prefix not found".to_string())
|
||||
}
|
||||
let data = &data[RLIB_BYTECODE_OBJECT_MAGIC.len()..];
|
||||
if !data.starts_with(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]) {
|
||||
return Err(format!("wrong version prefix found in bytecode"))
|
||||
return Err("wrong version prefix found in bytecode".to_string())
|
||||
}
|
||||
let data = &data[4..];
|
||||
if data.len() < 4 {
|
||||
return Err(format!("bytecode corrupted"))
|
||||
return Err("bytecode corrupted".to_string())
|
||||
}
|
||||
let identifier_len = unsafe {
|
||||
u32::from_le(ptr::read_unaligned(data.as_ptr() as *const u32)) as usize
|
||||
};
|
||||
let data = &data[4..];
|
||||
if data.len() < identifier_len {
|
||||
return Err(format!("bytecode corrupted"))
|
||||
return Err("bytecode corrupted".to_string())
|
||||
}
|
||||
let identifier = match str::from_utf8(&data[..identifier_len]) {
|
||||
Ok(s) => s,
|
||||
Err(_) => return Err(format!("bytecode corrupted"))
|
||||
Err(_) => return Err("bytecode corrupted".to_string())
|
||||
};
|
||||
let data = &data[identifier_len..];
|
||||
if data.len() < 8 {
|
||||
return Err(format!("bytecode corrupted"))
|
||||
return Err("bytecode corrupted".to_string())
|
||||
}
|
||||
let bytecode_len = unsafe {
|
||||
u64::from_le(ptr::read_unaligned(data.as_ptr() as *const u64)) as usize
|
||||
};
|
||||
let data = &data[8..];
|
||||
if data.len() < bytecode_len {
|
||||
return Err(format!("bytecode corrupted"))
|
||||
return Err("bytecode corrupted".to_string())
|
||||
}
|
||||
let encoded_bytecode = &data[..bytecode_len];
|
||||
|
||||
|
|
|
|||
|
|
@ -261,14 +261,14 @@ pub(crate) fn each_linked_rlib(sess: &Session,
|
|||
.or_else(|| fmts.get(&config::CrateTypeProcMacro));
|
||||
let fmts = match fmts {
|
||||
Some(f) => f,
|
||||
None => return Err(format!("could not find formats for rlibs"))
|
||||
None => return Err("could not find formats for rlibs".to_string())
|
||||
};
|
||||
for &(cnum, ref path) in crates {
|
||||
match fmts.get(cnum.as_usize() - 1) {
|
||||
Some(&Linkage::NotLinked) |
|
||||
Some(&Linkage::IncludedFromDylib) => continue,
|
||||
Some(_) => {}
|
||||
None => return Err(format!("could not find formats for rlibs"))
|
||||
None => return Err("could not find formats for rlibs".to_string())
|
||||
}
|
||||
let name = &info.crate_name[&cnum];
|
||||
let path = match *path {
|
||||
|
|
|
|||
|
|
@ -206,7 +206,7 @@ impl<'a> GccLinker<'a> {
|
|||
match self.sess.opts.cg.lto {
|
||||
config::Lto::Thin |
|
||||
config::Lto::ThinLocal => {
|
||||
self.linker_arg(&format!("-plugin-opt=thin"));
|
||||
self.linker_arg("-plugin-opt=thin");
|
||||
}
|
||||
config::Lto::Fat |
|
||||
config::Lto::Yes |
|
||||
|
|
|
|||
|
|
@ -421,7 +421,7 @@ fn thin_lto(diag_handler: &Handler,
|
|||
symbol_white_list.len() as u32,
|
||||
);
|
||||
if data.is_null() {
|
||||
let msg = format!("failed to prepare thin LTO context");
|
||||
let msg = "failed to prepare thin LTO context".to_string();
|
||||
return Err(write::llvm_err(&diag_handler, msg))
|
||||
}
|
||||
let data = ThinData(data);
|
||||
|
|
@ -647,7 +647,7 @@ impl ThinModule {
|
|||
self.shared.module_names[self.idx].as_ptr(),
|
||||
);
|
||||
if llmod.is_null() {
|
||||
let msg = format!("failed to parse bitcode for thin LTO module");
|
||||
let msg = "failed to parse bitcode for thin LTO module".to_string();
|
||||
return Err(write::llvm_err(&diag_handler, msg));
|
||||
}
|
||||
let module = ModuleCodegen {
|
||||
|
|
@ -670,7 +670,7 @@ impl ThinModule {
|
|||
let mut cu2 = ptr::null_mut();
|
||||
llvm::LLVMRustThinLTOGetDICompileUnit(llmod, &mut cu1, &mut cu2);
|
||||
if !cu2.is_null() {
|
||||
let msg = format!("multiple source DICompileUnits found");
|
||||
let msg = "multiple source DICompileUnits found".to_string();
|
||||
return Err(write::llvm_err(&diag_handler, msg))
|
||||
}
|
||||
|
||||
|
|
@ -691,25 +691,25 @@ impl ThinModule {
|
|||
// You can find some more comments about these functions in the LLVM
|
||||
// bindings we've got (currently `PassWrapper.cpp`)
|
||||
if !llvm::LLVMRustPrepareThinLTORename(self.shared.data.0, llmod) {
|
||||
let msg = format!("failed to prepare thin LTO module");
|
||||
let msg = "failed to prepare thin LTO module".to_string();
|
||||
return Err(write::llvm_err(&diag_handler, msg))
|
||||
}
|
||||
cgcx.save_temp_bitcode(&module, "thin-lto-after-rename");
|
||||
timeline.record("rename");
|
||||
if !llvm::LLVMRustPrepareThinLTOResolveWeak(self.shared.data.0, llmod) {
|
||||
let msg = format!("failed to prepare thin LTO module");
|
||||
let msg = "failed to prepare thin LTO module".to_string();
|
||||
return Err(write::llvm_err(&diag_handler, msg))
|
||||
}
|
||||
cgcx.save_temp_bitcode(&module, "thin-lto-after-resolve");
|
||||
timeline.record("resolve");
|
||||
if !llvm::LLVMRustPrepareThinLTOInternalize(self.shared.data.0, llmod) {
|
||||
let msg = format!("failed to prepare thin LTO module");
|
||||
let msg = "failed to prepare thin LTO module".to_string();
|
||||
return Err(write::llvm_err(&diag_handler, msg))
|
||||
}
|
||||
cgcx.save_temp_bitcode(&module, "thin-lto-after-internalize");
|
||||
timeline.record("internalize");
|
||||
if !llvm::LLVMRustPrepareThinLTOImport(self.shared.data.0, llmod) {
|
||||
let msg = format!("failed to prepare thin LTO module");
|
||||
let msg = "failed to prepare thin LTO module".to_string();
|
||||
return Err(write::llvm_err(&diag_handler, msg))
|
||||
}
|
||||
cgcx.save_temp_bitcode(&module, "thin-lto-after-import");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue