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

Commit

Permalink
Add clippy as an optional feature
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Feb 3, 2018
1 parent 5fe9745 commit 4a1116b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
62 changes: 62 additions & 0 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.6", 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"]
55 changes: 53 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,11 @@ 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(Some("-Aclippy".to_owned())).collect()
} else {
args.to_owned()
};

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

Expand Down Expand Up @@ -171,6 +178,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

0 comments on commit 4a1116b

Please sign in to comment.