rust-analyzer: Why rust-analyzer doesn't work in [test] function?

#[test]
fn test_fn(){
... //rust-analyzer doesn't work here! NO error prompt until you really compile file.
}

Is it a bug or a config/setting which should properly toggled?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

@Wyvern I confirm that --all-targets need to be enabled. It should probably be enabled by default, there’s no reason to avoid showing errors on test. For reference, my setup on vscode is

"rust-analyzer.checkOnSave.overrideCommand": [
        "cargo",
        "+nightly",
        "clippy",
        "--all",
        "--all-targets",
        "--all-features",
        "--message-format=json",
        "--",
        "-D",
        "clippy::all"
    ],

Had the same issue. The solution is to enable experimental diagnostics mode. My neovim specific lsp config:

-- snip
["rust_analyzer"] = function()
  require 'lspconfig'.rust_analyzer.setup {
    settings = {
      ["rust-analyzer"] = {
        diagnostics = {
          disabled = "unlinked-file",
          experimental = {
            enable = true,
          },
        },
      },
    },
  }
end,
-- snip

and for VS Code users, I think this should suffice:

"rust-analyzer.diagnostics.experimental.enable": true

I was able to resolve this similar to @nikkolasg but I had to enable checking tests, examples, and benches explicitly as well:

    "rust-analyzer.checkOnSave.overrideCommand": [
        "cargo",
        "+nightly",
        "clippy",
        "--workspace",
        "--all-targets",
        "--all-features",
        "--tests",
        "--examples",
        "--benches",
        "--message-format=json",
    ],

Had the same issue. The solution is to enable experimental diagnostics mode. My neovim specific lsp config:

-- snip
["rust_analyzer"] = function()
  require 'lspconfig'.rust_analyzer.setup {
    settings = {
      ["rust-analyzer"] = {
        diagnostics = {
          disabled = "unlinked-file",
          experimental = {
            enable = true,
          },
        },
      },
    },
  }
end,
-- snip

and for VS Code users, I think this should suffice:

"rust-analyzer.diagnostics.experimental.enable": true

Thank you - this was bothering me so much. I’m not sure why some projects need this and others do not.