]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/shadow.rs
Rewrite shadow lint
[rust.git] / tests / ui / shadow.rs
index a607161a949ee978f395587638bcd80bd84056d8..02e838456d0b574559f34404e79f22ffa158100e 100644 (file)
@@ -1,48 +1,77 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// 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.
+#![warn(clippy::shadow_same, clippy::shadow_reuse, clippy::shadow_unrelated)]
 
+fn shadow_same() {
+    let x = 1;
+    let x = x;
+    let mut x = &x;
+    let x = &mut x;
+    let x = *x;
+}
 
-#![feature(tool_lints)]
-
-
-#![warn(clippy::all, clippy::pedantic, clippy::shadow_same, clippy::shadow_reuse, clippy::shadow_unrelated)]
-#![allow(unused_parens, unused_variables, clippy::missing_docs_in_private_items)]
-
-fn id<T>(x: T) -> T { x }
+fn shadow_reuse() -> Option<()> {
+    let x = ([[0]], ());
+    let x = x.0;
+    let x = x[0];
+    let [x] = x;
+    let x = Some(x);
+    let x = foo(x);
+    let x = || x;
+    let x = Some(1).map(|_| x)?;
+    None
+}
 
-fn first(x: (isize, isize)) -> isize { x.0 }
+fn shadow_unrelated() {
+    let x = 1;
+    let x = 2;
+}
 
-fn main() {
-    let mut x = 1;
-    let x = &mut x;
-    let x = { x };
-    let x = (&*x);
-    let x = { *x + 1 };
-    let x = id(x);
-    let x = (1, x);
-    let x = first(x);
-    let y = 1;
-    let x = y;
-
-    let x;
-    x = 42;
-
-    let o = Some(1_u8);
-
-    if let Some(p) = o { assert_eq!(1, p); }
-    match o {
-        Some(p) => p, // no error, because the p above is in its own scope
-        None => 0,
+fn syntax() {
+    fn f(x: u32) {
+        let x = 1;
+    }
+    let x = 1;
+    match Some(1) {
+        Some(1) => {},
+        Some(x) => {
+            let x = 1;
+        },
+        _ => {},
+    }
+    if let Some(x) = Some(1) {}
+    while let Some(x) = Some(1) {}
+    let _ = |[x]: [u32; 1]| {
+        let x = 1;
     };
+}
 
-    match (x, o) {
-        (1, Some(a)) | (a, Some(1)) => (), // no error though `a` appears twice
-        _ => (),
+fn negative() {
+    match Some(1) {
+        Some(x) if x == 1 => {},
+        Some(x) => {},
+        None => {},
+    }
+    match [None, Some(1)] {
+        [Some(x), None] | [None, Some(x)] => {},
+        _ => {},
+    }
+    if let Some(x) = Some(1) {
+        let y = 1;
+    } else {
+        let x = 1;
+        let y = 1;
     }
+    let x = 1;
+    #[allow(clippy::shadow_unrelated)]
+    let x = 1;
 }
+
+fn foo<T>(_: T) {}
+
+fn question_mark() -> Option<()> {
+    let val = 1;
+    // `?` expands with a `val` binding
+    None?;
+    None
+}
+
+fn main() {}