about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-05-19 14:52:30 +0000
committerbors <bors@rust-lang.org>2018-05-19 14:52:30 +0000
commit1b240dad0648dc45e50fd0421a29390343ad5808 (patch)
tree32a9591c9cd4f961182958e1d238f6065c826928 /src/test
parentef8ee647749ad2f0acf235aaf18debacb974a048 (diff)
parent6da64a7666e89b39b99d728aaa3a40c124f31563 (diff)
downloadrust-1b240dad0648dc45e50fd0421a29390343ad5808.tar.gz
rust-1b240dad0648dc45e50fd0421a29390343ad5808.zip
Auto merge of #50763 - KyleStach1678:unused-loop-label, r=petrochenkov
Add lint checks for unused loop labels

Previously no warning was generated when a loop label was written out but never used (i.e. in a `break` or `continue` statement):
```rust
fn main() {
    'unused_label: loop {}
}
```
would compile without complaint.

This fix introduces `-W unused_loop_label`, which generates the following warning message for the above snippet:
```
warning: unused loop label
 --> main.rs:2:5
  |
2 |     'unused_label: loop {}
  |     ^^^^^^^^^^^^^
  |
  = note: #[warn(unused_loop_label)] on by default
```

Fixes: #50751.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/label_break_value_continue.rs1
-rw-r--r--src/test/ui/label_break_value_continue.stderr8
-rw-r--r--src/test/ui/label_break_value_unlabeled_break.rs1
-rw-r--r--src/test/ui/label_break_value_unlabeled_break.stderr4
-rw-r--r--src/test/ui/lint/unused_labels.rs96
-rw-r--r--src/test/ui/lint/unused_labels.stderr63
-rw-r--r--src/test/ui/loops-reject-duplicate-labels-2.rs1
-rw-r--r--src/test/ui/loops-reject-duplicate-labels-2.stderr18
-rw-r--r--src/test/ui/loops-reject-duplicate-labels.rs1
-rw-r--r--src/test/ui/loops-reject-duplicate-labels.stderr18
-rw-r--r--src/test/ui/suggestions/suggest-labels.rs2
11 files changed, 188 insertions, 25 deletions
diff --git a/src/test/ui/label_break_value_continue.rs b/src/test/ui/label_break_value_continue.rs
index 52e24b759d1..4a505dff3d4 100644
--- a/src/test/ui/label_break_value_continue.rs
+++ b/src/test/ui/label_break_value_continue.rs
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #![feature(label_break_value)]
+#![allow(unused_labels)]
 
 // Simple continue pointing to an unlabeled break should yield in an error
 fn continue_simple() {
diff --git a/src/test/ui/label_break_value_continue.stderr b/src/test/ui/label_break_value_continue.stderr
index 24c2d1a22d0..12a21a8a594 100644
--- a/src/test/ui/label_break_value_continue.stderr
+++ b/src/test/ui/label_break_value_continue.stderr
@@ -1,17 +1,17 @@
 error[E0695]: unlabeled `continue` inside of a labeled block
-  --> $DIR/label_break_value_continue.rs:16:9
+  --> $DIR/label_break_value_continue.rs:17:9
    |
 LL |         continue; //~ ERROR unlabeled `continue` inside of a labeled block
    |         ^^^^^^^^ `continue` statements that would diverge to or through a labeled block need to bear a label
 
 error[E0696]: `continue` pointing to a labeled block
-  --> $DIR/label_break_value_continue.rs:23:9
+  --> $DIR/label_break_value_continue.rs:24:9
    |
 LL |         continue 'b; //~ ERROR `continue` pointing to a labeled block
    |         ^^^^^^^^^^^ labeled blocks cannot be `continue`'d
    |
 note: labeled block the continue points to
-  --> $DIR/label_break_value_continue.rs:22:5
+  --> $DIR/label_break_value_continue.rs:23:5
    |
 LL | /     'b: {
 LL | |         continue 'b; //~ ERROR `continue` pointing to a labeled block
@@ -19,7 +19,7 @@ LL | |     }
    | |_____^
 
 error[E0695]: unlabeled `continue` inside of a labeled block
