Skip to content

cargo clippy -- -W help runs clippy #6122

@flip1995

Description

@flip1995
Member

cargo clippy -- -W help seems to run clippy, 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 to rustc -W help?

Context: in rust-analyzer we'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 #5385 (comment)

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.

Activity

added
good first issueThese issues are a good way to get started with Clippy
C-bugCategory: Clippy is not doing the correct thing
on Oct 6, 2020
bnjjj

bnjjj commented on Oct 6, 2020

@bnjjj

Will take a look to fix that as a first contribution to clippy :)

flip1995

flip1995 commented on Oct 6, 2020

@flip1995
MemberAuthor

@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.
ebroto

ebroto commented on Oct 7, 2020

@ebroto
Member

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)

flip1995

flip1995 commented on Oct 7, 2020

@flip1995
MemberAuthor

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).

added a commit that references this issue on Nov 26, 2020
72da5a9
added a commit that references this issue on Nov 27, 2020
f303168
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thinggood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @ebroto@bnjjj@flip1995

      Issue actions

        `cargo clippy -- -W help` runs `clippy` · Issue #6122 · rust-lang/rust-clippy