Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Improve tests
  • Loading branch information
smoelius committed Feb 16, 2021
commit 29134ea204bf989fdb587d9f787277a8782a77bc
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ out
/clippy_utils/target
/clippy_workspace_tests/target
/clippy_dev/target
/plugin_example/target
/plugin_example/tests/ui/*/target
/plugin_examples/*/target
/plugin_examples/*/tests/ui/*/target
/rustc_tools_util/target

# Generated by dogfood
Expand Down
7 changes: 5 additions & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ pub use crate::utils::conf::Conf;
/// level (i.e `#![cfg_attr(...)]`) will still be expanded even when using a pre-expansion pass.
///
/// Used in `./src/driver.rs`.
pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore) {
fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore) {
store.register_pre_expansion_pass(|| box write::Write::default());
store.register_pre_expansion_pass(|| box attrs::EarlyAttributes);
store.register_pre_expansion_pass(|| box dbg_macro::DbgMacro);
Expand Down Expand Up @@ -1931,6 +1931,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&transmute::USELESS_TRANSMUTE),
LintId::of(&use_self::USE_SELF),
]);

register_pre_expansion_lints(store);
register_renamed(store);
}

#[rustfmt::skip]
Expand Down Expand Up @@ -1980,7 +1983,7 @@ fn register_removed_non_tool_lints(store: &mut rustc_lint::LintStore) {
/// Register renamed lints.
///
/// Used in `./src/driver.rs`.
pub fn register_renamed(ls: &mut rustc_lint::LintStore) {
fn register_renamed(ls: &mut rustc_lint::LintStore) {
ls.register_renamed("clippy::stutter", "clippy::module_name_repetitions");
ls.register_renamed("clippy::new_without_default_derive", "clippy::new_without_default");
ls.register_renamed("clippy::cyclomatic_complexity", "clippy::cognitive_complexity");
Expand Down
4 changes: 2 additions & 2 deletions clippy_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "clippy_utils"
version = "0.1.0"
authors = ["The Rust Project Developers"]
version = "0.1.51"
authors = ["The Rust Clippy Developers"]
edition = "2018"

[dependencies]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[package]
name = "plugin_example"
name = "allow_clippy_lints"
version = "0.1.0"
authors = ["The Rust Clippy Developers"]
description = "A tongue-in-cheek example of a Clippy plugin"
edition = "2018"

[lib]
name = "plugin_example"
name = "allow_clippy_lints"
crate-type = ["dylib"]

[dependencies]
clippy_utils = { path = "../clippy_utils" }
clippy_utils = { path = "../../clippy_utils" }
if_chain = "1.0.1"

[dev-dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_>, attr: &Attribute, items: &[Nest
cx,
ALLOW_CLIPPY_LINTS,
attr.span,
"`allow`ing Clippy lints `deny`s your project of its true potential",
"allowing Clippy lints denies your project of its true potential",
"use",
"#[deny(clippy::restriction, clippy::style, clippy::pedantic, clippy::complexity, clippy::perf, clippy::cargo, clippy::nursery)]".to_string(),
Applicability::MachineApplicable,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::path::{Path, PathBuf};
use std::process::Command;

lazy_static! {
static ref CLIPPY: PathBuf = canonicalize("../target/debug/cargo-clippy").unwrap();
static ref CLIPPY: PathBuf = canonicalize("../../target/debug/cargo-clippy").unwrap();
}

const PLUGIN: &str = "libplugin_example.so";
const PLUGIN: &str = "liballow_clippy_lints.so";

#[test]
fn clippy_test() {
Expand All @@ -17,7 +17,7 @@ fn clippy_test() {
.join("..")
.join("..")
.join("target")
.join("release")
.join("debug")
.join(PLUGIN);

for entry in read_dir(src_base).unwrap() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `allow`ing Clippy lints `deny`s your project of its true potential
error: allowing Clippy lints denies your project of its true potential
--> src/main.rs:1:1
|
1 | #[allow(clippy::assertions_on_constants)]
Expand Down
14 changes: 14 additions & 0 deletions plugin_examples/clippy_lints/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "clippy_lints"
version = "0.1.0"
authors = ["The Rust Clippy Developers"]
description = "All of the Clippy lints as a Clippy plugin"
edition = "2018"

[lib]
name = "clippy_lints"
crate-type = ["dylib"]

[dependencies]
clippy_lints = { path = "../../clippy_lints" }
clippy_utils = { path = "../../clippy_utils" }
12 changes: 12 additions & 0 deletions plugin_examples/clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(rustc_private)]

extern crate rustc_lint;
extern crate rustc_session;

use clippy_utils::conf::Conf;
use rustc_session::Session;

#[no_mangle]
pub extern "C" fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: &Conf) {
clippy_lints::register_plugins(store, sess, conf);
}
21 changes: 15 additions & 6 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ impl LoadedPlugin {
}

struct ClippyCallbacks {
no_builtins: bool,
loaded_plugins: Vec<LoadedPlugin>,
}

impl ClippyCallbacks {
// smoelius: Load the libraries when ClippyCallbacks is created and not later (e.g., in `config`)
// to ensure that the libraries live long enough.
fn new(clippy_plugins: Vec<PathBuf>) -> ClippyCallbacks {
fn new(no_builtins: bool, clippy_plugins: Vec<PathBuf>) -> ClippyCallbacks {
let mut loaded_plugins = Vec::new();
for path in clippy_plugins {
unsafe {
Expand All @@ -111,13 +112,17 @@ impl ClippyCallbacks {
loaded_plugins.push(LoadedPlugin { path, lib });
}
}
ClippyCallbacks { loaded_plugins }
ClippyCallbacks {
no_builtins,
loaded_plugins,
}
}
}

impl rustc_driver::Callbacks for ClippyCallbacks {
fn config(&mut self, config: &mut interface::Config) {
let previous = config.register_lints.take();
let no_builtins = self.no_builtins;
let loaded_plugins = self.loaded_plugins.split_off(0);
config.register_lints = Some(Box::new(move |sess, mut lint_store| {
// technically we're ~guaranteed that this is none but might as well call anything that
Expand All @@ -127,12 +132,12 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
}

let conf = clippy_lints::read_conf(&[], &sess);
clippy_lints::register_plugins(&mut lint_store, &sess, &conf);
if !no_builtins {
clippy_lints::register_plugins(&mut lint_store, &sess, &conf);
}
for loaded_plugin in &loaded_plugins {
loaded_plugin.register_plugins(&mut lint_store, &sess, &conf);
}
clippy_lints::register_pre_expansion_lints(&mut lint_store);
clippy_lints::register_renamed(&mut lint_store);
}));

// FIXME: #4825; This is required, because Clippy lints that are based on MIR have to be
Expand Down Expand Up @@ -332,6 +337,7 @@ pub fn main() {
args.extend(vec!["--sysroot".into(), sys_root]);
};

let mut no_builtins = false;
let mut no_deps = false;
let mut clippy_args = Vec::new();
let mut clippy_plugins = Vec::new();
Expand All @@ -340,6 +346,9 @@ pub fn main() {
while let Some(s) = iter.next() {
match s {
"" => {},
"--no-builtins" => {
no_builtins = true;
},
"--no-deps" => {
no_deps = true;
},
Expand Down Expand Up @@ -372,7 +381,7 @@ pub fn main() {
args.extend(clippy_args);
}

let mut clippy = ClippyCallbacks::new(clippy_plugins);
let mut clippy = ClippyCallbacks::new(no_builtins, clippy_plugins);
let mut default = DefaultCallbacks;
let callbacks: &mut (dyn rustc_driver::Callbacks + Send) =
if clippy_enabled { &mut clippy } else { &mut default };
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Usage:

Common options:
-h, --help Print this message
--no-builtins Do not load builtin lints
--plugin PLUGIN Load lints in PLUGIN
-V, --version Print version info and exit

Expand Down Expand Up @@ -83,6 +84,10 @@ impl ClippyCmd {
cargo_subcommand = "fix";
continue;
},
"--no-builtins" => {
clippy_args.push(arg);
continue;
},
"--plugin" => {
let plugin = old_args.next().expect("missing argument to `--plugin`");
// smoelius: canonicalize in case clippy-driver is run from a different directory.
Expand Down
47 changes: 47 additions & 0 deletions tests/compile-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::ffi::OsStr;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;

mod cargo;

Expand Down Expand Up @@ -101,6 +102,51 @@ fn run_mode(cfg: &mut compiletest::Config) {
compiletest::run_tests(&cfg);
}

fn build_clippy_lints_plugin() {
assert!(Command::new("cargo")
.args(&[
"build",
"--manifest-path",
&Path::new("plugin_examples")
.join("clippy_lints")
.join("Cargo.toml")
.to_string_lossy(),
])
.status()
.unwrap()
.success());
}

fn encode_clippy_args(clippy_args: &[&str]) -> String {
clippy_args
.iter()
.map(|arg| format!("{}__CLIPPY_HACKERY__", arg))
.collect()
}

/// Like the `run_mode` ui tests but with all of the Clippy lints loaded from a plugin.
fn run_ui_plugin(cfg: &mut compiletest::Config) {
build_clippy_lints_plugin();
cfg.mode = TestMode::Ui;
cfg.src_base = Path::new("tests").join("ui");
set_var(
"CLIPPY_ARGS",
&encode_clippy_args(&[
"--no-builtins",
"--plugin",
&Path::new("plugin_examples")
.join("clippy_lints")
.join("target")
.join("debug")
.join("libclippy_lints.so")
.canonicalize()
.unwrap()
.to_string_lossy(),
]),
);
compiletest::run_tests(&cfg);
}

fn run_internal_tests(cfg: &mut compiletest::Config) {
// only run internal tests with the internal-tests feature
if !RUN_INTERNAL_TESTS {
Expand Down Expand Up @@ -265,6 +311,7 @@ fn compile_test() {
prepare_env();
let mut config = default_config();
run_mode(&mut config);
run_ui_plugin(&mut config);
run_ui_toml(&mut config);
run_ui_cargo(&mut config);
run_internal_tests(&mut config);
Expand Down
25 changes: 14 additions & 11 deletions tests/versioncheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
use rustc_tools_util::VersionInfo;

#[test]
fn check_that_clippy_lints_has_the_same_version_as_clippy() {
fn check_that_clippy_lints_and_clippy_utils_have_the_same_version_as_clippy() {
let clippy_meta = cargo_metadata::MetadataCommand::new()
.no_deps()
.exec()
.expect("could not obtain cargo metadata");
std::env::set_current_dir(std::env::current_dir().unwrap().join("clippy_lints")).unwrap();
let clippy_lints_meta = cargo_metadata::MetadataCommand::new()
.no_deps()
.exec()
.expect("could not obtain cargo metadata");
assert_eq!(clippy_lints_meta.packages[0].version, clippy_meta.packages[0].version);
for package in &clippy_meta.packages[0].dependencies {
if package.name == "clippy_lints" {
assert!(package.req.matches(&clippy_lints_meta.packages[0].version));
return;

for krate in &["clippy_lints", "clippy_utils"] {
let krate_meta = cargo_metadata::MetadataCommand::new()
.current_dir(std::env::current_dir().unwrap().join(krate))
.no_deps()
.exec()
.expect("could not obtain cargo metadata");
assert_eq!(krate_meta.packages[0].version, clippy_meta.packages[0].version);
for package in &clippy_meta.packages[0].dependencies {
if package.name == *krate {
assert!(package.req.matches(&krate_meta.packages[0].version));
break;
}
}
}
}
Expand Down