about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Macleod <alex@macleod.io>2021-11-26 14:16:48 +0000
committerAlex Macleod <alex@macleod.io>2021-11-26 14:27:53 +0000
commitd346ec94fe3477ee2ee0cf41061a92c156db8701 (patch)
treecd4929d2db698445995e73c1d78fab901f0d7698
parent3957244120444d2ff9a5e43a0a2df97d79168174 (diff)
downloadrust-d346ec94fe3477ee2ee0cf41061a92c156db8701.tar.gz
rust-d346ec94fe3477ee2ee0cf41061a92c156db8701.zip
Add async/const fn tests for needless-late-init
+nits
-rw-r--r--clippy_lints/src/needless_late_init.rs16
-rw-r--r--tests/ui/let_if_seq.rs3
-rw-r--r--tests/ui/let_if_seq.stderr51
-rw-r--r--tests/ui/needless_late_init.rs34
-rw-r--r--tests/ui/needless_late_init.stderr56
-rw-r--r--tests/ui/needless_late_init_fixable.stderr4
6 files changed, 102 insertions, 62 deletions
diff --git a/clippy_lints/src/needless_late_init.rs b/clippy_lints/src/needless_late_init.rs
index d2f040dc36a..e0522f3fe0b 100644
--- a/clippy_lints/src/needless_late_init.rs
+++ b/clippy_lints/src/needless_late_init.rs
@@ -11,11 +11,11 @@ use rustc_span::Span;
 
 declare_clippy_lint! {
     /// ### What it does
-    /// Checks for late initializations that can be replaced by a let statement
+    /// Checks for late initializations that can be replaced by a `let` statement
     /// with an initializer.
     ///
     /// ### Why is this bad?
-    /// Assigning in the let statement is less repetitive.
+    /// Assigning in the `let` statement is less repetitive.
     ///
     /// ### Example
     /// ```rust
@@ -55,7 +55,7 @@ declare_clippy_lint! {
     #[clippy::version = "1.58.0"]
     pub NEEDLESS_LATE_INIT,
     style,
-    "late initializations that can be replaced by a let statement with an initializer"
+    "late initializations that can be replaced by a `let` statement with an initializer"
 }
 declare_lint_pass!(NeedlessLateInit => [NEEDLESS_LATE_INIT]);
 
@@ -275,7 +275,7 @@ fn check<'tcx>(
                     if usage.needs_semi {
                         diag.span_suggestion(
                             usage.stmt.span.shrink_to_hi(),
-                            "add a semicolon after the if expression",
+                            "add a semicolon after the `if` expression",
                             ";".to_string(),
                             applicability,
                         );
@@ -301,12 +301,16 @@ fn check<'tcx>(
                         applicability,
                     );
 
-                    diag.multipart_suggestion("remove the assignments from the match arms", suggestions, applicability);
+                    diag.multipart_suggestion(
+                        "remove the assignments from the `match` arms",
+                        suggestions,
+                        applicability,
+                    );
 
                     if usage.needs_semi {
                         diag.span_suggestion(
                             usage.stmt.span.shrink_to_hi(),
-                            "add a semicolon after the match expression",
+                            "add a semicolon after the `match` expression",
                             ";".to_string(),
                             applicability,
                         );
diff --git a/tests/ui/let_if_seq.rs b/tests/ui/let_if_seq.rs
index 2d8f3c2f0e7..c5cb2eb1fe1 100644
--- a/tests/ui/let_if_seq.rs
+++ b/tests/ui/let_if_seq.rs
@@ -3,7 +3,8 @@
     unused_assignments,
     clippy::similar_names,
     clippy::blacklisted_name,
-    clippy::branches_sharing_code
+    clippy::branches_sharing_code,
+    clippy::needless_late_init
 )]
 #![warn(clippy::useless_let_if_seq)]
 
diff --git a/tests/ui/let_if_seq.stderr b/tests/ui/let_if_seq.stderr
index 3a522628fea..271ccce681c 100644
--- a/tests/ui/let_if_seq.stderr
+++ b/tests/ui/let_if_seq.stderr
@@ -1,25 +1,5 @@
-error: unneeded late initalization
-  --> $DIR/let_if_seq.rs:48:5
-   |
-LL |     let foo;
-   |     ^^^^^^^^
-   |
-   = note: `-D clippy::needless-late-init` implied by `-D warnings`
-help: declare `foo` here
-   |
-LL |     let foo = if f() {
-   |     +++++++++
-help: remove the assignments from the branches
-   |
-LL |         0
-   |
-help: add a semicolon after the if expression
-   |
-LL |     };
-   |      +
-
 error: `if _ { .. } else { .. }` is an expression
-  --> $DIR/let_if_seq.rs:65:5
+  --> $DIR/let_if_seq.rs:66:5
    |
 LL | /     let mut foo = 0;
 LL | |     if f() {
@@ -31,7 +11,7 @@ LL | |     }
    = note: you might not need `mut` at all
 
 error: `if _ { .. } else { .. }` is an expression
-  --> $DIR/let_if_seq.rs:70:5
+  --> $DIR/let_if_seq.rs:71:5
    |
 LL | /     let mut bar = 0;
 LL | |     if f() {
@@ -45,7 +25,7 @@ LL | |     }
    = note: you might not need `mut` at all
 
 error: `if _ { .. } else { .. }` is an expression
