]> git.lizzy.rs Git - rust.git/commitdiff
Fix Range warning and improve tests
authorKevin Leimkuhler <kevin@kleimkuhler.com>
Sun, 7 Oct 2018 02:48:04 +0000 (19:48 -0700)
committerKevin Leimkuhler <kevin@kleimkuhler.com>
Wed, 10 Oct 2018 04:10:27 +0000 (21:10 -0700)
src/librustc_lint/unused.rs
src/test/ui/lint/issue-54538-unused-parens-lint.rs
src/test/ui/lint/issue-54538-unused-parens-lint.stderr

index 0ef46e77280db99c8e457d0589b35f24e4b32efe..050e52d2e0cdd827b1932908fa36d62527a09411 100644 (file)
@@ -376,8 +376,12 @@ fn check_expr(&mut self, cx: &EarlyContext, e: &ast::Expr) {
     }
 
     fn check_pat(&mut self, cx: &EarlyContext, p: &ast::Pat) {
-        if let ast::PatKind::Paren(_) = p.node {
-            self.check_unused_parens_pat(cx, &p, "pattern");
+        use ast::PatKind::{Paren, Range};
+        if let Paren(ref pat) = p.node {
+            match pat.node {
+                Range(..) => {}
+                _ => self.check_unused_parens_pat(cx, &p, "pattern")
+            }
         }
     }
 
index 5702a8941d4663f45c0732c782355cecb4cac0b1..97a2dd59a6209d0332099fca262f65fb4e37cae8 100644 (file)
 #![allow(unused_variables)]
 #![warn(unused_parens)]
 
-struct A {
-    field: Option<String>,
-}
-
 fn main() {
-    let x = 3;
-    match x {
-        (_) => {}     //~ WARNING: unnecessary parentheses around pattern
-        (y) => {}     //~ WARNING: unnecessary parentheses around pattern
-        (ref r) => {} //~ WARNING: unnecessary parentheses around pattern
-        e @ 1...2 | (e @ (3...4)) => {}
-        //~^ WARNING: unnecessary parentheses around pattern (3 ... 4)
-        //~^ WARNING: unnecessary parentheses around pattern (e @ _)
+    match 1 {
+        (_) => {}         //~ WARNING: unnecessary parentheses around pattern
+        (y) => {}         //~ WARNING: unnecessary parentheses around pattern
+        (ref r) => {}     //~ WARNING: unnecessary parentheses around pattern
+        (e @ 1..=2) => {} //~ WARNING: unnecessary parentheses around outer pattern
+        (1..=2) => {}     // Non ambiguous range pattern should not warn
+        e @ (3..=4) => {} // Non ambiguous range pattern should not warn
+    }
+
+    match &1 {
+        (e @ &(1...2)) => {} //~ WARNING: unnecessary parentheses around outer pattern
+        &(_) => {}           //~ WARNING: unnecessary parentheses around pattern
+        e @ &(1...2) => {}   // Ambiguous range pattern should not warn
+        &(1..=2) => {}       // Ambiguous range pattern should not warn
     }
 
-    let field = "foo".to_string();
-    let x: Option<A> = Some(A { field: Some(field) });
-    match x {
-        Some(A {
-            field: (ref a @ Some(_)),
-            //~^ WARNING: unnecessary parentheses around pattern
-            ..
-        }) => {}
-        _ => {}
+    match &1 {
+        e @ &(1...2) | e @ &(3..=4) => {} // Complex ambiguous pattern should not warn
+        &_ => {}
     }
 }
index 7d9eb697fd7ea88afa88596f60188b7df3ba91b7..b76b969fd2b1a8ab1693933076b75d177db00149 100644 (file)
@@ -1,7 +1,7 @@
 warning: unnecessary parentheses around pattern
-  --> $DIR/issue-54538-unused-parens-lint.rs:24:9
+  --> $DIR/issue-54538-unused-parens-lint.rs:19:9
    |
-LL |         (_) => {}     //~ WARNING: unnecessary parentheses around pattern
+LL |         (_) => {}         //~ WARNING: unnecessary parentheses around pattern
    |         ^^^ help: remove these parentheses
    |
 note: lint level defined here
@@ -11,32 +11,32 @@ LL | #![warn(unused_parens)]
    |         ^^^^^^^^^^^^^
 
 warning: unnecessary parentheses around pattern
-  --> $DIR/issue-54538-unused-parens-lint.rs:25:9
+  --> $DIR/issue-54538-unused-parens-lint.rs:20:9
    |
-LL |         (y) => {}     //~ WARNING: unnecessary parentheses around pattern
+LL |         (y) => {}         //~ WARNING: unnecessary parentheses around pattern
    |         ^^^ help: remove these parentheses
 
 warning: unnecessary parentheses around pattern
-  --> $DIR/issue-54538-unused-parens-lint.rs:26:9
+  --> $DIR/issue-54538-unused-parens-lint.rs:21:9
    |
-LL |         (ref r) => {} //~ WARNING: unnecessary parentheses around pattern
+LL |         (ref r) => {}     //~ WARNING: unnecessary parentheses around pattern
    |         ^^^^^^^ help: remove these parentheses
 
 warning: unnecessary parentheses around pattern
-  --> $DIR/issue-54538-unused-parens-lint.rs:27:21
+  --> $DIR/issue-54538-unused-parens-lint.rs:22:9
    |
-LL |         e @ 1...2 | (e @ (3...4)) => {}
-   |                     ^^^^^^^^^^^^^ help: remove these parentheses
+LL |         (e @ 1..=2) => {} //~ WARNING: unnecessary parentheses around outer pattern
+   |         ^^^^^^^^^^^ help: remove these parentheses
 
 warning: unnecessary parentheses around pattern
-  --> $DIR/issue-54538-unused-parens-lint.rs:27:26
+  --> $DIR/issue-54538-unused-parens-lint.rs:28:9
    |
-LL |         e @ 1...2 | (e @ (3...4)) => {}
-   |                          ^^^^^^^ help: remove these parentheses
+LL |         (e @ &(1...2)) => {} //~ WARNING: unnecessary parentheses around outer pattern
+   |         ^^^^^^^^^^^^^^ help: remove these parentheses
 
 warning: unnecessary parentheses around pattern
-  --> $DIR/issue-54538-unused-parens-lint.rs:36:20
+  --> $DIR/issue-54538-unused-parens-lint.rs:29:10
    |
-LL |             field: (ref a @ Some(_)),
-   |                    ^^^^^^^^^^^^^^^^^ help: remove these parentheses
+LL |         &(_) => {}           //~ WARNING: unnecessary parentheses around pattern
+   |          ^^^ help: remove these parentheses