simrat39 / rust-tools.nvim

Tools for better development in rust using neovim's builtin lsp
MIT License
2.17k stars 160 forks source link

Documentation request: clippy integration #208

Open RobertMenke opened 2 years ago

RobertMenke commented 2 years ago

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" },
    },
ok-nick commented 2 years ago

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?

ahkohd commented 2 years ago

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.

blachniet commented 1 year ago

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)
ok-nick commented 1 year ago

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.

bsamseth commented 1 year ago

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" },
                },
            },
        },
    },
})