Rust-analyzer run clippy on demand

I'm using rust-analyzer through coc.nvim and I'm wondering whether there is any way to run clippy on demand instead of on every save. I'm currently using
"rust-analyzer.check.command": "clippy" and
"rust-analyzer.checkOnSave": true

But I'd like to use cargo check on save to get diagnostics quicker on save, while still being able to get clippy diagnostics on demand. Anyone knows how to set this up?

You can't make it. I don't use coc.nvim for a long time, but scanned the doc for a while and searched around.

First, coc has fetched diagnostics on the fly already by default. So it's the suitable option for your question.

Second, nvim supports lsp handlers with on_publish_diagnostics where only the update_in_insert option is related here.

update_in_insert: (default false) Update diagnostics in Insert mode (if false, diagnostics are updated on InsertLeave)

Third, a closed pr in nvim proposed the following option (and the currently adopted update_in_insert option mentioned above)

show_diagnostic_autocmds = { 'InsertLeave', 'TextChanged' },

but it turned out the option can be replaced with a user's autocmd

-- shows on InsertLeave & TextChanged for all clients attached to current buffer, where virtual_text is false, but underline and signs default true
vim.cmd[[autocmd InsertLeave,TextChanged * lua vim.lsp.diagnostic.show_buffer_diagnostics(nil, nil, {virtual_text = false})]]

No, it's not possible. The last thing is that you actually can't do much, especially with how RA is designed:

rust-analyzer is a young tool and comes with a lot of limitations. The most significant one is that we are not at the moment using rustc directly, so our capabilities for detecting errors are limited.

In particular, to show inline errors we are doing what Emacs has been doing for ages — running cargo check after the file is saved. If auto-save is enabled in the editor, the result is actually quite nice for small projects.

For bigger projects though, I feel like cargo check in background gets in the way. So for rust-analyzer I have rust-analyzer.checkOnSave.enabled = false; in the settings. Instead, I use the Run functionality to run check / test and keyboard shortcuts to navigate between errors.

src: First Release

Thanks for your answer! It's unfortunate this is not possible. I also tried changing the check on save setting dynamically, but it requires a whole reload of coc, which then triggers a reindexing of the project by RA, much too slow to be useful.. Probably have to go with the approach the RA author is using.

Actually I finally found that RA does support running check/clippy on demand!
It's called "runFlycheck":

I noticed that new functionality in weekly changelog news. But it seems to only work in VScode and remains undocmented in the manual.

According to this comment

and the flycheck pr code,

you'll need to save the file anyway.

So a new way is to auto save in nvim, like what the plugin GitHub - Pocco81/auto-save.nvim: 🧶 Automatically save your changes in NeoVim does. (I haven't used that)

It also works with GitHub - fannheyward/coc-rust-analyzer: rust-analyzer extension for coc.nvim. I'm using noremap <Leader>c :CocCommand rust-analyzer.runFlycheck<CR> to trigger it. And yea it operates on the file saved to disk, so it probably makes sense to add a buffer write to that shortcut.

Or maybe I'll go for the auto-save plugin, thanks for the pointer!

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.