-  --> $DIR/label_break_value_continue.rs:31:13
+  --> $DIR/label_break_value_continue.rs:32:13
    |
 LL |             continue; //~ ERROR unlabeled `continue` inside of a labeled block
    |             ^^^^^^^^ `continue` statements that would diverge to or through a labeled block need to bear a label
diff --git a/src/test/ui/label_break_value_unlabeled_break.rs b/src/test/ui/label_break_value_unlabeled_break.rs
index 38918da291c..454ebd4c6cf 100644
--- a/src/test/ui/label_break_value_unlabeled_break.rs
+++ b/src/test/ui/label_break_value_unlabeled_break.rs
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #![feature(label_break_value)]
+#![allow(unused_labels)]
 
 // Simple unlabeled break should yield in an error
 fn unlabeled_break_simple() {
diff --git a/src/test/ui/label_break_value_unlabeled_break.stderr b/src/test/ui/label_break_value_unlabeled_break.stderr
index 8a25975a7bd..62c4a12231b 100644
--- a/src/test/ui/label_break_value_unlabeled_break.stderr
+++ b/src/test/ui/label_break_value_unlabeled_break.stderr
@@ -1,11 +1,11 @@
 error[E0695]: unlabeled `break` inside of a labeled block
-  --> $DIR/label_break_value_unlabeled_break.rs:16:9
+  --> $DIR/label_break_value_unlabeled_break.rs:17:9
    |
 LL |         break; //~ ERROR unlabeled `break` inside of a labeled block
    |         ^^^^^ `break` statements that would diverge to or through a labeled block need to bear a label
 
 error[E0695]: unlabeled `break` inside of a labeled block
-  --> $DIR/label_break_value_unlabeled_break.rs:24:13
+  --> $DIR/label_break_value_unlabeled_break.rs:25:13
    |
 LL |             break; //~ ERROR unlabeled `break` inside of a labeled block
    |             ^^^^^ `break` statements that would diverge to or through a labeled block need to bear a label
diff --git a/src/test/ui/lint/unused_labels.rs b/src/test/ui/lint/unused_labels.rs
new file mode 100644
index 00000000000..23add604da6
--- /dev/null
+++ b/src/test/ui/lint/unused_labels.rs
@@ -0,0 +1,96 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// The output should warn when a loop label is not used. However, it
+// should also deal with the edge cases where a label is shadowed,
+// within nested loops
+
+// compile-pass
+
+#![feature(label_break_value)]
+#![warn(unused_labels)]
+
+fn main() {
+    'unused_while_label: while 0 == 0 {
+        //~^ WARN unused label
+    }
+
+    let opt = Some(0);
+    'unused_while_let_label: while let Some(_) = opt {
+        //~^ WARN unused label
+    }
+
+    'unused_for_label: for _ in 0..10 {
+        //~^ WARN unused label
+    }
+
+    'used_loop_label: loop {
+        break 'used_loop_label;
+    }
+
+    'used_loop_label_outer_1: for _ in 0..10 {
+        'used_loop_label_inner_1: for _ in 0..10 {
+            break 'used_loop_label_inner_1;
+        }
+        break 'used_loop_label_outer_1;
+    }
+
+    'used_loop_label_outer_2: for _ in 0..10 {
+        'unused_loop_label_inner_2: for _ in 0..10 {
+            //~^ WARN unused label
+            break 'used_loop_label_outer_2;
+        }
+    }
+
+    'unused_loop_label_outer_3: for _ in 0..10 {
+        //~^ WARN unused label
+        'used_loop_label_inner_3: for _ in 0..10 {
+            break 'used_loop_label_inner_3;
+        }
+    }
+
+    // You should be able to break the same label many times
+    'many_used: loop {
+        if true {
+            break 'many_used;
+        } else {
+            break 'many_used;
+        }
+    }
+
+    // Test breaking many times with the same inner label doesn't break the
+    // warning on the outer label
+    'many_used_shadowed: for _ in 0..10 {
+        //~^ WARN unused label
+        'many_used_shadowed: for _ in 0..10 {
+            //~^ WARN label name `'many_used_shadowed` shadows a label name that is already in scope
+            if 1 % 2 == 0 {
+                break 'many_used_shadowed;
+            } else {
+                break 'many_used_shadowed;
+            }
+        }
+    }
+
+    'unused_loop_label: loop {
+        //~^ WARN unused label
+        break;
+    }
+
+    // Make sure unused block labels give warnings...
+    'unused_block_label: {
+        //~^ WARN unused label
+    }
+
+    // ...and that used ones don't:
+    'used_block_label: {
+        break 'used_block_label;
+    }
+}
diff --git a/src/test/ui/lint/unused_labels.stderr b/src/test/ui/lint/unused_labels.stderr
new file mode 100644
index 00000000000..825f5e281f0
--- /dev/null
+++ b/src/test/ui/lint/unused_labels.stderr
@@ -0,0 +1,63 @@
+warning: unused label
+  --> $DIR/unused_labels.rs:21:5
+   |
+LL |     'unused_while_label: while 0 == 0 {
+   |     ^^^^^^^^^^^^^^^^^^^
+   |
+note: lint level defined here
+  --> $DIR/unused_labels.rs:18:9
+   |
+LL | #![warn(unused_labels)]
+   |         ^^^^^^^^^^^^^
+
+warning: unused label
+  --> $DIR/unused_labels.rs:26:5
+   |
+LL |     'unused_while_let_label: while let Some(_) = opt {
+   |     ^^^^^^^^^^^^^^^^^^^^^^^
+
+warning: unused label
+  --> $DIR/unused_labels.rs:30:5
+   |
+LL |     'unused_for_label: for _ in 0..10 {
+   |     ^^^^^^^^^^^^^^^^^
+
+warning: unused label
+  --> $DIR/unused_labels.rs:46:9
+   |
+LL |         'unused_loop_label_inner_2: for _ in 0..10 {
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+warning: unused label
+  --> $DIR/unused_labels.rs:52:5
+   |
+LL |     'unused_loop_label_outer_3: for _ in 0..10 {
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+warning: unused label
+  --> $DIR/unused_labels.rs:70:5
+   |
+LL |     'many_used_shadowed: for _ in 0..10 {
+   |     ^^^^^^^^^^^^^^^^^^^
+
+warning: unused label
+  --> $DIR/unused_labels.rs:82:5
+   |
+LL |     'unused_loop_label: loop {
+   |     ^^^^^^^^^^^^^^^^^^
+
+warning: unused label
+  --> $DIR/unused_labels.rs:88:5
+   |
+LL |     'unused_block_label: {
+   |     ^^^^^^^^^^^^^^^^^^^
+
+warning: label name `'many_used_shadowed` shadows a label name that is already in scope
+  --> $DIR/unused_labels.rs:72:9
+   |
+LL |     'many_used_shadowed: for _ in 0..10 {
+   |     ------------------- first declared here
+LL |         //~^ WARN unused label
+LL |         'many_used_shadowed: for _ in 0..10 {
+   |         ^^^^^^^^^^^^^^^^^^^ lifetime 'many_used_shadowed already in scope
+
diff --git a/src/test/ui/loops-reject-duplicate-labels-2.rs b/src/test/ui/loops-reject-duplicate-labels-2.rs
index 598c7370b89..b273e7a0c7c 100644
--- a/src/test/ui/loops-reject-duplicate-labels-2.rs
+++ b/src/test/ui/loops-reject-duplicate-labels-2.rs
@@ -18,6 +18,7 @@
 // discussed here:
 // https://internals.rust-lang.org/t/psa-rejecting-duplicate-loop-labels/1833
 
