rust-tools.nvim icon indicating copy to clipboard operation
rust-tools.nvim copied to clipboard

Documentation request: clippy integration

Open RobertMenke opened this issue 3 years ago • 5 comments

Hey, thank you for putting together this wonderful plugin!

I'm trying to get clippy feedback to show up in my editor as warnings. I think I may be missing something obvious, but given clippy's, popularity it would be great to have this integration documented on the readme. I'm more than happy to submit a PR once I can get this figured out myself.

My config is below:

    {
        "simrat39/rust-tools.nvim",
        config = function()
            local lsp_installer_servers = require "nvim-lsp-installer.servers"
            local _, requested_server = lsp_installer_servers.get_server "rust_analyzer"
            local extension_path = vim.env.HOME .. '/.vscode/extensions/vadimcn.vscode-lldb-1.7.0/'
            local codelldb_path = extension_path .. 'adapter/codelldb'
            local liblldb_path = extension_path .. 'lldb/lib/liblldb.dylib'
            local rust_tools = require("rust-tools")

            rust_tools.setup({
                dap = {
                    adapter = require('rust-tools.dap').get_codelldb_adapter(codelldb_path, liblldb_path)
                },
                tools = {
                    autoSetHints = true,
                    hover_with_actions = true,
                    hover_actions = {
                        auto_focus = true
                    },
                    runnables = {
                        use_telescope = true,
                    },
                },
                server = {
                    cmd_env = requested_server._default_options.cmd_env,
                    on_attach = require("lvim.lsp").common_on_attach,
                    on_init = require("lvim.lsp").common_on_init,
                    settings = {
                        ["rust-analyzer"] = {
                            checkOnSave = {
                                command = "clippy"
                            }
                        }
                    }
                },
            })
        end,
        ft = { "rust", "rs" },
    },

RobertMenke avatar Jun 13 '22 02:06 RobertMenke

I'm having the same issue. I can't seem to get LSP for clippy, even after adding it to checkOnSave.

Are you sure this is a documentation issue and not a feature issue?

ok-nick avatar Jun 24 '22 15:06 ok-nick

Make sure you're not using a rust nightly toolchain with Clippy broken. I spent days on this till I find out that Clippy was broken on the nightly version I was using. I switched back to stable and it was working.

ahkohd avatar Jul 02 '22 11:07 ahkohd

FWIW, the checkOnSave configuration you showed above is working for me. Here's my config.

local rt = require("rust-tools")
rt.setup({
	server = {
		on_attach = function(_, bufnr)
			-- Hover actions
			vim.keymap.set("n", "K", rt.hover_actions.hover_actions, { buffer = bufnr })
			-- Code action groups
			vim.keymap.set("n", "<space>ca", rt.code_action_group.code_action_group, { buffer = bufnr })
		end,
		settings = {
			["rust-analyzer"] = {
				checkOnSave = {
					command = "clippy"
				}
			}
		}
	},
})

And here's my clippy version info:

$ cargo clippy --version
clippy 0.1.64 (a55dd71d 2022-09-19)

blachniet avatar Oct 29 '22 13:10 blachniet

I fixed this issue a while ago, but I believe it was caused by initializing rust-analyzer directly with the settings, then calling rust-tools with no settings, which overwrote it.

ok-nick avatar Oct 31 '22 19:10 ok-nick

The above might have been correct for previous versions of rust-analyzer, but not anymore. Check out the rust-analyzer manual and scroll/search your way down to rust-analyzer.checkOnSave. You will see that this is a boolean value. It controls whether or not a "check" is run on save, and defaults to true. By default that does cargo check on save.

What you want is to override the rust-analyzer.check.command to instead run clippy. You can also add extra arguments to the command if you want.

TLDR; You want something like this:

require("rust-tools").setup({
	server = {
		settings = {
			["rust-analyzer"] = {
				check = {
					command = "clippy",
					extraArgs = { "--all", "--", "-W", "clippy::all" },
				},
			},
		},
	},
})

bsamseth avatar Jan 18 '23 13:01 bsamseth