rust/tests/ui/liveness/liveness-unused.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 lines
2.4 KiB
Rust
Raw Normal View History

rustc: Rearchitect lints to be emitted more eagerly In preparation for incremental compilation this commit refactors the lint handling infrastructure in the compiler to be more "eager" and overall more incremental-friendly. Many passes of the compiler can emit lints at various points but before this commit all lints were buffered in a table to be emitted at the very end of compilation. This commit changes these lints to be emitted immediately during compilation using pre-calculated lint level-related data structures. Linting today is split into two phases, one set of "early" lints run on the `syntax::ast` and a "late" set of lints run on the HIR. This commit moves the "early" lints to running as late as possible in compilation, just before HIR lowering. This notably means that we're catching resolve-related lints just before HIR lowering. The early linting remains a pass very similar to how it was before, maintaining context of the current lint level as it walks the tree. Post-HIR, however, linting is structured as a method on the `TyCtxt` which transitively executes a query to calculate lint levels. Each request to lint on a `TyCtxt` will query the entire crate's 'lint level data structure' and then go from there about whether the lint should be emitted or not. The query depends on the entire HIR crate but should be very quick to calculate (just a quick walk of the HIR) and the red-green system should notice that the lint level data structure rarely changes, and should hopefully preserve incrementality. Overall this resulted in a pretty big change to the test suite now that lints are emitted much earlier in compilation (on-demand vs only at the end). This in turn necessitated the addition of many `#![allow(warnings)]` directives throughout the compile-fail test suite and a number of updates to the UI test suite.
2017-07-27 04:51:09 +00:00
#![warn(unused)]
2014-10-27 22:37:07 +00:00
#![deny(unused_variables)]
#![deny(unused_assignments)]
#![allow(dead_code, non_camel_case_types, trivial_numeric_casts, dropping_copy_types)]
use std::ops::AddAssign;
fn f1(x: isize) {
//~^ ERROR unused variable: `x`
}
fn f1b(x: &mut isize) {
//~^ ERROR unused variable: `x`
}
2014-10-27 22:37:07 +00:00
#[allow(unused_variables)]
fn f1c(x: isize) {}
fn f1d() {
let x: isize;
//~^ ERROR unused variable: `x`
}
fn f2() {
2015-01-31 16:23:42 +00:00
let x = 3;
//~^ ERROR unused variable: `x`
}
fn f3() {
2015-01-31 16:23:42 +00:00
let mut x = 3;
//~^ ERROR variable `x` is assigned to, but never used
2015-01-31 16:23:42 +00:00
x += 4;
//~^ ERROR value assigned to `x` is never read
}
fn f3b() {
2015-01-31 16:23:42 +00:00
let mut z = 3;
//~^ ERROR variable `z` is assigned to, but never used
loop {
2015-01-31 16:23:42 +00:00
z += 4;
}
}
2014-10-27 22:37:07 +00:00
#[allow(unused_variables)]
fn f3c() {
2015-01-31 16:23:42 +00:00
let mut z = 3;
loop { z += 4; }
}
2014-10-27 22:37:07 +00:00
#[allow(unused_variables)]
#[allow(unused_assignments)]
fn f3d() {
2015-01-31 16:23:42 +00:00
let mut x = 3;
x += 4;
}
fn f4() {
2015-01-31 16:23:42 +00:00
match Some(3) {
2012-08-20 19:23:37 +00:00
Some(i) => {
//~^ ERROR unused variable: `i`
}
2012-08-20 19:23:37 +00:00
None => {}
}
}
enum tri {
a(isize), b(isize), c(isize)
}
fn f4b() -> isize {
2015-01-31 16:23:42 +00:00
match tri::a(3) {
tri::a(i) | tri::b(i) | tri::c(i) => {
i
}
}
}
fn f5a() {
2015-01-31 16:23:42 +00:00
for x in 1..10 { }
//~^ ERROR unused variable: `x`
}
fn f5b() {
2015-01-31 16:23:42 +00:00
for (x, _) in [1, 2, 3].iter().enumerate() { }
//~^ ERROR unused variable: `x`
}
fn f5c() {
2015-01-31 16:23:42 +00: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 20:20:58 +00: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() {
}