]> git.lizzy.rs Git - rust.git/commitdiff
unnecessary_owned_empty_string -> unnecessary_owned_empty_strings
authorYoav Lavi <yoavlavi122@gmail.com>
Mon, 11 Apr 2022 11:05:42 +0000 (13:05 +0200)
committerYoav Lavi <yoavlavi122@gmail.com>
Mon, 11 Apr 2022 11:05:42 +0000 (13:05 +0200)
13 files changed:
CHANGELOG.md
clippy_lints/src/lib.register_all.rs
clippy_lints/src/lib.register_lints.rs
clippy_lints/src/lib.register_style.rs
clippy_lints/src/lib.rs
clippy_lints/src/unnecessary_owned_empty_string.rs [deleted file]
clippy_lints/src/unnecessary_owned_empty_strings.rs [new file with mode: 0644]
tests/ui/unnecessary_owned_empty_string.fixed [deleted file]
tests/ui/unnecessary_owned_empty_string.rs [deleted file]
tests/ui/unnecessary_owned_empty_string.stderr [deleted file]
tests/ui/unnecessary_owned_empty_strings.fixed [new file with mode: 0644]
tests/ui/unnecessary_owned_empty_strings.rs [new file with mode: 0644]
tests/ui/unnecessary_owned_empty_strings.stderr [new file with mode: 0644]

index e48413dc30c5d63f461ae0757e2d2dc00f84e3f3..44a36870108d5c5900577dd31fd3d5809a3076b3 100644 (file)
@@ -3650,7 +3650,7 @@ Released 2018-09-13
 [`unnecessary_lazy_evaluations`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
 [`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
 [`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
-[`unnecessary_owned_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_string
+[`unnecessary_owned_empty_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_strings
 [`unnecessary_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports
 [`unnecessary_sort_by`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by
 [`unnecessary_to_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
index b601e37a4c8769921e62b0fd8f077b3f692aaa96..02ba7835639e7e58c104c06031c9c71b644e2095 100644 (file)
     LintId::of(unit_types::UNIT_CMP),
     LintId::of(unnamed_address::FN_ADDRESS_COMPARISONS),
     LintId::of(unnamed_address::VTABLE_ADDRESS_COMPARISONS),
-    LintId::of(unnecessary_owned_empty_string::UNNECESSARY_OWNED_EMPTY_STRING),
+    LintId::of(unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS),
     LintId::of(unnecessary_sort_by::UNNECESSARY_SORT_BY),
     LintId::of(unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME),
     LintId::of(unused_io_amount::UNUSED_IO_AMOUNT),
index 0a23e60fd2c4cab1dea313b13ef54950858146ca..704e79885cfb45b7e38fff57832673812f119202 100644 (file)
     unit_types::UNIT_CMP,
     unnamed_address::FN_ADDRESS_COMPARISONS,
     unnamed_address::VTABLE_ADDRESS_COMPARISONS,
-    unnecessary_owned_empty_string::UNNECESSARY_OWNED_EMPTY_STRING,
+    unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS,
     unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS,
     unnecessary_sort_by::UNNECESSARY_SORT_BY,
     unnecessary_wraps::UNNECESSARY_WRAPS,
index b014c11f0f4a37526eeb060cdce2ce2a4488458e..f52fe97ed237c6ed763113751ed716afd142415b 100644 (file)
     LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
     LintId::of(tabs_in_doc_comments::TABS_IN_DOC_COMMENTS),
     LintId::of(to_digit_is_some::TO_DIGIT_IS_SOME),
-    LintId::of(unnecessary_owned_empty_string::UNNECESSARY_OWNED_EMPTY_STRING),
+    LintId::of(unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS),
     LintId::of(unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME),
     LintId::of(unused_unit::UNUSED_UNIT),
     LintId::of(upper_case_acronyms::UPPER_CASE_ACRONYMS),
index 576a6611267577916bbf839f1616670fa5a0e7a2..74ade422dc88707ca1031f9ea2c7a19aee411838 100644 (file)
@@ -383,7 +383,7 @@ macro_rules! declare_clippy_lint {
 mod unit_return_expecting_ord;
 mod unit_types;
 mod unnamed_address;
-mod unnecessary_owned_empty_string;
+mod unnecessary_owned_empty_strings;
 mod unnecessary_self_imports;
 mod unnecessary_sort_by;
 mod unnecessary_wraps;
@@ -869,7 +869,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
     });
     store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef));
     store.register_early_pass(|| Box::new(empty_structs_with_brackets::EmptyStructsWithBrackets));
-    store.register_late_pass(|| Box::new(unnecessary_owned_empty_string::UnnecessaryOwnedEmptyString));
+    store.register_late_pass(|| Box::new(unnecessary_owned_empty_strings::UnnecessaryOwnedEmptyStrings));
     // add lints here, do not remove this comment, it's used in `new_lint`
 }
 
