about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-04-11 11:12:33 +0000
committerbors <bors@rust-lang.org>2022-04-11 11:12:33 +0000
commit5c19ae96e7e7b8eecf86c87c96ba666cd5b5066d (patch)
tree1b48cafc0290fcedaa52c40944339829f5c3d10c /tests
parent85e91dc99e1fe21eebe7f9485a8af8b5a3430051 (diff)
parent10201370a1509b806b9e2a83ded6b132466291b8 (diff)
downloadrust-5c19ae96e7e7b8eecf86c87c96ba666cd5b5066d.tar.gz
rust-5c19ae96e7e7b8eecf86c87c96ba666cd5b5066d.zip
Auto merge of #8660 - yoav-lavi:squashed-master, r=flip1995
`unnecessary_owned_empty_strings`

[`unnecessary_owned_empty_strings`]

Fixes https://github.com/rust-lang/rust-clippy/issues/8650

- \[x] Followed [lint naming conventions][lint_naming]
- \[x] Added passing UI tests (including committed `.stderr` file)
- \[x] `cargo test` passes locally
- \[x] Executed `cargo dev update_lints`
- \[x] Added lint documentation
- \[x] Run `cargo dev fmt`

[lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints

changelog: Adds `unnecessary_owned_empty_strings`, a lint that detects passing owned empty strings to a function expecting `&str`
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/unnecessary_owned_empty_strings.fixed22
-rw-r--r--tests/ui/unnecessary_owned_empty_strings.rs22
-rw-r--r--tests/ui/unnecessary_owned_empty_strings.stderr16
3 files changed, 60 insertions, 0 deletions
diff --git a/tests/ui/unnecessary_owned_empty_strings.fixed b/tests/ui/unnecessary_owned_empty_strings.fixed
new file mode 100644
index 00000000000..f95f91329a2
--- /dev/null
+++ b/tests/ui/unnecessary_owned_empty_strings.fixed
@@ -0,0 +1,22 @@
+// run-rustfix
+
+#![warn(clippy::unnecessary_owned_empty_strings)]
+
+fn ref_str_argument(_value: &str) {}
+
+#[allow(clippy::ptr_arg)]
+fn ref_string_argument(_value: &String) {}
+
+fn main() {
+    // should be linted
+    ref_str_argument("");
+
+    // should be linted
+    ref_str_argument("");
+
+    // should not be linted
+    ref_str_argument("");
+
+    // should not be linted
+    ref_string_argument(&String::new());
+}
diff --git a/tests/ui/unnecessary_owned_empty_strings.rs b/tests/ui/unnecessary_owned_empty_strings.rs
new file mode 100644
index 00000000000..0cbdc151ed9
--- /dev/null
+++ b/tests/ui/unnecessary_owned_empty_strings.rs
@@ -0,0 +1,22 @@
+// run-rustfix
+
+#![warn(clippy::unnecessary_owned_empty_strings)]
+
+fn ref_str_argument(_value: &str) {}
+
+#[allow(clippy::ptr_arg)]
+fn ref_string_argument(_value: &String) {}
+
+fn main() {
+    // should be linted
+    ref_str_argument(&String::new());
+
+    // should be linted
+    ref_str_argument(&String::from(""));
+
+    // should not be linted
+    ref_str_argument("");
+
+    // should not be linted
+    ref_string_argument(&String::new());
+}
diff --git a/tests/ui/unnecessary_owned_empty_strings.stderr b/tests/ui/unnecessary_owned_empty_strings.stderr
new file mode 100644
index 00000000000..46bc4597b33
--- /dev/null
+++ b/tests/ui/unnecessary_owned_empty_strings.stderr
@@ -0,0 +1,16 @@
+error: usage of `&String::new()` for a function expecting a `&str` argument
+  --> $DIR/unnecessary_owned_empty_strings.rs:12:22
+   |
+LL |     ref_str_argument(&String::new());
+   |                      ^^^^^^^^^^^^^^ help: try: `""`
+   |
+   = 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
+   |
+LL |     ref_str_argument(&String::from(""));
+   |                      ^^^^^^^^^^^^^^^^^ help: try: `""`
+
+error: aborting due to 2 previous errors
+