rust/src/test/compile-fail/liveness-unused.rs

151 lines
2.8 KiB
Rust
Raw Normal View History

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)]
use std::ops::AddAssign;
fn f1(x: isize) {
//~^ ERROR unused variable: `x`
}
fn f1b(x: &mut isize) {
//~^ ERROR unused variable: `x`
}
2014-10-27 17:37:07 -05:00
#[allow(unused_variables)]
fn f1c(x: isize) {}
fn f1d() {
let x: isize;
//~^ ERROR unused variable: `x`
}
fn f2() {
2015-01-31 10:23:42 -06:00
let x = 3;
//~^ ERROR unused variable: `x`
}
fn f3() {
2015-01-31 10:23:42 -06:00
let mut x = 3;
//~^ ERROR variable `x` is assigned to, but never used
2015-01-31 10:23:42 -06:00
x += 4;
//~^ ERROR value assigned to `x` is never read
}
fn f3b() {
2015-01-31 10:23:42 -06:00
let mut z = 3;
//~^ ERROR variable `z` is assigned to, but never used
loop {
2015-01-31 10:23:42 -06:00
z += 4;
}
}
2014-10-27 17:37:07 -05:00
#[allow(unused_variables)]
fn f3c() {
2015-01-31 10:23:42 -06:00
let mut z = 3;
loop { z += 4; }
}
2014-10-27 17:37:07 -05:00
#[allow(unused_variables)]
#[allow(unused_assignments)]
fn f3d() {
2015-01-31 10:23:42 -06:00
let mut x = 3;
x += 4;
}
fn f4() {
2015-01-31 10:23:42 -06:00
match Some(3) {
2012-08-20 14:23:37 -05:00
Some(i) => {
//~^ ERROR unused variable: `i`
}
2012-08-20 14:23:37 -05:00
None => {}
}
}
enum tri {
a(isize), b(isize), c(isize)
}
fn f4b() -> isize {
2015-01-31 10:23:42 -06:00
match tri::a(3) {
tri::a(i) | tri::b(i) | tri::c(i) => {
i
}
}
}
fn f5a() {
2015-01-31 10:23:42 -06:00
for x in 1..10 { }
//~^ ERROR unused variable: `x`
}
fn f5b() {
2015-01-31 10:23:42 -06:00
for (x, _) in [1, 2, 3].iter().enumerate() { }
//~^ ERROR unused variable: `x`
}
fn f5c() {
2015-01-31 10:23:42 -06:00
for (_, x) in [1, 2, 3].iter().enumerate() {
//~^ ERROR unused variable: `x`
continue;
std: Add a new `env` module This is an implementation of [RFC 578][rfc] which adds a new `std::env` module to replace most of the functionality in the current `std::os` module. More details can be found in the RFC itself, but as a summary the following methods have all been deprecated: [rfc]: https://github.com/rust-lang/rfcs/pull/578 * `os::args_as_bytes` => `env::args` * `os::args` => `env::args` * `os::consts` => `env::consts` * `os::dll_filename` => no replacement, use `env::consts` directly * `os::page_size` => `env::page_size` * `os::make_absolute` => use `env::current_dir` + `join` instead * `os::getcwd` => `env::current_dir` * `os::change_dir` => `env::set_current_dir` * `os::homedir` => `env::home_dir` * `os::tmpdir` => `env::temp_dir` * `os::join_paths` => `env::join_paths` * `os::split_paths` => `env::split_paths` * `os::self_exe_name` => `env::current_exe` * `os::self_exe_path` => use `env::current_exe` + `pop` * `os::set_exit_status` => `env::set_exit_status` * `os::get_exit_status` => `env::get_exit_status` * `os::env` => `env::vars` * `os::env_as_bytes` => `env::vars` * `os::getenv` => `env::var` or `env::var_string` * `os::getenv_as_bytes` => `env::var` * `os::setenv` => `env::set_var` * `os::unsetenv` => `env::remove_var` Many function signatures have also been tweaked for various purposes, but the main changes were: * `Vec`-returning APIs now all return iterators instead * All APIs are now centered around `OsString` instead of `Vec<u8>` or `String`. There is currently on convenience API, `env::var_string`, which can be used to get the value of an environment variable as a unicode `String`. All old APIs are `#[deprecated]` in-place and will remain for some time to allow for migrations. The semantics of the APIs have been tweaked slightly with regard to dealing with invalid unicode (panic instead of replacement). The new `std::env` module is all contained within the `env` feature, so crates must add the following to access the new APIs: #![feature(env)] [breaking-change]
2015-01-27 14:20:58 -06:00
drop(*x as i32); //~ WARNING unreachable statement
}
}
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;
}
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);
}
fn main() {
}