diff --git a/clippy_lints/src/unnecessary_owned_empty_string.rs b/clippy_lints/src/unnecessary_owned_empty_string.rs
deleted file mode 100644 (file)
index bb42c78..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-use clippy_utils::{diagnostics::span_lint_and_sugg, ty::is_type_diagnostic_item};
-use clippy_utils::{match_def_path, paths};
-use if_chain::if_chain;
-use rustc_ast::ast::LitKind;
-use rustc_errors::Applicability;
-use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability};
-use rustc_lint::{LateContext, LateLintPass};
-use rustc_middle::ty;
-use rustc_session::{declare_lint_pass, declare_tool_lint};
-use rustc_span::sym;
-
-declare_clippy_lint! {
-    /// ### What it does
-    ///
-    /// Detects cases of owned empty strings being passed as an argument to a function expecting `&str`
-    ///
-    /// ### Why is this bad?
-    ///
-    /// This results in longer and less readable code
-    ///
-    /// ### Example
-    /// ```rust
-    /// vec!["1", "2", "3"].join(&String::new());
-    /// ```
-    /// Use instead:
-    /// ```rust
-    /// vec!["1", "2", "3"].join("");
-    /// ```
-    #[clippy::version = "1.62.0"]
-    pub UNNECESSARY_OWNED_EMPTY_STRING,
-    style,
-    "detects cases of references to owned empty strings being passed as an argument to a function expecting `&str`"
-}
-declare_lint_pass!(UnnecessaryOwnedEmptyString => [UNNECESSARY_OWNED_EMPTY_STRING]);
-
-impl<'tcx> LateLintPass<'tcx> for UnnecessaryOwnedEmptyString {
-    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
-        if_chain! {
-            if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner_expr) = expr.kind;
-            if let ExprKind::Call(fun, args) = inner_expr.kind;
-            if let ExprKind::Path(ref qpath) = fun.kind;
-            if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id();
-            if let ty::Ref(_, inner_str, _) = cx.typeck_results().expr_ty_adjusted(expr).kind();
-            if inner_str.is_str();
-            then {
-                if match_def_path(cx, fun_def_id, &paths::STRING_NEW) {
-                     span_lint_and_sugg(
-                            cx,
-                            UNNECESSARY_OWNED_EMPTY_STRING,
-                            expr.span,
-                            "usage of `&String::new()` for a function expecting a `&str` argument",
-                            "try",
-                            "\"\"".to_owned(),
-                            Applicability::MachineApplicable,
-                        );
-                } else {
-                    if_chain! {
-                        if match_def_path(cx, fun_def_id, &paths::FROM_FROM);
-                        if let [.., last_arg] = args;
-                        if let ExprKind::Lit(spanned) = &last_arg.kind;
-                        if let LitKind::Str(symbol, _) = spanned.node;
-                        if symbol.is_empty();
-                        let inner_expr_type = cx.typeck_results().expr_ty(inner_expr);
-                        if is_type_diagnostic_item(cx, inner_expr_type, sym::String);
-                        then {
-                            span_lint_and_sugg(
-                                cx,
-                                UNNECESSARY_OWNED_EMPTY_STRING,
-                                expr.span,
-                                "usage of `&String::from(\"\")` for a function expecting a `&str` argument",
-                                "try",
-                                "\"\"".to_owned(),
-                                Applicability::MachineApplicable,
-                            );
-                        }
-                    }
-                }
-            }
-        }
-    }
-}
diff --git a/clippy_lints/src/unnecessary_owned_empty_strings.rs b/clippy_lints/src/unnecessary_owned_empty_strings.rs
new file mode 100644 (file)
index 0000000..8a4f4c0
--- /dev/null
@@ -0,0 +1,81 @@
+use clippy_utils::{diagnostics::span_lint_and_sugg, ty::is_type_diagnostic_item};
+use clippy_utils::{match_def_path, paths};
+use if_chain::if_chain;
+use rustc_ast::ast::LitKind;
+use rustc_errors::Applicability;
+use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability};
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_middle::ty;
+use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_span::sym;
+
+declare_clippy_lint! {
+    /// ### What it does
+    ///
+    /// Detects cases of owned empty strings being passed as an argument to a function expecting `&str`
+    ///
+    /// ### Why is this bad?
+    ///
+    /// This results in longer and less readable code
+    ///
+    /// ### Example
+    /// ```rust
+    /// vec!["1", "2", "3"].join(&String::new());
+    /// ```
+    /// Use instead:
+    /// ```rust
+    /// vec!["1", "2", "3"].join("");
+    /// ```
+    #[clippy::version = "1.62.0"]
+    pub UNNECESSARY_OWNED_EMPTY_STRINGS,
+    style,
+    "detects cases of references to owned empty strings being passed as an argument to a function expecting `&str`"
+}
+declare_lint_pass!(UnnecessaryOwnedEmptyStrings => [UNNECESSARY_OWNED_EMPTY_STRINGS]);
+
+impl<'tcx> LateLintPass<'tcx> for UnnecessaryOwnedEmptyStrings {
+    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
+        if_chain! {
+            if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner_expr) = expr.kind;
+            if let ExprKind::Call(fun, args) = inner_expr.kind;
+            if let ExprKind::Path(ref qpath) = fun.kind;
+            if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id();
+            if let ty::Ref(_, inner_str, _) = cx.typeck_results().expr_ty_adjusted(expr).kind();
+            if inner_str.is_str();
+            then {
+                if match_def_path(cx, fun_def_id, &paths::STRING_NEW) {
+                     span_lint_and_sugg(
+                            cx,
+                            UNNECESSARY_OWNED_EMPTY_STRINGS,
+                            expr.span,
+                            "usage of `&String::new()` for a function expecting a `&str` argument",
+                            "try",
+                            "\"\"".to_owned(),
+                            Applicability::MachineApplicable,
+                        );
+                } else {
+                    if_chain! {
+                        if match_def_path(cx, fun_def_id, &paths::FROM_FROM);
+                        if let [.., last_arg] = args;
+                        if let ExprKind::Lit(spanned) = &last_arg.kind;
+                        if let LitKind::Str(symbol, _) = spanned.node;
+                        if symbol.is_empty();
+                        let inner_expr_type = cx.typeck_results().expr_ty(inner_expr);
+                        if is_type_diagnostic_item(cx, inner_expr_type, sym::String);
+                        then {
+                            span_lint_and_sugg(
+                                cx,
+                                UNNECESSARY_OWNED_EMPTY_STRINGS,
+                                expr.span,
+                                "usage of `&String::from(\"\")` for a function expecting a `&str` argument",
+                                "try",
+                                "\"\"".to_owned(),
+                                Applicability::MachineApplicable,
+                            );
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
diff --git a/tests/ui/unnecessary_owned_empty_string.fixed b/tests/ui/unnecessary_owned_empty_string.fixed
deleted file mode 100644 (file)
index cd69e95..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-// run-rustfix
-
-#![warn(clippy::unnecessary_owned_empty_string)]
-
-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_string.rs b/tests/ui/unnecessary_owned_empty_string.rs
deleted file mode 100644 (file)
index 3fbba15..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-// run-rustfix
-
-#![warn(clippy::unnecessary_owned_empty_string)]
-
-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_string.stderr b/tests/ui/unnecessary_owned_empty_string.stderr
deleted file mode 100644 (file)
index c323722..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-error: usage of `&String::new()` for a function expecting a `&str` argument
-  --> $DIR/unnecessary_owned_empty_string.rs:12:22
-   |
-LL |     ref_str_argument(&String::new());
-   |                      ^^^^^^^^^^^^^^ help: try: `""`
-   |
-   = note: `-D clippy::unnecessary-owned-empty-string` implied by `-D warnings`
-
-error: usage of `&String::from("")` for a function expecting a `&str` argument
-  --> $DIR/unnecessary_owned_empty_string.rs:15:22
-   |
-LL |     ref_str_argument(&String::from(""));
-   |                      ^^^^^^^^^^^^^^^^^ help: try: `""`
-
-error: aborting due to 2 previous errors
-
diff --git a/tests/ui/unnecessary_owned_empty_strings.fixed b/tests/ui/unnecessary_owned_empty_strings.fixed
new file mode 100644 (file)
index 0000000..f95f913
--- /dev/null
@@ -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 (file)
index 0000000..0cbdc15
--- /dev/null
@@ -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 (file)
index 0000000..46bc459
--- /dev/null
@@ -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
+