Skip to content

Commit 02547b9

Browse files
committed
Merge remote-tracking branch 'origin/master' into sugg
2 parents ded5b30 + 3bd0aca commit 02547b9

File tree

2 files changed

+55
-46
lines changed

2 files changed

+55
-46
lines changed

clippy_lints/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![feature(collections)]
55
#![feature(custom_attribute)]
66
#![feature(dotdot_in_tuple_patterns)]
7-
#![feature(iter_arith)]
87
#![feature(question_mark)]
98
#![feature(rustc_private)]
109
#![feature(slice_patterns)]

src/main.rs

+55-45
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ use std::process::Command;
1818

1919
use clippy_lints::utils::cargo;
2020

21-
struct ClippyCompilerCalls(RustcDefaultCalls);
22-
23-
impl std::default::Default for ClippyCompilerCalls {
24-
fn default() -> Self {
25-
Self::new()
26-
}
21+
struct ClippyCompilerCalls {
22+
default: RustcDefaultCalls,
23+
run_lints: bool,
2724
}
2825

2926
impl ClippyCompilerCalls {
30-
fn new() -> Self {
31-
ClippyCompilerCalls(RustcDefaultCalls)
27+
fn new(run_lints: bool) -> Self {
28+
ClippyCompilerCalls {
29+
default: RustcDefaultCalls,
30+
run_lints: run_lints,
31+
}
3232
}
3333
}
3434

@@ -39,7 +39,7 @@ impl<'a> CompilerCalls<'a> for ClippyCompilerCalls {
3939
descriptions: &rustc_errors::registry::Registry,
4040
output: ErrorOutputType)
4141
-> Compilation {
42-
self.0.early_callback(matches, sopts, descriptions, output)
42+
self.default.early_callback(matches, sopts, descriptions, output)
4343
}
4444
fn no_input(&mut self,
4545
matches: &getopts::Matches,
@@ -48,7 +48,7 @@ impl<'a> CompilerCalls<'a> for ClippyCompilerCalls {
4848
ofile: &Option<PathBuf>,
4949
descriptions: &rustc_errors::registry::Registry)
5050
-> Option<(Input, Option<PathBuf>)> {
51-
self.0.no_input(matches, sopts, odir, ofile, descriptions)
51+
self.default.no_input(matches, sopts, odir, ofile, descriptions)
5252
}
5353
fn late_callback(&mut self,
5454
matches: &getopts::Matches,
@@ -57,44 +57,46 @@ impl<'a> CompilerCalls<'a> for ClippyCompilerCalls {
5757
odir: &Option<PathBuf>,
5858
ofile: &Option<PathBuf>)
5959
-> Compilation {
60-
self.0.late_callback(matches, sess, input, odir, ofile)
60+
self.default.late_callback(matches, sess, input, odir, ofile)
6161
}
6262
fn build_controller(&mut self, sess: &Session, matches: &getopts::Matches) -> driver::CompileController<'a> {
63-
let mut control = self.0.build_controller(sess, matches);
64-
65-
let old = std::mem::replace(&mut control.after_parse.callback, box |_| {});
66-
control.after_parse.callback = Box::new(move |state| {
67-
{
68-
let mut registry = rustc_plugin::registry::Registry::new(state.session, state.krate.as_ref().expect("at this compilation stage the krate must be parsed"));
69-
registry.args_hidden = Some(Vec::new());
70-
clippy_lints::register_plugins(&mut registry);
71-
72-
let rustc_plugin::registry::Registry { early_lint_passes,
73-
late_lint_passes,
74-
lint_groups,
75-
llvm_passes,
76-
attributes,
77-
mir_passes,
78-
.. } = registry;
79-
let sess = &state.session;
80-
let mut ls = sess.lint_store.borrow_mut();
81-
for pass in early_lint_passes {
82-
ls.register_early_pass(Some(sess), true, pass);
83-
}
84-
for pass in late_lint_passes {
85-
ls.register_late_pass(Some(sess), true, pass);
86-
}
63+
let mut control = self.default.build_controller(sess, matches);
64+
65+
if self.run_lints {
66+
let old = std::mem::replace(&mut control.after_parse.callback, box |_| {});
67+
control.after_parse.callback = Box::new(move |state| {
68+
{
69+
let mut registry = rustc_plugin::registry::Registry::new(state.session, state.krate.as_ref().expect("at this compilation stage the krate must be parsed"));
70+
registry.args_hidden = Some(Vec::new());
71+
clippy_lints::register_plugins(&mut registry);
72+
73+
let rustc_plugin::registry::Registry { early_lint_passes,
74+
late_lint_passes,
75+
lint_groups,
76+
llvm_passes,
77+
attributes,
78+
mir_passes,
79+
.. } = registry;
80+
let sess = &state.session;
81+
let mut ls = sess.lint_store.borrow_mut();
82+
for pass in early_lint_passes {
83+
ls.register_early_pass(Some(sess), true, pass);
84+
}
85+
for pass in late_lint_passes {
86+
ls.register_late_pass(Some(sess), true, pass);
87+
}
8788

88-
for (name, to) in lint_groups {
89-
ls.register_group(Some(sess), true, name, to);
90-
}
89+
for (name, to) in lint_groups {
90+
ls.register_group(Some(sess), true, name, to);
91+
}
9192

92-
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
93-
sess.mir_passes.borrow_mut().extend(mir_passes);
94-
sess.plugin_attributes.borrow_mut().extend(attributes);
95-
}
96-
old(state);
97-
});
93+
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
94+
sess.mir_passes.borrow_mut().extend(mir_passes);
95+
sess.plugin_attributes.borrow_mut().extend(attributes);
96+
}
97+
old(state);
98+
});
99+
}
98100

99101
control
100102
}
@@ -129,6 +131,7 @@ pub fn main() {
129131
};
130132

131133
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
134+
// this arm is executed on the initial call to `cargo clippy`
132135
let manifest_path = std::env::args().skip(2).find(|val| val.starts_with("--manifest-path="));
133136
let mut metadata = cargo::metadata(manifest_path).expect("could not obtain cargo metadata");
134137
assert_eq!(metadata.version, 1);
@@ -149,12 +152,19 @@ pub fn main() {
149152
}
150153
}
151154
} else {
155+
// this arm is executed when cargo-clippy runs `cargo rustc` with the `RUSTC` env var set to itself
156+
157+
// this conditional check for the --sysroot flag is there so users can call `cargo-clippy` directly
158+
// without having to pass --sysroot or anything
152159
let args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
153160
env::args().collect()
154161
} else {
155162
env::args().chain(Some("--sysroot".to_owned())).chain(Some(sys_root)).collect()
156163
};
157-
let (result, _) = rustc_driver::run_compiler(&args, &mut ClippyCompilerCalls::new());
164+
// this check ensures that dependencies are built but not linted and the final crate is
165+
// linted but not built
166+
let mut ccc = ClippyCompilerCalls::new(env::args().any(|s| s == "-Zno-trans"));
167+
let (result, _) = rustc_driver::run_compiler(&args, &mut ccc);
158168

159169
if let Err(err_count) = result {
160170
if err_count > 0 {

0 commit comments

Comments
 (0)