How can I apply the quick fix "Remove all the unused imports" to the entire project?

I use Neovim with rust-analyzer and plugins prabirshrestha/vim-lsp and rust-lang/rust.vim.

I can apply the quick fix Remove all the unused imports by running <plug>(lsp-code-action) on a line with unused imports.

Is there a way to apply the quick fix on the entire project, not just on the line?

I don’t know that there’s a way to do it with rust-analyzer, but cargo fix will remove unused imports.

2 Likes

I want to remove unused imports only. I think cargo fix will apply other fixes as well.

1 Like

It's unfortunate that there is no option to select only a specific lint, but in general it should be unlikely that any of the fixes recommended by rustc will cause harm.

(Note that cargo fix does not consult clippy lints.)

1 Like

If a rustc lint is set to allow (in the source or Cargo.toml), does that mean cargo fix won't correct it? If so, it will only correct the current set of warnings and errors from cargo check. Is that right?

1 Like

Yes.

1 Like

Based on the info from @kpreid, you could prevent some things from being fixed by setting their rustc lints to allow in the [lints.rust] section of your Cargo.toml, before running cargo fix.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.