Skip to content

Commit 1bf8841

Browse files
Update all tests to comply with clippy::manual_empty_string_creations
1 parent 80826c3 commit 1bf8841

23 files changed

+40
-38
lines changed

clippy_dev/src/new_lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn to_camel_case(name: &str) -> String {
155155
name.split('_')
156156
.map(|s| {
157157
if s.is_empty() {
158-
String::from("")
158+
String::new()
159159
} else {
160160
[&s[0..1].to_uppercase(), &s[1..]].concat()
161161
}

clippy_lints/src/manual_async_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ fn suggested_ret(cx: &LateContext<'_>, output: &Ty<'_>) -> Option<(&'static str,
192192
match output.kind {
193193
TyKind::Tup(tys) if tys.is_empty() => {
194194
let sugg = "remove the return type";
195-
Some((sugg, "".into()))
195+
Some((sugg, String::new()))
196196
},
197197
_ => {
198198
let sugg = "return the output of the future directly";

clippy_lints/src/methods/option_map_unwrap_or.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(super) fn check<'tcx>(
7878
map_span,
7979
String::from(if unwrap_snippet_none { "and_then" } else { "map_or" }),
8080
),
81-
(expr.span.with_lo(unwrap_recv.span.hi()), String::from("")),
81+
(expr.span.with_lo(unwrap_recv.span.hi()), String::new()),
8282
];
8383

8484
if !unwrap_snippet_none {

clippy_lints/src/misc_early/unneeded_wildcard_pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) {
4646
"these patterns are unneeded as the `..` pattern can match those elements"
4747
},
4848
if only_one { "remove it" } else { "remove them" },
49-
"".to_string(),
49+
String::new(),
5050
Applicability::MachineApplicable,
5151
);
5252
}

clippy_lints/src/unnecessary_wraps.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
130130
(
131131
ret_expr.span,
132132
if inner_type.is_unit() {
133-
"".to_string()
133+
String::new()
134134
} else {
135135
snippet(cx, arg.span.source_callsite(), "..").to_string()
136136
}

tests/ui/case_sensitive_file_extension_comparisons.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@ fn is_rust_file(filename: &str) -> bool {
1414

1515
fn main() {
1616
// std::string::String and &str should trigger the lint failure with .ext12
17-
let _ = String::from("").ends_with(".ext12");
17+
let _ = String::new().ends_with(".ext12");
1818
let _ = "str".ends_with(".ext12");
1919

2020
// The test struct should not trigger the lint failure with .ext12
2121
TestStruct {}.ends_with(".ext12");
2222

2323
// std::string::String and &str should trigger the lint failure with .EXT12
24-
let _ = String::from("").ends_with(".EXT12");
24+
let _ = String::new().ends_with(".EXT12");
2525
let _ = "str".ends_with(".EXT12");
2626

2727
// The test struct should not trigger the lint failure with .EXT12
2828
TestStruct {}.ends_with(".EXT12");
2929

3030
// Should not trigger the lint failure with .eXT12
31-
let _ = String::from("").ends_with(".eXT12");
31+
let _ = String::new().ends_with(".eXT12");
3232
let _ = "str".ends_with(".eXT12");
3333
TestStruct {}.ends_with(".eXT12");
3434

3535
// Should not trigger the lint failure with .EXT123 (too long)
36-
let _ = String::from("").ends_with(".EXT123");
36+
let _ = String::new().ends_with(".EXT123");
3737
let _ = "str".ends_with(".EXT123");
3838
TestStruct {}.ends_with(".EXT123");
3939

4040
// Shouldn't fail if it doesn't start with a dot
41-
let _ = String::from("").ends_with("a.ext");
41+
let _ = String::new().ends_with("a.ext");
4242
let _ = "str".ends_with("a.extA");
4343
TestStruct {}.ends_with("a.ext");
4444
}

tests/ui/case_sensitive_file_extension_comparisons.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ LL | filename.ends_with(".rs")
88
= help: consider using a case-insensitive comparison instead
99

1010
error: case-sensitive file extension comparison
11-
--> $DIR/case_sensitive_file_extension_comparisons.rs:17:30
11+
--> $DIR/case_sensitive_file_extension_comparisons.rs:17:27
1212
|
13-
LL | let _ = String::from("").ends_with(".ext12");
14-
| ^^^^^^^^^^^^^^^^^^^
13+
LL | let _ = String::new().ends_with(".ext12");
14+
| ^^^^^^^^^^^^^^^^^^^
1515
|
1616
= help: consider using a case-insensitive comparison instead
1717

@@ -24,10 +24,10 @@ LL | let _ = "str".ends_with(".ext12");
2424
= help: consider using a case-insensitive comparison instead
2525

2626
error: case-sensitive file extension comparison
27-
--> $DIR/case_sensitive_file_extension_comparisons.rs:24:30
27+
--> $DIR/case_sensitive_file_extension_comparisons.rs:24:27
2828
|
29-
LL | let _ = String::from("").ends_with(".EXT12");
30-
| ^^^^^^^^^^^^^^^^^^^
29+
LL | let _ = String::new().ends_with(".EXT12");
30+
| ^^^^^^^^^^^^^^^^^^^
3131
|
3232
= help: consider using a case-insensitive comparison instead
3333

tests/ui/format.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn main() {
3333
format!("foo {}", "bar");
3434
format!("{} bar", "foo");
3535

36-
let arg: String = "".to_owned();
36+
let arg = String::new();
3737
arg.to_string();
3838
format!("{:?}", arg); // Don't warn about debug.
3939
format!("{:8}", arg);

tests/ui/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn main() {
3535
format!("foo {}", "bar");
3636
format!("{} bar", "foo");
3737

38-
let arg: String = "".to_owned();
38+
let arg = String::new();
3939
format!("{}", arg);
4040
format!("{:?}", arg); // Don't warn about debug.
4141
format!("{:8}", arg);

tests/ui/identity_op.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn main() {
6868
&x;
6969
x;
7070

71-
let mut a = A("".into());
71+
let mut a = A(String::new());
7272
let b = a << 0; // no error: non-integer
7373

7474
1 * Meter; // no error: non-integer

tests/ui/identity_op.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn main() {
6868
&x >> 0;
6969
x >> &0;
7070

71-
let mut a = A("".into());
71+
let mut a = A(String::new());
7272
let b = a << 0; // no error: non-integer
7373

7474
1 * Meter; // no error: non-integer

tests/ui/manual_empty_string_creations.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ macro_rules! create_strings_from_macro {
77
($some_str:expr) => {
88
let _: String = $some_str.into();
99
let _ = $some_str.to_string();
10-
}
10+
};
1111
}
1212

1313
fn main() {

tests/ui/or_fun_call.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ fn or_fun_call() {
9090
let mut btree_vec = BTreeMap::<u64, Vec<i32>>::new();
9191
btree_vec.entry(42).or_insert(vec![]);
9292

93-
let stringy = Some(String::from(""));
94-
let _ = stringy.unwrap_or_else(|| "".to_owned());
93+
let stringy = Some(String::new());
94+
let _ = stringy.unwrap_or_default();
9595

9696
let opt = Some(1);
9797
let hello = "Hello";

tests/ui/or_fun_call.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ fn or_fun_call() {
9090
let mut btree_vec = BTreeMap::<u64, Vec<i32>>::new();
9191
btree_vec.entry(42).or_insert(vec![]);
9292

93-
let stringy = Some(String::from(""));
94-
let _ = stringy.unwrap_or("".to_owned());
93+
let stringy = Some(String::new());
94+
let _ = stringy.unwrap_or(String::new());
9595

9696
let opt = Some(1);
9797
let hello = "Hello";

tests/ui/or_fun_call.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ error: use of `unwrap_or` followed by a function call
6666
LL | without_default.unwrap_or(Foo::new());
6767
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`
6868

69-
error: use of `unwrap_or` followed by a function call
69+
error: use of `unwrap_or` followed by a call to `new`
7070
--> $DIR/or_fun_call.rs:94:21
7171
|
72-
LL | let _ = stringy.unwrap_or("".to_owned());
73-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
72+
LL | let _ = stringy.unwrap_or(String::new());
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()`
7474

7575
error: use of `unwrap_or` followed by a function call
7676
--> $DIR/or_fun_call.rs:102:21

tests/ui/string_add.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ extern crate macro_rules;
77
#[allow(clippy::string_add_assign, unused)]
88
fn main() {
99
// ignores assignment distinction
10-
let mut x = "".to_owned();
10+
let mut x = String::new();
1111

1212
for _ in 1..3 {
1313
x = x + ".";
1414
}
1515

16-
let y = "".to_owned();
16+
let y = String::new();
1717
let z = y + "...";
1818

1919
assert_eq!(&x, &z);

tests/ui/string_add_assign.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#[warn(clippy::string_add_assign)]
55
fn main() {
66
// ignores assignment distinction
7-
let mut x = "".to_owned();
7+
let mut x = String::new();
88

99
for _ in 1..3 {
1010
x += ".";
1111
}
1212

13-
let y = "".to_owned();
13+
let y = String::new();
1414
let z = y + "...";
1515

1616
assert_eq!(&x, &z);

tests/ui/string_add_assign.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#[warn(clippy::string_add_assign)]
55
fn main() {
66
// ignores assignment distinction
7-
let mut x = "".to_owned();
7+
let mut x = String::new();
88

99
for _ in 1..3 {
1010
x = x + ".";
1111
}
1212

13-
let y = "".to_owned();
13+
let y = String::new();
1414
let z = y + "...";
1515

1616
assert_eq!(&x, &z);

tests/ui/unnecessary_owned_empty_strings.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fn main() {
1212
ref_str_argument("");
1313

1414
// should be linted
15+
#[allow(clippy::manual_empty_string_creations)]
1516
ref_str_argument("");
1617

1718
// should not be linted

tests/ui/unnecessary_owned_empty_strings.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fn main() {
1212
ref_str_argument(&String::new());
1313

1414
// should be linted
15+
#[allow(clippy::manual_empty_string_creations)]
1516
ref_str_argument(&String::from(""));
1617

1718
// should not be linted

tests/ui/unnecessary_owned_empty_strings.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | ref_str_argument(&String::new());
77
= note: `-D clippy::unnecessary-owned-empty-strings` implied by `-D warnings`
88

99
error: usage of `&String::from("")` for a function expecting a `&str` argument
10-
--> $DIR/unnecessary_owned_empty_strings.rs:15:22
10+
--> $DIR/unnecessary_owned_empty_strings.rs:16:22
1111
|
1212
LL | ref_str_argument(&String::from(""));
1313
| ^^^^^^^^^^^^^^^^^ help: try: `""`

tests/ui/useless_conversion_try.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ fn main() {
2929
let _ = String::try_from("foo".to_string()).unwrap();
3030
let _ = String::try_from(format!("A: {:04}", 123)).unwrap();
3131
let _: String = format!("Hello {}", "world").try_into().unwrap();
32-
let _: String = "".to_owned().try_into().unwrap();
32+
let _: String = String::new().try_into().unwrap();
3333
let _: String = match String::from("_").try_into() {
3434
Ok(a) => a,
35-
Err(_) => "".into(),
35+
Err(_) => String::new(),
3636
};
3737
// FIXME this is a false negative
3838
#[allow(clippy::cmp_owned)]

tests/ui/useless_conversion_try.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ LL | let _: String = format!("Hello {}", "world").try_into().unwrap();
6262
error: useless conversion to the same type: `std::string::String`
6363
--> $DIR/useless_conversion_try.rs:32:21
6464
|
65-
LL | let _: String = "".to_owned().try_into().unwrap();
65+
LL | let _: String = String::new().try_into().unwrap();
6666
| ^^^^^^^^^^^^^^^^^^^^^^^^
6767
|
6868
= help: consider removing `.try_into()`

0 commit comments

Comments
 (0)