2014-02-05 16:33:10 -06:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
2014-10-27 17:37:07 -05:00
|
|
|
#![deny(unused_variables)]
|
|
|
|
#![deny(unused_assignments)]
|
2015-03-23 17:23:34 -05:00
|
|
|
#![allow(dead_code, non_camel_case_types, trivial_numeric_casts)]
|
2013-04-09 12:16:27 -05:00
|
|
|
|
2016-03-04 14:44:45 -06:00
|
|
|
use std::ops::AddAssign;
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn f1(x: isize) {
|
2013-04-09 12:16:27 -05:00
|
|
|
//~^ ERROR unused variable: `x`
|
2012-05-23 22:53:49 -05:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn f1b(x: &mut isize) {
|
2013-04-09 12:16:27 -05:00
|
|
|
//~^ ERROR unused variable: `x`
|
2012-05-23 22:53:49 -05:00
|
|
|
}
|
|
|
|
|
2014-10-27 17:37:07 -05:00
|
|
|
#[allow(unused_variables)]
|
2015-01-08 04:54:35 -06:00
|
|
|
fn f1c(x: isize) {}
|
2013-04-09 12:16:27 -05:00
|
|
|
|
2014-03-06 08:58:34 -06:00
|
|
|
fn f1d() {
|
2015-01-08 04:54:35 -06:00
|
|
|
let x: isize;
|
2014-03-06 08:58:34 -06:00
|
|
|
//~^ ERROR unused variable: `x`
|
|
|
|
}
|
|
|
|
|
2012-05-23 22:53:49 -05:00
|
|
|
fn f2() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let x = 3;
|
2013-04-09 12:16:27 -05:00
|
|
|
//~^ ERROR unused variable: `x`
|
2012-05-23 22:53:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn f3() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let mut x = 3;
|
2013-04-09 12:16:27 -05:00
|
|
|
//~^ ERROR variable `x` is assigned to, but never used
|
2015-01-31 10:23:42 -06:00
|
|
|
x += 4;
|
2013-04-09 12:16:27 -05:00
|
|
|
//~^ ERROR value assigned to `x` is never read
|
2012-05-23 22:53:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn f3b() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let mut z = 3;
|
2013-04-09 12:16:27 -05:00
|
|
|
//~^ ERROR variable `z` is assigned to, but never used
|
2012-05-23 22:53:49 -05:00
|
|
|
loop {
|
2015-01-31 10:23:42 -06:00
|
|
|
z += 4;
|
2012-05-23 22:53:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 17:37:07 -05:00
|
|
|
#[allow(unused_variables)]
|
2013-04-09 12:16:27 -05:00
|
|
|
fn f3c() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let mut z = 3;
|
|
|
|
loop { z += 4; }
|
2013-04-09 12:16:27 -05:00
|
|
|
}
|
|
|
|
|
2014-10-27 17:37:07 -05:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
#[allow(unused_assignments)]
|
2013-04-09 12:16:27 -05:00
|
|
|
fn f3d() {
|
2015-01-31 10:23:42 -06:00
|
|
|
let mut x = 3;
|
|
|
|
x += 4;
|
2013-04-09 12:16:27 -05:00
|
|
|
}
|
|
|
|
|
2012-05-23 22:53:49 -05:00
|
|
|
fn f4() {
|
2015-01-31 10:23:42 -06:00
|
|
|
match Some(3) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(i) => {
|
2013-04-09 12:16:27 -05:00
|
|
|
//~^ ERROR unused variable: `i`
|
2012-05-23 22:53:49 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {}
|
2012-05-23 22:53:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 13:04:07 -05:00
|
|
|
enum tri {
|
2015-01-08 04:54:35 -06:00
|
|
|
a(isize), b(isize), c(isize)
|
2012-08-24 13:04:07 -05:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn f4b() -> isize {
|
2015-01-31 10:23:42 -06:00
|
|
|
match tri::a(3) {
|
2014-11-06 02:05:53 -06:00
|
|
|
tri::a(i) | tri::b(i) | tri::c(i) => {
|
2012-08-24 13:04:07 -05:00
|
|
|
i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 15:07:00 -05:00
|
|
|
fn f5a() {
|
2015-01-31 10:23:42 -06:00
|
|
|
for x in 1..10 { }
|
2014-09-15 15:07:00 -05:00
|
|
|
//~^ ERROR unused variable: `x`
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f5b() {
|
2015-01-31 10:23:42 -06:00
|
|
|
for (x, _) in [1, 2, 3].iter().enumerate() { }
|
2014-09-15 15:07:00 -05:00
|
|
|
//~^ ERROR unused variable: `x`
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f5c() {
|
2015-01-31 10:23:42 -06:00
|
|
|
for (_, x) in [1, 2, 3].iter().enumerate() {
|
2014-09-15 15:07:00 -05:00
|
|
|
//~^ ERROR unused variable: `x`
|
|
|
|
continue;
|
2015-01-27 14:20:58 -06:00
|
|
|
drop(*x as i32); //~ WARNING unreachable statement
|
2014-09-15 15:07:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 14:44:45 -06:00
|
|
|
struct View<'a>(&'a mut [i32]);
|
|
|
|
|
|
|
|
impl<'a> AddAssign<i32> for View<'a> {
|
|
|
|
fn add_assign(&mut self, rhs: i32) {
|
|
|
|
for lhs in self.0.iter_mut() {
|
|
|
|
*lhs += rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f6() {
|
|
|
|
let mut array = [1, 2, 3];
|
|
|
|
let mut v = View(&mut array);
|
|
|
|
|
|
|
|
// ensure an error shows up for x even if lhs of an overloaded add assign
|
|
|
|
|
|
|
|
let x;
|
|
|
|
//~^ ERROR variable `x` is assigned to, but never used
|
|
|
|
|
|
|
|
*({
|
|
|
|
x = 0; //~ ERROR value assigned to `x` is never read
|
|
|
|
&mut v
|
|
|
|
}) += 1;
|
|
|
|
}
|
|
|
|
|
2016-03-04 15:52:34 -06:00
|
|
|
|
|
|
|
struct MutRef<'a>(&'a mut i32);
|
|
|
|
|
|
|
|
impl<'a> AddAssign<i32> for MutRef<'a> {
|
|
|
|
fn add_assign(&mut self, rhs: i32) {
|
|
|
|
*self.0 += rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f7() {
|
|
|
|
let mut a = 1;
|
|
|
|
{
|
|
|
|
// `b` does not trigger unused_variables
|
|
|
|
let mut b = MutRef(&mut a);
|
|
|
|
b += 1;
|
|
|
|
}
|
|
|
|
drop(a);
|
|
|
|
}
|
|
|
|
|
2012-05-23 22:53:49 -05:00
|
|
|
fn main() {
|
|
|
|
}
|