cargo clippy -- -W helpseems to runclippy, so it takes a while on a first build and, of course, it requires a project. Would it be possible to print the available lints and quit, similar torustc -W help?
Context: in
rust-analyzerwe'd like to offer code completions for the various lints, but the only way to get them is to download http://rust-lang.github.io/rust-clippy/master/lints.json.
_Originally posted by @lnicola in https://github.com/rust-lang/rust-clippy/issues/5385#issuecomment-704233618_
Even though the -Whelp thing is unstable, it shouldn't have to run Clippy on a crate to print out the help message with all lints.
Will take a look to fix that as a first contribution to clippy :)
@lnicola You can get the list of clippy lints with clippy-driver -W help (-Whelp w/o whitespace, just prints the rustc lints). Running cargo clippy -- -W help is like running cargo rustc -- -W help, except that Clippy has an (unwanted?) hack, that this works. So the current behavior is kind of intentional. Only the clippy-driver -W help version is expected to work.
@bnjjj asked about adding a flag to display the lint list in json format. We could think about something like that. I will start a discussion about this on Zulip, but this issue is not really an issue.
@bnjjj Thanks for your work! Even though your PR won't get merged, it helped to figure out what is going on and is therefore much appreciated! If you want to improve the situation with clippy-driver -W help, we could try to not handle -Whelp at all in driver.rs, but just pass it along with the other flags to rustc and let it handle by rustc. It works for -Wlint_name, so it might also work for -Whelp 馃し
Also this message at the end of the output of rustc -Whelp suggests, that it might work:
Compiler plugins can provide additional lints and lint groups. To see a listing of these, re-run `rustc -W help` with a crate filename.
we could try to not handle -Whelp at all in driver.rs, but just pass it along with the other flags to rustc and let it handle by rustc
The problem I see with that approach is that it requires providing a crate, while the manual handling of -W help does not. It seems that the request to print all the lints is not processed until after parsing, see here. This is probably related to the plugin interface, which has not been removed yet AFAIK.
(pointing this out because it was one of the concerns in the OP)
The problem I see with that approach is that it requires providing a crate
Yeah, I wondered why and went investigating. Because from a UX perspective it doesn't make sense, that clippy-driver -W help prints rustc lints and clippy-driver -W help main.rs prints rustc and Clippy lints. (And it doesn't require a crate in the cargo sense, but in the rustc/AST sense, so some dummy rust file)
I found this code snippet, which is executed if rustc is called without an input file:
https://github.com/rust-lang/rust/blob/98edd1fbf8a68977a2a7c1312eb1ebff80515a92/compiler/rustc_driver/src/lib.rs#L209-L213
We could easily modify this with
diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs
index 3f50c68e3eb..bae45c12058 100644
--- a/compiler/rustc_driver/src/lib.rs
+++ b/compiler/rustc_driver/src/lib.rs
@@ -206,11 +206,18 @@ pub fn run_compiler(
interface::run_compiler(config, |compiler| {
let sopts = &compiler.session().opts;
if sopts.describe_lints {
- let lint_store = rustc_lint::new_lint_store(
+ let mut lint_store = rustc_lint::new_lint_store(
sopts.debugging_opts.no_interleave_lints,
compiler.session().unstable_options(),
);
- describe_lints(compiler.session(), &lint_store, false);
+ let registered_lints =
+ if let Some(register_lints) = compiler.register_lints() {
+ register_lints(compiler.session(), &mut lint_store);
+ true
+ } else {
+ false
+ };
+ describe_lints(compiler.session(), &lint_store, registered_lints);
return;
}
let should_stop = RustcDefaultCalls::print_crate_info(
diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs
index 73a51ad477b..11dd6ec32c0 100644
--- a/compiler/rustc_interface/src/interface.rs
+++ b/compiler/rustc_interface/src/interface.rs
@@ -56,6 +56,9 @@ impl Compiler {
pub fn output_file(&self) -> &Option<PathBuf> {
&self.output_file
}
+ pub fn register_lints(&self) -> &Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> {
+ &self.register_lints
+ }
pub fn build_output_filenames(
&self,
sess: &Session,
which then _should_ always print all lints, if there is a plugin (or something else that registers lints).
Most helpful comment
Will take a look to fix that as a first contribution to clippy :)