Skip to content

Commit 6147bdf

Browse files
authored
Remove unnecessary invocations of .to_string() (#1321)
* remove unnecessary to_string * more to_string removals * fix borrows * fix typo
1 parent f81bbd8 commit 6147bdf

File tree

43 files changed

+128
-133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+128
-133
lines changed

crates/cxx-qt-build/src/dependencies.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ impl Dependency {
4141
/// See also the internals "build system" section of our book.
4242
pub(crate) fn find_all() -> Vec<Dependency> {
4343
std::env::vars_os()
44-
.map(|(var, value)| (var.to_string_lossy().to_string(), value))
45-
.filter(|(var, _)| var.starts_with("DEP_") && var.ends_with("_CXX_QT_MANIFEST_PATH"))
44+
.filter(|(var, _)| {
45+
let var = var.to_string_lossy();
46+
var.starts_with("DEP_") && var.ends_with("_CXX_QT_MANIFEST_PATH")
47+
})
4648
.map(|(_, manifest_path)| {
4749
let manifest_path = PathBuf::from(manifest_path);
4850
let manifest: Manifest = serde_json::from_str(
@@ -54,7 +56,7 @@ impl Dependency {
5456
println!(
5557
"cxx-qt-build: Discovered dependency `{}` at: {}",
5658
manifest.name,
57-
manifest_path.to_string_lossy()
59+
manifest_path.display(),
5860
);
5961
Dependency {
6062
path: manifest_path.parent().unwrap().to_owned(),

crates/cxx-qt-build/src/interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Interface {
4545
mut self,
4646
prefixes: impl IntoIterator<Item = &'a str>,
4747
) -> Self {
48-
let prefixes = prefixes.into_iter().map(|item| item.to_string()).collect();
48+
let prefixes = prefixes.into_iter().map(str::to_owned).collect();
4949

5050
self.exported_include_prefixes = prefixes;
5151
self
@@ -99,7 +99,7 @@ impl Interface {
9999
std::fs::write(&manifest_path, manifest_json).expect("Failed to write manifest.json!");
100100
println!(
101101
"cargo::metadata=CXX_QT_MANIFEST_PATH={}",
102-
manifest_path.to_string_lossy()
102+
manifest_path.display()
103103
);
104104
}
105105
}

crates/cxx-qt-build/src/lib.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl GeneratedCpp {
101101
// Remove the .rs extension
102102
.with_extension("")
103103
.to_string_lossy()
104-
.to_string();
104+
.into_owned();
105105

106106
// The include path we inject needs any prefix (eg the crate name) too
107107
let include_ident = format!("{include_prefix}/{file_ident}");
@@ -280,7 +280,7 @@ fn generate_cxxqt_cpp_files(
280280
let mut generated_file_paths: Vec<GeneratedCppFilePaths> = Vec::with_capacity(rs_source.len());
281281
for rs_path in rs_source {
282282
let path = manifest_dir.join(rs_path);
283-
println!("cargo::rerun-if-changed={}", path.to_string_lossy());
283+
println!("cargo::rerun-if-changed={}", path.display());
284284

285285
let generated_code = match GeneratedCpp::new(&path, rs_path, include_prefix) {
286286
Ok(v) => v,
@@ -416,7 +416,7 @@ impl CxxQtBuilder {
416416
qml_modules: vec![],
417417
cc_builder: cc::Build::new(),
418418
include_prefix: crate_name(),
419-
crate_include_root: Some("".to_owned()),
419+
crate_include_root: Some(String::new()),
420420
additional_include_dirs: vec![],
421421
}
422422
}
@@ -641,7 +641,7 @@ impl CxxQtBuilder {
641641
// Find the Qt version and tell the Rust compiler
642642
// this allows us to have conditional Rust code
643643
CxxQtBuilder::define_cfg_variable(
644-
"cxxqt_qt_version_major".to_string(),
644+
"cxxqt_qt_version_major".to_owned(),
645645
Some(version.major.to_string().as_str()),
646646
);
647647

@@ -663,7 +663,7 @@ impl CxxQtBuilder {
663663
for minor in 0..=version.minor {
664664
let qt_version_at_least =
665665
format!("cxxqt_qt_version_at_least_{}_{}", version.major, minor);
666-
CxxQtBuilder::define_cfg_variable(qt_version_at_least.to_string(), None);
666+
CxxQtBuilder::define_cfg_variable(qt_version_at_least, None);
667667
}
668668

669669
// We don't support Qt < 5
@@ -773,15 +773,12 @@ impl CxxQtBuilder {
773773
std::fs::create_dir_all(directory).unwrap_or_else(|_| {
774774
panic!(
775775
"Could not create directory for exporting object file: {}",
776-
export_path.to_string_lossy()
776+
export_path.display()
777777
)
778778
});
779779
}
780780
std::fs::copy(obj_file, &export_path).unwrap_or_else(|_| {
781-
panic!(
782-
"Failed to export object file to {}!",
783-
export_path.to_string_lossy()
784-
)
781+
panic!("Failed to export object file to {}!", export_path.display())
785782
});
786783
} else {
787784
panic!(
@@ -821,7 +818,7 @@ impl CxxQtBuilder {
821818
.iter()
822819
.map(|file| {
823820
if let Some(parent) = file.parent() {
824-
parent.to_string_lossy().to_string()
821+
parent.to_string_lossy().into_owned()
825822
} else {
826823
// Fallback to an empty string if there is no parent path
827824
String::new()

crates/cxx-qt-build/src/qml_modules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn collect_pathbuf_vec(asref: &[impl AsRef<Path>]) -> Vec<PathBuf> {
7171
impl<A: AsRef<Path>, B: AsRef<Path>> From<QmlModule<'_, A, B>> for OwningQmlModule {
7272
fn from(other: QmlModule<'_, A, B>) -> Self {
7373
OwningQmlModule {
74-
uri: other.uri.to_string(),
74+
uri: other.uri.to_owned(),
7575
version_major: other.version_major,
7676
version_minor: other.version_minor,
7777
rust_files: collect_pathbuf_vec(other.rust_files),

crates/cxx-qt-build/src/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ pub(crate) fn best_effort_copy_headers(
2121
};
2222
// Ensure that we rebuild if there are files added/removed
2323
if emit_rerun_if_changed {
24-
println!("cargo::rerun-if-changed={}", src.to_string_lossy());
24+
println!("cargo::rerun-if-changed={}", src.display());
2525
}
2626

2727
while let Some(Ok(entry)) = entries.next() {
2828
let file_name = entry.file_name();
29-
if file_name.to_string_lossy().starts_with('.') {
29+
if file_name.as_encoded_bytes().starts_with(b".") {
3030
continue;
3131
}
3232
match entry.file_type() {
@@ -50,7 +50,7 @@ pub(crate) fn best_effort_copy_headers(
5050

5151
// Ensure that we rebuild if there are changes
5252
if emit_rerun_if_changed {
53-
println!("cargo::rerun-if-changed={}", src.to_string_lossy());
53+
println!("cargo::rerun-if-changed={}", src.display());
5454
}
5555

5656
dst_created = true;

crates/cxx-qt-gen/src/generator/cpp/constructor.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub fn generate(
124124
.collect::<Vec<_>>()
125125
.join(", ")
126126
} else {
127-
"".to_string()
127+
String::new()
128128
};
129129
// For each constructor defined in CXX-Qt we need a pair of one public and one private
130130
// constructor.
@@ -170,7 +170,7 @@ mod tests {
170170
GeneratedCppQObject {
171171
name: Name::mock("MyObject"),
172172
rust_struct: Name::mock("MyObjectRust"),
173-
namespace_internals: "rust".to_string(),
173+
namespace_internals: "rust".to_owned(),
174174
blocks: GeneratedCppQObjectBlocks::default(),
175175
has_qobject_macro: true,
176176
}
@@ -199,7 +199,7 @@ mod tests {
199199
&qobject_for_testing(),
200200
&[],
201201
"BaseClass".to_owned(),
202-
&["member1(1)".to_string(), "member2{ 2 }".to_string()],
202+
&["member1(1)".to_owned(), "member2{ 2 }".to_owned()],
203203
&type_names_with_qobject(),
204204
)
205205
.unwrap();
@@ -209,7 +209,7 @@ mod tests {
209209
assert_eq!(
210210
blocks.methods,
211211
vec![CppFragment::Pair {
212-
header: "explicit MyObject(QObject* parent = nullptr);".to_string(),
212+
header: "explicit MyObject(QObject* parent = nullptr);".to_owned(),
213213
source: formatdoc!(
214214
"
215215
MyObject::MyObject(QObject* parent)
@@ -239,7 +239,7 @@ mod tests {
239239
assert_eq!(
240240
blocks.methods,
241241
vec![CppFragment::Pair {
242-
header: "explicit MyObject(QObject* parent = nullptr);".to_string(),
242+
header: "explicit MyObject(QObject* parent = nullptr);".to_owned(),
243243
source: formatdoc!(
244244
"
245245
MyObject::MyObject(QObject* parent)
@@ -270,7 +270,7 @@ mod tests {
270270
assert_eq!(
271271
blocks.methods,
272272
vec![CppFragment::Pair {
273-
header: "explicit MyObject();".to_string(),
273+
header: "explicit MyObject();".to_owned(),
274274
source: formatdoc!(
275275
"
276276
MyObject::MyObject()
@@ -301,7 +301,7 @@ mod tests {
301301
assert_eq!(
302302
blocks.private_methods,
303303
vec![CppFragment::Pair {
304-
header: "explicit MyObject(::rust::CxxQtConstructorArguments0&& args);".to_string(),
304+
header: "explicit MyObject(::rust::CxxQtConstructorArguments0&& args);".to_owned(),
305305
source: formatdoc!(
306306
"
307307
MyObject::MyObject(::rust::CxxQtConstructorArguments0&& args)
@@ -317,7 +317,7 @@ mod tests {
317317
assert_eq!(
318318
blocks.methods,
319319
vec![CppFragment::Pair {
320-
header: "explicit MyObject(::std::int32_t arg0, QObject* arg1);".to_string(),
320+
header: "explicit MyObject(::std::int32_t arg0, QObject* arg1);".to_owned(),
321321
source: formatdoc!(
322322
"
323323
MyObject::MyObject(::std::int32_t arg0, QObject* arg1)
@@ -342,7 +342,7 @@ mod tests {
342342
..mock_constructor()
343343
}],
344344
"BaseClass".to_owned(),
345-
&["initializer".to_string()],
345+
&["initializer".to_owned()],
346346
&type_names_with_qobject(),
347347
)
348348
.unwrap();
@@ -351,7 +351,7 @@ mod tests {
351351
assert_eq!(
352352
blocks.methods,
353353
vec![CppFragment::Pair {
354-
header: "explicit MyObject(::std::int8_t arg0, ::std::int16_t arg1);".to_string(),
354+
header: "explicit MyObject(::std::int8_t arg0, ::std::int16_t arg1);".to_owned(),
355355
source: formatdoc!(
356356
"
357357
MyObject::MyObject(::std::int8_t arg0, ::std::int16_t arg1)
@@ -364,7 +364,7 @@ mod tests {
364364
assert_eq!(
365365
blocks.private_methods,
366366
vec![CppFragment::Pair {
367-
header: "explicit MyObject(::rust::CxxQtConstructorArguments0&& args);".to_string(),
367+
header: "explicit MyObject(::rust::CxxQtConstructorArguments0&& args);".to_owned(),
368368
source: formatdoc!(
369369
"
370370
MyObject::MyObject(::rust::CxxQtConstructorArguments0&& args)
@@ -396,7 +396,7 @@ mod tests {
396396
},
397397
],
398398
"BaseClass".to_owned(),
399-
&["initializer".to_string()],
399+
&["initializer".to_owned()],
400400
&type_names_with_qobject(),
401401
)
402402
.unwrap();
@@ -407,7 +407,7 @@ mod tests {
407407
blocks.methods,
408408
vec![
409409
CppFragment::Pair {
410-
header: "explicit MyObject();".to_string(),
410+
header: "explicit MyObject();".to_owned(),
411411
source: formatdoc!(
412412
"
413413
MyObject::MyObject()
@@ -417,7 +417,7 @@ mod tests {
417417
),
418418
},
419419
CppFragment::Pair {
420-
header: "explicit MyObject(QObject* arg0);".to_string(),
420+
header: "explicit MyObject(QObject* arg0);".to_owned(),
421421
source: formatdoc! {
422422
"
423423
MyObject::MyObject(QObject* arg0)
@@ -434,7 +434,7 @@ mod tests {
434434
vec![
435435
CppFragment::Pair {
436436
header: "explicit MyObject(::rust::CxxQtConstructorArguments0&& args);"
437-
.to_string(),
437+
.to_owned(),
438438
source: formatdoc!(
439439
"
440440
MyObject::MyObject(::rust::CxxQtConstructorArguments0&& args)
@@ -449,7 +449,7 @@ mod tests {
449449
},
450450
CppFragment::Pair {
451451
header: "explicit MyObject(::rust::CxxQtConstructorArguments1&& args);"
452-
.to_string(),
452+
.to_owned(),
453453
source: formatdoc!(
454454
"
455455
MyObject::MyObject(::rust::CxxQtConstructorArguments1&& args)

crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn generate(
5959
let base_class = if let Some(ident) = &qobject.base_class {
6060
type_names.lookup(ident)?.cxx_qualified()
6161
} else {
62-
"QObject".to_string()
62+
"QObject".to_owned()
6363
};
6464
generated.base_classes.push(base_class);
6565
}

crates/cxx-qt-gen/src/generator/cpp/inherit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn generate(
4747
mutability = if method.mutable { "" } else { " const" },
4848
func_ident = method.name.cxx_unqualified(),
4949
wrapper_ident = method.wrapper_ident(),
50-
return_type = return_type.unwrap_or_else(|| "void".to_string()),
50+
return_type = return_type.unwrap_or_else(|| "void".to_owned()),
5151
base_class = base_class
5252
}));
5353
}

crates/cxx-qt-gen/src/generator/cpp/qenum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn generate_declaration(
4646
let is_standalone = qenum.qobject.is_none();
4747
if is_standalone {
4848
// required for Q_NAMESPACE and Q_ENUM_NS if we're not on a QObject
49-
includes.insert("#include <QtCore/QObject>".to_string());
49+
includes.insert("#include <QtCore/QObject>".to_owned());
5050
}
5151

5252
let enum_definition = generate_definition(qenum).indented(2);
@@ -88,7 +88,7 @@ pub fn generate_on_qobject<'a>(
8888
qualified_name.insert_str(0, "::");
8989
}
9090

91-
generated.includes.insert("#include <cstdint>".to_string());
91+
generated.includes.insert("#include <cstdint>".to_owned());
9292
let enum_definition = generate_definition(qenum);
9393
generated.metaobjects.push(formatdoc! {r#"
9494
#ifdef Q_MOC_RUN

crates/cxx-qt-gen/src/generator/cpp/qnamespace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use crate::{parser::qnamespace::ParsedQNamespace, writer::cpp::namespaced};
1111

1212
/// Generate the declaration of the namespace, including the Q_NAMESPACE macro.
1313
pub fn generate(qnamespace: &ParsedQNamespace, includes: &mut BTreeSet<String>) -> String {
14-
includes.insert("#include <QtCore/QObject>".to_string());
14+
includes.insert("#include <QtCore/QObject>".to_owned());
1515
let mut result = "Q_NAMESPACE".to_owned();
1616
if qnamespace.qml_element {
17-
includes.insert("#include <QtQml/QQmlEngine>".to_string());
17+
includes.insert("#include <QtQml/QQmlEngine>".to_owned());
1818
result = formatdoc! { r#"
1919
{result}
2020
QML_ELEMENT"#};

0 commit comments

Comments
 (0)