Skip to content

Commit

Permalink
Update all tests to comply with clippy::manual_empty_string_creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme-Vasconcelos committed Aug 9, 2022
1 parent c20af54 commit a51c778
Show file tree
Hide file tree
Showing 22 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion clippy_dev/src/new_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn to_camel_case(name: &str) -> String {
name.split('_')
.map(|s| {
if s.is_empty() {
String::from("")
String::new()
} else {
[&s[0..1].to_uppercase(), &s[1..]].concat()
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/manual_async_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn suggested_ret(cx: &LateContext<'_>, output: &Ty<'_>) -> Option<(&'static str,
match output.kind {
TyKind::Tup(tys) if tys.is_empty() => {
let sugg = "remove the return type";
Some((sugg, "".into()))
Some((sugg, String::new()))
},
_ => {
let sugg = "return the output of the future directly";
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/option_map_unwrap_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(super) fn check<'tcx>(
map_span,
String::from(if unwrap_snippet_none { "and_then" } else { "map_or" }),
),
(expr.span.with_lo(unwrap_recv.span.hi()), String::from("")),
(expr.span.with_lo(unwrap_recv.span.hi()), String::new()),
];

if !unwrap_snippet_none {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/misc_early/unneeded_wildcard_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) {
"these patterns are unneeded as the `..` pattern can match those elements"
},
if only_one { "remove it" } else { "remove them" },
"".to_string(),
String::new(),
Applicability::MachineApplicable,
);
}
2 changes: 1 addition & 1 deletion clippy_lints/src/unnecessary_wraps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
(
ret_expr.span,
if inner_type.is_unit() {
"".to_string()
String::new()
} else {
snippet(cx, arg.span.source_callsite(), "..").to_string()
}
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/case_sensitive_file_extension_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ fn is_rust_file(filename: &str) -> bool {

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

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

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

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

// Should not trigger the lint failure with .eXT12
let _ = String::from("").ends_with(".eXT12");
let _ = String::new().ends_with(".eXT12");
let _ = "str".ends_with(".eXT12");
TestStruct {}.ends_with(".eXT12");

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

// Shouldn't fail if it doesn't start with a dot
let _ = String::from("").ends_with("a.ext");
let _ = String::new().ends_with("a.ext");
let _ = "str".ends_with("a.extA");
TestStruct {}.ends_with("a.ext");
}
12 changes: 6 additions & 6 deletions tests/ui/case_sensitive_file_extension_comparisons.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ LL | filename.ends_with(".rs")
= help: consider using a case-insensitive comparison instead

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/format.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {
format!("foo {}", "bar");
format!("{} bar", "foo");

let arg: String = "".to_owned();
let arg = String::new();
arg.to_string();
format!("{:?}", arg); // Don't warn about debug.
format!("{:8}", arg);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {
format!("foo {}", "bar");
format!("{} bar", "foo");

let arg: String = "".to_owned();
let arg = String::new();
format!("{}", arg);
format!("{:?}", arg); // Don't warn about debug.
format!("{:8}", arg);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/identity_op.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn main() {
&x;
x;

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

1 * Meter; // no error: non-integer
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/identity_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn main() {
&x >> 0;
x >> &0;

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

1 * Meter; // no error: non-integer
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/or_fun_call.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ fn or_fun_call() {
let mut btree_vec = BTreeMap::<u64, Vec<i32>>::new();
btree_vec.entry(42).or_insert(vec![]);

let stringy = Some(String::from(""));
let _ = stringy.unwrap_or_else(|| "".to_owned());
let stringy = Some(String::new());
let _ = stringy.unwrap_or_default();

let opt = Some(1);
let hello = "Hello";
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/or_fun_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ fn or_fun_call() {
let mut btree_vec = BTreeMap::<u64, Vec<i32>>::new();
btree_vec.entry(42).or_insert(vec![]);

let stringy = Some(String::from(""));
let _ = stringy.unwrap_or("".to_owned());
let stringy = Some(String::new());
let _ = stringy.unwrap_or(String::new());

let opt = Some(1);
let hello = "Hello";
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/or_fun_call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ error: use of `unwrap_or` followed by a function call
LL | without_default.unwrap_or(Foo::new());
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`

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

error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:102:21
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/string_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ extern crate macro_rules;
#[allow(clippy::string_add_assign, unused)]
fn main() {
// ignores assignment distinction
let mut x = "".to_owned();
let mut x = String::new();

for _ in 1..3 {
x = x + ".";
}

let y = "".to_owned();
let y = String::new();
let z = y + "...";

assert_eq!(&x, &z);
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/string_add_assign.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#[warn(clippy::string_add_assign)]
fn main() {
// ignores assignment distinction
let mut x = "".to_owned();
let mut x = String::new();

for _ in 1..3 {
x += ".";
}

let y = "".to_owned();
let y = String::new();
let z = y + "...";

assert_eq!(&x, &z);
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/string_add_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#[warn(clippy::string_add_assign)]
fn main() {
// ignores assignment distinction
let mut x = "".to_owned();
let mut x = String::new();

for _ in 1..3 {
x = x + ".";
}

let y = "".to_owned();
let y = String::new();
let z = y + "...";

assert_eq!(&x, &z);
Expand Down
1 change: 1 addition & 0 deletions tests/ui/unnecessary_owned_empty_strings.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn main() {
ref_str_argument("");

// should be linted
#[allow(clippy::manual_empty_string_creation)]
ref_str_argument("");

// should not be linted
Expand Down
1 change: 1 addition & 0 deletions tests/ui/unnecessary_owned_empty_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn main() {
ref_str_argument(&String::new());

// should be linted
#[allow(clippy::manual_empty_string_creation)]
ref_str_argument(&String::from(""));

// should not be linted
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unnecessary_owned_empty_strings.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | ref_str_argument(&String::new());
= note: `-D clippy::unnecessary-owned-empty-strings` implied by `-D warnings`

error: usage of `&String::from("")` for a function expecting a `&str` argument
--> $DIR/unnecessary_owned_empty_strings.rs:15:22
--> $DIR/unnecessary_owned_empty_strings.rs:16:22
|
LL | ref_str_argument(&String::from(""));
| ^^^^^^^^^^^^^^^^^ help: try: `""`
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/useless_conversion_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ fn main() {
let _ = String::try_from("foo".to_string()).unwrap();
let _ = String::try_from(format!("A: {:04}", 123)).unwrap();
let _: String = format!("Hello {}", "world").try_into().unwrap();
let _: String = "".to_owned().try_into().unwrap();
let _: String = String::new().try_into().unwrap();
let _: String = match String::from("_").try_into() {
Ok(a) => a,
Err(_) => "".into(),
Err(_) => String::new(),
};
// FIXME this is a false negative
#[allow(clippy::cmp_owned)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/useless_conversion_try.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ LL | let _: String = format!("Hello {}", "world").try_into().unwrap();
error: useless conversion to the same type: `std::string::String`
--> $DIR/useless_conversion_try.rs:32:21
|
LL | let _: String = "".to_owned().try_into().unwrap();
LL | let _: String = String::new().try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `.try_into()`
Expand Down

0 comments on commit a51c778

Please sign in to comment.