+#[allow(unused_labels)]
 pub fn foo() {
     { 'fl: for _ in 0..10 { break; } }
     { 'fl: loop { break; } }             //~ WARN label name `'fl` shadows a label name that is already in scope
diff --git a/src/test/ui/loops-reject-duplicate-labels-2.stderr b/src/test/ui/loops-reject-duplicate-labels-2.stderr
index 830270a99d1..41b4a850f1b 100644
--- a/src/test/ui/loops-reject-duplicate-labels-2.stderr
+++ b/src/test/ui/loops-reject-duplicate-labels-2.stderr
@@ -1,5 +1,5 @@
 warning: label name `'fl` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels-2.rs:23:7
+  --> $DIR/loops-reject-duplicate-labels-2.rs:24:7
    |
 LL |     { 'fl: for _ in 0..10 { break; } }
    |       --- first declared here
@@ -7,7 +7,7 @@ LL |     { 'fl: loop { break; } }             //~ WARN label name `'fl` shadows
    |       ^^^ lifetime 'fl already in scope
 
 warning: label name `'lf` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels-2.rs:25:7
+  --> $DIR/loops-reject-duplicate-labels-2.rs:26:7
    |
 LL |     { 'lf: loop { break; } }
    |       --- first declared here
@@ -15,7 +15,7 @@ LL |     { 'lf: for _ in 0..10 { break; } }   //~ WARN label name `'lf` shadows
    |       ^^^ lifetime 'lf already in scope
 
 warning: label name `'wl` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels-2.rs:27:7
+  --> $DIR/loops-reject-duplicate-labels-2.rs:28:7
    |
 LL |     { 'wl: while 2 > 1 { break; } }
    |       --- first declared here
@@ -23,7 +23,7 @@ LL |     { 'wl: loop { break; } }             //~ WARN label name `'wl` shadows
    |       ^^^ lifetime 'wl already in scope
 
 warning: label name `'lw` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels-2.rs:29:7
+  --> $DIR/loops-reject-duplicate-labels-2.rs:30:7
    |
 LL |     { 'lw: loop { break; } }
    |       --- first declared here
@@ -31,7 +31,7 @@ LL |     { 'lw: while 2 > 1 { break; } }      //~ WARN label name `'lw` shadows
    |       ^^^ lifetime 'lw already in scope
 
 warning: label name `'fw` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels-2.rs:31:7
+  --> $DIR/loops-reject-duplicate-labels-2.rs:32:7
    |
 LL |     { 'fw: for _ in 0..10 { break; } }
    |       --- first declared here
@@ -39,7 +39,7 @@ LL |     { 'fw: while 2 > 1 { break; } }      //~ WARN label name `'fw` shadows
    |       ^^^ lifetime 'fw already in scope
 
 warning: label name `'wf` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels-2.rs:33:7
+  --> $DIR/loops-reject-duplicate-labels-2.rs:34:7
    |
 LL |     { 'wf: while 2 > 1 { break; } }
    |       --- first declared here
@@ -47,7 +47,7 @@ LL |     { 'wf: for _ in 0..10 { break; } }   //~ WARN label name `'wf` shadows
    |       ^^^ lifetime 'wf already in scope
 
 warning: label name `'tl` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels-2.rs:35:7
+  --> $DIR/loops-reject-duplicate-labels-2.rs:36:7
    |
 LL |     { 'tl: while let Some(_) = None::<i32> { break; } }
    |       --- first declared here
@@ -55,7 +55,7 @@ LL |     { 'tl: loop { break; } }             //~ WARN label name `'tl` shadows
    |       ^^^ lifetime 'tl already in scope
 
 warning: label name `'lt` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels-2.rs:37:7
+  --> $DIR/loops-reject-duplicate-labels-2.rs:38:7
    |
 LL |     { 'lt: loop { break; } }
    |       --- first declared here
@@ -63,7 +63,7 @@ LL |     { 'lt: while let Some(_) = None::<i32> { break; } }
    |       ^^^ lifetime 'lt already in scope
 
 error: compilation successful
-  --> $DIR/loops-reject-duplicate-labels-2.rs:42:1
+  --> $DIR/loops-reject-duplicate-labels-2.rs:43:1
    |
 LL | / pub fn main() { //~ ERROR compilation successful
 LL | |     foo();
diff --git a/src/test/ui/loops-reject-duplicate-labels.rs b/src/test/ui/loops-reject-duplicate-labels.rs
index d768b002ab1..ad24f69871c 100644
--- a/src/test/ui/loops-reject-duplicate-labels.rs
+++ b/src/test/ui/loops-reject-duplicate-labels.rs
@@ -15,6 +15,7 @@
 // Issue #21633: reject duplicate loop labels in function bodies.
 // This is testing the exact cases that are in the issue description.
 
+#[allow(unused_labels)]
 fn foo() {
     'fl: for _ in 0..10 { break; }
     'fl: loop { break; }           //~ WARN label name `'fl` shadows a label name that is already in scope
diff --git a/src/test/ui/loops-reject-duplicate-labels.stderr b/src/test/ui/loops-reject-duplicate-labels.stderr
index a71f98b812a..d0cb81544f8 100644
--- a/src/test/ui/loops-reject-duplicate-labels.stderr
+++ b/src/test/ui/loops-reject-duplicate-labels.stderr
@@ -1,5 +1,5 @@
 warning: label name `'fl` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels.rs:20:5
+  --> $DIR/loops-reject-duplicate-labels.rs:21:5
    |
 LL |     'fl: for _ in 0..10 { break; }
    |     --- first declared here
@@ -7,7 +7,7 @@ LL |     'fl: loop { break; }           //~ WARN label name `'fl` shadows a labe
    |     ^^^ lifetime 'fl already in scope
 
 warning: label name `'lf` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels.rs:23:5
+  --> $DIR/loops-reject-duplicate-labels.rs:24:5
    |
 LL |     'lf: loop { break; }
    |     --- first declared here
@@ -15,7 +15,7 @@ LL |     'lf: for _ in 0..10 { break; } //~ WARN label name `'lf` shadows a labe
    |     ^^^ lifetime 'lf already in scope
 
 warning: label name `'wl` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels.rs:25:5
+  --> $DIR/loops-reject-duplicate-labels.rs:26:5
    |
 LL |     'wl: while 2 > 1 { break; }
    |     --- first declared here
@@ -23,7 +23,7 @@ LL |     'wl: loop { break; }           //~ WARN label name `'wl` shadows a labe
    |     ^^^ lifetime 'wl already in scope
 
 warning: label name `'lw` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels.rs:27:5
+  --> $DIR/loops-reject-duplicate-labels.rs:28:5
    |
 LL |     'lw: loop { break; }
    |     --- first declared here
@@ -31,7 +31,7 @@ LL |     'lw: while 2 > 1 { break; }    //~ WARN label name `'lw` shadows a labe
    |     ^^^ lifetime 'lw already in scope
 
 warning: label name `'fw` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels.rs:29:5
+  --> $DIR/loops-reject-duplicate-labels.rs:30:5
    |
 LL |     'fw: for _ in 0..10 { break; }
    |     --- first declared here
@@ -39,7 +39,7 @@ LL |     'fw: while 2 > 1 { break; }    //~ WARN label name `'fw` shadows a labe
    |     ^^^ lifetime 'fw already in scope
 
 warning: label name `'wf` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels.rs:31:5
+  --> $DIR/loops-reject-duplicate-labels.rs:32:5
    |
 LL |     'wf: while 2 > 1 { break; }
    |     --- first declared here
@@ -47,7 +47,7 @@ LL |     'wf: for _ in 0..10 { break; } //~ WARN label name `'wf` shadows a labe
    |     ^^^ lifetime 'wf already in scope
 
 warning: label name `'tl` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels.rs:33:5
+  --> $DIR/loops-reject-duplicate-labels.rs:34:5
    |
 LL |     'tl: while let Some(_) = None::<i32> { break; }
    |     --- first declared here
@@ -55,7 +55,7 @@ LL |     'tl: loop { break; }           //~ WARN label name `'tl` shadows a labe
    |     ^^^ lifetime 'tl already in scope
 
 warning: label name `'lt` shadows a label name that is already in scope
-  --> $DIR/loops-reject-duplicate-labels.rs:35:5
+  --> $DIR/loops-reject-duplicate-labels.rs:36:5
    |
 LL |     'lt: loop { break; }
    |     --- first declared here
@@ -63,7 +63,7 @@ LL |     'lt: while let Some(_) = None::<i32> { break; }
    |     ^^^ lifetime 'lt already in scope
 
 error: compilation successful
-  --> $DIR/loops-reject-duplicate-labels.rs:49:1
+  --> $DIR/loops-reject-duplicate-labels.rs:50:1
    |
 LL | / pub fn main() { //~ ERROR compilation successful
 LL | |     let s = S;
diff --git a/src/test/ui/suggestions/suggest-labels.rs b/src/test/ui/suggestions/suggest-labels.rs
index 8c97301f40b..9fb519c57ed 100644
--- a/src/test/ui/suggestions/suggest-labels.rs
+++ b/src/test/ui/suggestions/suggest-labels.rs
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-#[allow(unreachable_code)]
+#[allow(unreachable_code, unused_labels)]
 fn main() {
     'foo: loop {
         break 'fo; //~ ERROR use of undeclared label