Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Add clippy as an optional feature #698

Merged
merged 2 commits into from
Feb 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
71 changes: 66 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rls-rustc = "0.2.1"
rls-span = { version = "0.4", features = ["serialize-serde"] }
rls-vfs = { version = "0.4", features = ["racer-impls"] }
rustfmt-nightly = { version = "0.3.8", optional = true }
clippy_lints = { version = "0.0.185", optional = true }
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
Expand All @@ -37,3 +38,4 @@ json = "0.11"
[features]
default = ["rustfmt"]
rustfmt = ["rustfmt-nightly"]
clippy = ["clippy_lints"]
62 changes: 60 additions & 2 deletions src/build/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
extern crate getopts;
extern crate rustc;
extern crate rustc_driver;
extern crate rustc_plugin;
extern crate rustc_errors as errors;
extern crate rustc_resolve;
extern crate rustc_save_analysis;
extern crate rustc_trans_utils;
#[cfg(feature = "clippy")]
extern crate clippy_lints;
extern crate syntax;

use self::rustc::middle::cstore::CrateStore;
use self::rustc::session::Session;
use self::rustc::session::config::{self, ErrorOutputType, Input};
use self::rustc_driver::{run, run_compiler, Compilation, CompilerCalls, RustcDefaultCalls};
use self::rustc_driver::driver::CompileController;
use self::rustc_driver::driver::{CompileController, CompileState};
use self::rustc_save_analysis as save;
use self::rustc_save_analysis::CallbackHandler;
use self::rustc_trans_utils::trans_crate::TransCrate;
Expand Down Expand Up @@ -70,7 +73,18 @@ pub fn rustc(

let buf = Arc::new(Mutex::new(vec![]));
let err_buf = buf.clone();
let args = args.to_owned();
let args: Vec<_> = if cfg!(feature = "clippy") {
args.iter()
.map(|s| s.to_owned())
.chain(vec![
"-Aclippy".to_owned(),
"--cfg".to_owned(),
r#"feature="cargo-clippy""#.to_owned(),
])
.collect()
} else {
args.to_owned()
};

let analysis = Arc::new(Mutex::new(None));

Expand Down Expand Up @@ -171,6 +185,50 @@ impl<'a> CompilerCalls<'a> for RlsRustcCalls {
result.keep_ast = true;
let analysis = self.analysis.clone();

#[cfg(feature = "clippy")]
fn clippy(state: &mut CompileState) {
let mut registry = rustc_plugin::registry::Registry::new(
state.session,
state
.krate
.as_ref()
.expect(
"at this compilation stage \
the crate must be parsed",
)
.span,
);
registry.args_hidden = Some(Vec::new());
clippy_lints::register_plugins(&mut registry);

let rustc_plugin::registry::Registry {
early_lint_passes,
late_lint_passes,
lint_groups,
llvm_passes,
attributes,
..
} = registry;
let sess = &state.session;
let mut ls = sess.lint_store.borrow_mut();
for pass in early_lint_passes {
ls.register_early_pass(Some(sess), true, pass);
}
for pass in late_lint_passes {
ls.register_late_pass(Some(sess), true, pass);
}

for (name, to) in lint_groups {
ls.register_group(Some(sess), true, name, to);
}

sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
sess.plugin_attributes.borrow_mut().extend(attributes);
}
#[cfg(not(feature = "clippy"))]
fn clippy(_: &mut CompileState) {}
result.after_parse.callback = Box::new(clippy);

result.after_analysis.callback = Box::new(move |state| {
// There are two ways to move the data from rustc to the RLS, either
// directly or by serialising and deserialising. We only want to do
Expand Down