diff --git a/Cargo.toml b/Cargo.toml index 157a0d18a99..38fcae19354 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,12 +40,12 @@ path = "src/driver.rs" clippy_lints = { version = "0.0.170", path = "clippy_lints" } # end automatic update cargo_metadata = "0.2" +regex = "0.2" [dev-dependencies] compiletest_rs = "0.3" duct = "0.8.2" lazy_static = "0.2" -regex = "0.2" serde_derive = "1.0" clippy-mini-macro-test = { version = "0.1", path = "mini-macro" } serde = "1.0" diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 275508a372a..b53b34b3b2b 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -183,17 +183,28 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { match utils::conf::lookup_conf_file() { Ok(path) => path, Err(error) => { - reg.sess.struct_err(&format!("error reading Clippy's configuration file: {}", error)).emit(); + reg.sess.struct_err(&format!("error finding Clippy's configuration file: {}", error)).emit(); None } } }; + let file_name = file_name.map(|file_name| if file_name.is_relative() { + reg.sess + .local_crate_source_file + .as_ref() + .and_then(|file| std::path::Path::new(&file).parent().map(std::path::Path::to_path_buf)) + .unwrap_or_default() + .join(file_name) + } else { + file_name + }); + let (conf, errors) = utils::conf::read(file_name.as_ref().map(|p| p.as_ref())); // all conf errors are non-fatal, we just use the default conf in case of error for error in errors { - reg.sess.struct_err(&format!("error reading Clippy's configuration file: {}", error)).emit(); + reg.sess.struct_err(&format!("error reading Clippy's configuration file `{}`: {}", file_name.as_ref().and_then(|p| p.to_str()).unwrap_or(""), error)).emit(); } conf diff --git a/src/driver.rs b/src/driver.rs index 8e88cc2e346..090e69cf027 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -129,33 +129,29 @@ fn show_version() { pub fn main() { use std::env; - if env::var("CLIPPY_DOGFOOD").is_ok() { - panic!("yummy"); - } - if std::env::args().any(|a| a == "--version" || a == "-V") { show_version(); return; } - let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME")); - let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN")); - let sys_root = if let (Some(home), Some(toolchain)) = (home, toolchain) { - format!("{}/toolchains/{}", home, toolchain) - } else { - option_env!("SYSROOT") - .map(|s| s.to_owned()) - .or_else(|| { - Command::new("rustc") - .arg("--print") - .arg("sysroot") - .output() - .ok() - .and_then(|out| String::from_utf8(out.stdout).ok()) - .map(|s| s.trim().to_owned()) - }) - .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust") - }; + let sys_root = option_env!("SYSROOT") + .map(String::from) + .or_else(|| std::env::var("SYSROOT").ok()) + .or_else(|| { + let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME")); + let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN")); + home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain))) + }) + .or_else(|| { + Command::new("rustc") + .arg("--print") + .arg("sysroot") + .output() + .ok() + .and_then(|out| String::from_utf8(out.stdout).ok()) + .map(|s| s.trim().to_owned()) + }) + .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust"); rustc_driver::in_rustc_thread(|| { // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument. diff --git a/src/main.rs b/src/main.rs index 10c0360a267..c613f029b16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,12 +49,6 @@ fn show_version() { } pub fn main() { - use std::env; - - if env::var("CLIPPY_DOGFOOD").is_ok() { - panic!("yummy"); - } - // Check for version and help flags even when invoked as 'cargo-clippy' if std::env::args().any(|a| a == "--help" || a == "-h") { show_help(); diff --git a/tests/compile-test.rs b/tests/compile-test.rs index bd968cc6009..51ab6aee3a4 100644 --- a/tests/compile-test.rs +++ b/tests/compile-test.rs @@ -1,6 +1,9 @@ -extern crate compiletest_rs as compiletest; +#![feature(test)] -use std::path::PathBuf; +extern crate compiletest_rs as compiletest; +extern crate test; + +use std::path::{PathBuf, Path}; use std::env::{set_var, var}; fn clippy_driver_path() -> PathBuf { @@ -11,16 +14,37 @@ fn clippy_driver_path() -> PathBuf { } } -fn run_mode(dir: &'static str, mode: &'static str) { +fn host_libs() -> PathBuf { + if let Some(path) = option_env!("HOST_LIBS") { + PathBuf::from(path) + } else { + Path::new("target").join(env!("PROFILE")) + } +} + +fn rustc_test_suite() -> Option { + option_env!("RUSTC_TEST_SUITE").map(PathBuf::from) +} + +fn rustc_lib_path() -> PathBuf { + option_env!("RUSTC_LIB_PATH").unwrap().into() +} + +fn config(dir: &'static str, mode: &'static str) -> compiletest::Config { let mut config = compiletest::Config::default(); let cfg_mode = mode.parse().expect("Invalid mode"); - config.target_rustcflags = Some("-L target/debug/ -L target/debug/deps -Dwarnings".to_owned()); if let Ok(name) = var::<&str>("TESTNAME") { let s: String = name.to_owned(); config.filter = Some(s) } + if rustc_test_suite().is_some() { + config.run_lib_path = rustc_lib_path(); + config.compile_lib_path = rustc_lib_path(); + } + config.target_rustcflags = Some(format!("-L {0} -L {0}/deps -Dwarnings", host_libs().display())); + config.mode = cfg_mode; config.build_base = { let mut path = std::env::current_dir().unwrap(); @@ -29,8 +53,11 @@ fn run_mode(dir: &'static str, mode: &'static str) { }; config.src_base = PathBuf::from(format!("tests/{}", dir)); config.rustc_path = clippy_driver_path(); + config +} - compiletest::run_tests(&config); +fn run_mode(dir: &'static str, mode: &'static str) { + compiletest::run_tests(&config(dir, mode)); } fn prepare_env() { @@ -45,3 +72,21 @@ fn compile_test() { run_mode("run-pass", "run-pass"); run_mode("ui", "ui"); } + +#[test] +fn dogfood() { + prepare_env(); + let files = ["src/main.rs", "src/driver.rs", "src/lib.rs", "clippy_lints/src/lib.rs"]; + let mut config = config("dogfood", "ui"); + config.target_rustcflags = config.target_rustcflags.map(|flags| format!("{} -Dclippy -Dclippy_pedantic -Dclippy_internal", flags)); + + for file in &files { + let paths = test::TestPaths { + base: PathBuf::new(), + file: PathBuf::from(file), + relative_dir: PathBuf::new(), + }; + + compiletest::runtest::run(config.clone(), &paths); + } +} diff --git a/tests/conf_whitelisted.rs b/tests/conf_whitelisted.rs index 5ada775560e..10a7d3e72d7 100644 --- a/tests/conf_whitelisted.rs +++ b/tests/conf_whitelisted.rs @@ -1,2 +1,2 @@ #![feature(plugin)] -#![plugin(clippy(conf_file = "./tests/auxiliary/conf_whitelisted.toml"))] +#![plugin(clippy(conf_file = "./auxiliary/conf_whitelisted.toml"))] diff --git a/tests/dogfood.rs b/tests/dogfood.rs deleted file mode 100644 index 4870bd285b4..00000000000 --- a/tests/dogfood.rs +++ /dev/null @@ -1,49 +0,0 @@ -#![feature(test, plugin)] -#![plugin(clippy)] -#![deny(clippy, clippy_pedantic)] - -extern crate compiletest_rs as compiletest; -extern crate test; - -use std::env::{set_var, var}; -use std::path::PathBuf; -use test::TestPaths; - -#[test] -fn dogfood() { - // don't run dogfood on travis, cargo-clippy already runs clippy on itself - if let Ok(travis) = var("TRAVIS") { - if travis == "true" { - return; - } - } - - let mut config = compiletest::Config::default(); - - let cfg_mode = "run-fail".parse().expect("Invalid mode"); - let mut s = String::new(); - s.push_str(" -L target/debug/"); - s.push_str(" -L target/debug/deps"); - s.push_str(" -Zextra-plugins=clippy -Ltarget_recur/debug -Dwarnings -Dclippy_pedantic -Dclippy -Dclippy_internal"); - config.target_rustcflags = Some(s); - if let Ok(name) = var("TESTNAME") { - config.filter = Some(name.to_owned()) - } - - config.mode = cfg_mode; - config.verbose = true; - - let files = ["src/main.rs", "src/lib.rs", "clippy_lints/src/lib.rs"]; - - for file in &files { - let paths = TestPaths { - base: PathBuf::new(), - file: PathBuf::from(file), - relative_dir: PathBuf::new(), - }; - - set_var("CLIPPY_DOGFOOD", "tastes like chicken"); - - compiletest::runtest::run(config.clone(), &paths); - } -} diff --git a/tests/ui/conf_bad_toml.rs b/tests/ui/conf_bad_toml.rs index 4de2cf6ae73..a2ce7ecc519 100644 --- a/tests/ui/conf_bad_toml.rs +++ b/tests/ui/conf_bad_toml.rs @@ -1,6 +1,6 @@ // error-pattern: error reading Clippy's configuration file -#![plugin(clippy(conf_file="./tests/ui/conf_bad_toml.toml"))] +#![plugin(clippy(conf_file="../ui/conf_bad_toml.toml"))] fn main() {} diff --git a/tests/ui/conf_bad_toml.stderr b/tests/ui/conf_bad_toml.stderr index 5ddf8c14f70..45477ff0855 100644 --- a/tests/ui/conf_bad_toml.stderr +++ b/tests/ui/conf_bad_toml.stderr @@ -1,8 +1,8 @@ error: compiler plugins are experimental and possibly buggy (see issue #29597) --> $DIR/conf_bad_toml.rs:4:1 | -4 | #![plugin(clippy(conf_file="./$DIR/conf_bad_toml.toml"))] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +4 | #![plugin(clippy(conf_file="../ui/conf_bad_toml.toml"))] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: add #![feature(plugin)] to the crate attributes to enable diff --git a/tests/ui/conf_bad_type.rs b/tests/ui/conf_bad_type.rs index 4cb21b91582..cb18bfb8c90 100644 --- a/tests/ui/conf_bad_type.rs +++ b/tests/ui/conf_bad_type.rs @@ -1,6 +1,6 @@ // error-pattern: error reading Clippy's configuration file: `blacklisted-names` is expected to be a `Vec < String >` but is a `integer` -#![plugin(clippy(conf_file="./tests/ui/conf_bad_type.toml"))] +#![plugin(clippy(conf_file="../ui/conf_bad_type.toml"))] fn main() {} diff --git a/tests/ui/conf_bad_type.stderr b/tests/ui/conf_bad_type.stderr index 961df381c99..0fa40cfca9b 100644 --- a/tests/ui/conf_bad_type.stderr +++ b/tests/ui/conf_bad_type.stderr @@ -1,8 +1,8 @@ error: compiler plugins are experimental and possibly buggy (see issue #29597) --> $DIR/conf_bad_type.rs:4:1 | -4 | #![plugin(clippy(conf_file="./$DIR/conf_bad_type.toml"))] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +4 | #![plugin(clippy(conf_file="../ui/conf_bad_type.toml"))] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: add #![feature(plugin)] to the crate attributes to enable diff --git a/tests/ui/conf_french_blacklisted_name.rs b/tests/ui/conf_french_blacklisted_name.rs index 9f22ff659f2..dbe6d85e5d2 100644 --- a/tests/ui/conf_french_blacklisted_name.rs +++ b/tests/ui/conf_french_blacklisted_name.rs @@ -1,5 +1,5 @@ -#![plugin(clippy(conf_file="./tests/auxiliary/conf_french_blacklisted_name.toml"))] +#![plugin(clippy(conf_file="../auxiliary/conf_french_blacklisted_name.toml"))] #![allow(dead_code)] #![allow(single_match)] diff --git a/tests/ui/conf_french_blacklisted_name.stderr b/tests/ui/conf_french_blacklisted_name.stderr index c98adb6029f..f7eb174f9a6 100644 --- a/tests/ui/conf_french_blacklisted_name.stderr +++ b/tests/ui/conf_french_blacklisted_name.stderr @@ -1,8 +1,8 @@ error: compiler plugins are experimental and possibly buggy (see issue #29597) --> $DIR/conf_french_blacklisted_name.rs:2:1 | -2 | #![plugin(clippy(conf_file="./tests/auxiliary/conf_french_blacklisted_name.toml"))] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +2 | #![plugin(clippy(conf_file="../auxiliary/conf_french_blacklisted_name.toml"))] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: add #![feature(plugin)] to the crate attributes to enable diff --git a/tests/ui/conf_unknown_key.rs b/tests/ui/conf_unknown_key.rs index aec2c883367..437d0f9d8b0 100644 --- a/tests/ui/conf_unknown_key.rs +++ b/tests/ui/conf_unknown_key.rs @@ -1,6 +1,6 @@ // error-pattern: error reading Clippy's configuration file: unknown key `foobar` -#![plugin(clippy(conf_file="./tests/auxiliary/conf_unknown_key.toml"))] +#![plugin(clippy(conf_file="../auxiliary/conf_unknown_key.toml"))] fn main() {} diff --git a/tests/ui/conf_unknown_key.stderr b/tests/ui/conf_unknown_key.stderr index 9fc7dbea563..c525366c129 100644 --- a/tests/ui/conf_unknown_key.stderr +++ b/tests/ui/conf_unknown_key.stderr @@ -1,8 +1,8 @@ error: compiler plugins are experimental and possibly buggy (see issue #29597) --> $DIR/conf_unknown_key.rs:4:1 | -4 | #![plugin(clippy(conf_file="./tests/auxiliary/conf_unknown_key.toml"))] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +4 | #![plugin(clippy(conf_file="../auxiliary/conf_unknown_key.toml"))] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: add #![feature(plugin)] to the crate attributes to enable diff --git a/tests/ui/format.stderr b/tests/ui/format.stderr index 67d97f295d8..558e9e83c33 100644 --- a/tests/ui/format.stderr +++ b/tests/ui/format.stderr @@ -6,15 +6,3 @@ error: useless use of `format!` | = note: `-D useless-format` implied by `-D warnings` -error: useless use of `format!` - --> $DIR/format.rs:8:5 - | -8 | format!("{}", "foo"); - | ^^^^^^^^^^^^^^^^^^^^^ - -error: useless use of `format!` - --> $DIR/format.rs:15:5 - | -15 | format!("{}", arg); - | ^^^^^^^^^^^^^^^^^^^ - diff --git a/tests/ui/implicit_hasher.rs b/tests/ui/implicit_hasher.rs index 32ca0f56d77..c93f858b5ca 100644 --- a/tests/ui/implicit_hasher.rs +++ b/tests/ui/implicit_hasher.rs @@ -1,5 +1,5 @@ #![allow(unused)] -//#![feature(plugin)]#![plugin(clippy)] + use std::collections::{HashMap, HashSet}; use std::cmp::Eq; use std::hash::{Hash, BuildHasher}; diff --git a/tests/ui/int_plus_one.rs b/tests/ui/int_plus_one.rs index 90375dad555..a9e059f4a3e 100644 --- a/tests/ui/int_plus_one.rs +++ b/tests/ui/int_plus_one.rs @@ -1,5 +1,5 @@ -#![feature(plugin)] -#![plugin(clippy)] + + #[allow(no_effect, unnecessary_operation)] #[warn(int_plus_one)] diff --git a/tests/ui/int_plus_one.stderr b/tests/ui/int_plus_one.stderr index 92b012bd104..5d42ebb8986 100644 --- a/tests/ui/int_plus_one.stderr +++ b/tests/ui/int_plus_one.stderr @@ -1,5 +1,3 @@ -warning: running cargo clippy on a crate that also imports the clippy plugin - error: Unnecessary `>= y + 1` or `x - 1 >=` --> $DIR/int_plus_one.rs:10:5 | diff --git a/tests/ui/invalid_ref.rs b/tests/ui/invalid_ref.rs index 2b8f04c9781..ce2596c0c1a 100644 --- a/tests/ui/invalid_ref.rs +++ b/tests/ui/invalid_ref.rs @@ -1,5 +1,5 @@ -#![feature(plugin)] -#![plugin(clippy)] + + #![allow(unused)] #![feature(core_intrinsics)] diff --git a/tests/ui/invalid_ref.stderr b/tests/ui/invalid_ref.stderr index 18064c91a01..c018bdf6dd3 100644 --- a/tests/ui/invalid_ref.stderr +++ b/tests/ui/invalid_ref.stderr @@ -1,5 +1,3 @@ -warning: running cargo clippy on a crate that also imports the clippy plugin - error: reference to zeroed memory --> $DIR/invalid_ref.rs:27:24 | diff --git a/tests/ui/mut_range_bound.rs b/tests/ui/mut_range_bound.rs index 835ceeedc94..0e397c7ae8c 100644 --- a/tests/ui/mut_range_bound.rs +++ b/tests/ui/mut_range_bound.rs @@ -1,5 +1,5 @@ -#![feature(plugin)] -#![plugin(clippy)] + + #![allow(unused)] diff --git a/tests/ui/mut_range_bound.stderr b/tests/ui/mut_range_bound.stderr index f516ec9d95e..20dbb6511d7 100644 --- a/tests/ui/mut_range_bound.stderr +++ b/tests/ui/mut_range_bound.stderr @@ -1,5 +1,3 @@ -warning: running cargo clippy on a crate that also imports the clippy plugin - error: attempt to mutate range bound within loop; note that the range of the loop is unchanged --> $DIR/mut_range_bound.rs:18:21 | diff --git a/tests/ui/print_with_newline.stderr b/tests/ui/print_with_newline.stderr index 2ade3ae4ef5..0148a470e0d 100644 --- a/tests/ui/print_with_newline.stderr +++ b/tests/ui/print_with_newline.stderr @@ -6,21 +6,3 @@ error: using `print!()` with a format string that ends in a newline, consider us | = note: `-D print-with-newline` implied by `-D warnings` -error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead - --> $DIR/print_with_newline.rs:7:5 - | -7 | print!("Hello {}/n", "world"); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead - --> $DIR/print_with_newline.rs:8:5 - | -8 | print!("Hello {} {}/n/n", "world", "#2"); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead - --> $DIR/print_with_newline.rs:9:5 - | -9 | print!("{}/n", 1265); - | ^^^^^^^^^^^^^^^^^^^^^ -