-  --> $DIR/let_if_seq.rs:78:5
+  --> $DIR/let_if_seq.rs:79:5
    |
 LL | /     let quz;
 LL | |     if f() {
@@ -56,7 +36,7 @@ LL | |     }
    | |_____^ help: it is more idiomatic to write: `let quz = if f() { 42 } else { 0 };`
 
 error: `if _ { .. } else { .. }` is an expression
-  --> $DIR/let_if_seq.rs:107:5
+  --> $DIR/let_if_seq.rs:108:5
    |
 LL | /     let mut baz = 0;
 LL | |     if f() {
@@ -66,26 +46,5 @@ LL | |     }
    |
    = note: you might not need `mut` at all
 
-error: unneeded late initalization
-  --> $DIR/let_if_seq.rs:78:5
-   |
-LL |     let quz;
-   |     ^^^^^^^^
-   |
-help: declare `quz` here
-   |
-LL |     let quz = if f() {
-   |     +++++++++
-help: remove the assignments from the branches
-   |
-LL ~         42
-LL |     } else {
-LL ~         0
-   |
-help: add a semicolon after the if expression
-   |
-LL |     };
-   |      +
-
-error: aborting due to 6 previous errors
+error: aborting due to 4 previous errors
 
diff --git a/tests/ui/needless_late_init.rs b/tests/ui/needless_late_init.rs
index 37b3cb34e2a..6fdb4cc566f 100644
--- a/tests/ui/needless_late_init.rs
+++ b/tests/ui/needless_late_init.rs
@@ -42,6 +42,40 @@ fn main() {
     println!("{}", a);
 }
 
+async fn in_async() -> &'static str {
+    async fn f() -> &'static str {
+        "one"
+    }
+
+    let a;
+    let n = 1;
+    match n {
+        1 => a = f().await,
+        _ => {
+            a = "two";
+        },
+    }
+
+    a
+}
+
+const fn in_const() -> &'static str {
+    const fn f() -> &'static str {
+        "one"
+    }
+
+    let a;
+    let n = 1;
+    match n {
+        1 => a = f(),
+        _ => {
+            a = "two";
+        },
+    }
+
+    a
+}
+
 fn does_not_lint() {
     let z;
     if false {
diff --git a/tests/ui/needless_late_init.stderr b/tests/ui/needless_late_init.stderr
index ece50773f45..cbb7538c63b 100644
--- a/tests/ui/needless_late_init.stderr
+++ b/tests/ui/needless_late_init.stderr
@@ -9,13 +9,13 @@ help: declare `a` here
    |
 LL |     let a = match n {
    |     +++++++
-help: remove the assignments from the match arms
+help: remove the assignments from the `match` arms
    |
 LL ~         1 => "one",
 LL |         _ => {
 LL ~             "two"
    |
-help: add a semicolon after the match expression
+help: add a semicolon after the `match` expression
    |
 LL |     };
    |      +
@@ -36,7 +36,7 @@ LL ~         "four"
 LL |     } else {
 LL ~         "five"
    |
-help: add a semicolon after the if expression
+help: add a semicolon after the `if` expression
    |
 LL |     };
    |      +
@@ -57,7 +57,7 @@ LL ~         n
 LL |     } else {
 LL ~         -50
    |
-help: add a semicolon after the if expression
+help: add a semicolon after the `if` expression
    |
 LL |     };
    |      +
@@ -78,7 +78,7 @@ LL ~         temp
 LL |     } else {
 LL ~         15
    |
-help: add a semicolon after the if expression
+help: add a semicolon after the `if` expression
    |
 LL |     };
    |      +
@@ -99,10 +99,52 @@ LL ~         format!("{} {}", a, b)
 LL |     } else {
 LL ~         format!("{}", c)
    |
-help: add a semicolon after the if expression
+help: add a semicolon after the `if` expression
    |
 LL |     };
    |      +
 
-error: aborting due to 5 previous errors
+error: unneeded late initalization
+  --> $DIR/needless_late_init.rs:50:5
+   |
+LL |     let a;
+   |     ^^^^^^
+   |
+help: declare `a` here
+   |
+LL |     let a = match n {
+   |     +++++++
+help: remove the assignments from the `match` arms
+   |
+LL ~         1 => f().await,
+LL |         _ => {
+LL ~             "two"
+   |
+help: add a semicolon after the `match` expression
+   |
+LL |     };
+   |      +
+
+error: unneeded late initalization
+  --> $DIR/needless_late_init.rs:67:5
+   |
+LL |     let a;
+   |     ^^^^^^
+   |
+help: declare `a` here
+   |
+LL |     let a = match n {
+   |     +++++++
+help: remove the assignments from the `match` arms
+   |
+LL ~         1 => f(),
+LL |         _ => {
+LL ~             "two"
+   |
+help: add a semicolon after the `match` expression
+   |
+LL |     };
+   |      +
+
+error: aborting due to 7 previous errors
 
diff --git a/tests/ui/needless_late_init_fixable.stderr b/tests/ui/needless_late_init_fixable.stderr
index 05491496cd7..a0ce4f812f4 100644
--- a/tests/ui/needless_late_init_fixable.stderr
+++ b/tests/ui/needless_late_init_fixable.stderr
@@ -64,7 +64,7 @@ help: declare `f` here
    |
 LL |     let f = match 1 {
    |     +++++++
-help: remove the assignments from the match arms
+help: remove the assignments from the `match` arms
    |
 LL |         1 => "three",
    |              ~~~~~~~
@@ -83,7 +83,7 @@ help: remove the assignments from the branches
    |
 LL |         5
    |
-help: add a semicolon after the if expression
+help: add a semicolon after the `if` expression
    |
 LL |     };
    |      +