Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo clippy -- -W help runs clippy #6122

Closed
flip1995 opened this issue Oct 6, 2020 · 4 comments · Fixed by rust-lang/rust#77671
Closed

cargo clippy -- -W help runs clippy #6122

flip1995 opened this issue Oct 6, 2020 · 4 comments · Fixed by rust-lang/rust#77671
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy

Comments

@flip1995
Copy link
Member

flip1995 commented Oct 6, 2020

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.

@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy C-bug Category: Clippy is not doing the correct thing labels Oct 6, 2020
@bnjjj
Copy link

bnjjj commented Oct 6, 2020

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

bnjjj added a commit to bnjjj/rust-clippy that referenced this issue Oct 6, 2020
Signed-off-by: Benjamin Coenen <[email protected]>
bnjjj added a commit to bnjjj/rust-clippy that referenced this issue Oct 6, 2020
bnjjj added a commit to bnjjj/rust-clippy that referenced this issue Oct 6, 2020
@flip1995
Copy link
Member Author

flip1995 commented Oct 6, 2020

@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
Copy link
Member

ebroto commented Oct 7, 2020

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
Copy link
Member Author

flip1995 commented Oct 7, 2020

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

giraffate pushed a commit to giraffate/rust-clippy that referenced this issue Nov 27, 2020
…anishearth

Always print lints from plugins, if they're available

Currently you can get a list of lints and lint groups by running `rustc
-Whelp`. This prints an additional line at the end:
```
Compiler plugins can provide additional lints and lint groups. To see a listing of these, re-run `rustc -W help` with a crate filename.
```

Clippy is such a "compiler plugin", that provides additional lints.
Running `clippy-driver -Whelp` (`rustc` wrapper) still only prints the
rustc lints with the above message at the end. But when running
`clippy-driver -Whelp main.rs`, where `main.rs` is any rust file, it
also prints Clippy lints. I don't think this is a good approach from a
UX perspective: Why is a random file necessary to print a help message?

This PR changes this behavior: Whenever a compiler callback
registers lints, it is assumed that these lints come from a plugin and
are printed without having to specify a Rust source file.

Fixes rust-lang#6122

cc `@Manishearth` `@ebroto` for the Clippy changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
3 participants