3

Rust analyzer notifying me of any type errors in my code can be immensely useful, both in production code and tests. However, when making a large refactoring, errors in tests are just noise up to the point where the production code is error free.

Is there any way to temporarily make RA ignore errors in tests? Bonus points for solutions that work for the VS Code plugin.

--Edit
I should point out that I'm particularly interested in silencing these errors in the "Problems" window of VS Code

askedJul 20 at 20:43
NoBullsh1t's user avatar
4
  • 1
    You can set a setting, but that will require unsetting it <s>and restarting rust-analyzer</s> (looks like it doesn't require restarting).CommentedJul 20 at 20:57
  • 3
    rust-analyzer.github.io/book/configuration.html#cfg.setTestCommentedJul 20 at 20:59
  • Instead of disabling errors in tests why won't you simply fix them? That would be infinitely more useful. Honestly, this sounds like XY problem.CommentedJul 21 at 13:31
  • 1
    It's beneficial to have some way to 'divide and conquer' when dealing with large amount of compiler errors during a refactor. Hence the request is about a way to temporary de-prioritize errors in tests - with the aim to enable those back once refactored changes crystalize.CommentedJul 21 at 16:48

2 Answers2

4

Rust-analyzer's documentationdescribes the optioncfg.setTest which allows to disabletest feature when language server compiles the crate and suppress all errors in#[test] and#[cfg(test)] blocks. It'strue by default, so setting it tofalse prevents errors in test cases from being produced.

It's possible to set this option tofalse temporarily in VSCode's user-sidesettings.json ifrust-analyzer plugin is installed:

{  "rust-analyzer.cfg.setTest": false}

Note that the current implementation would also exclude benchmark targets. Argument is applied tocargo check on the nightly version ofrust-analyzer at the moment or writing (the patch was merged after the initial reply).

Another option which provides a bit more control on which targets should be included is to providecheck.allTargets option asfalse (socargo check wouldn't receive--all-targets flag), and specify target flags manually:

{  "rust-analyzer.check.allTargets": false,  "rust-analyzer.check.extraArgs": ["--bins", "--examples"]}

That way it would be possible to decide if--examples,--benches and--lib/--bins should be included or not.

answeredJul 20 at 21:46
lufterd's user avatar
Sign up to request clarification or add additional context in comments.

3 Comments

I'll retest later, but I believe this option did make the test appear as though unused (greyed out), but the "Problems" window in VS Code still listed all the errors in tests. And window is quite helpful to jump from one error to the next. So no, I don't think this is the full solution
@NoBullsh1t This option indeed disables rust-analyzer's own analysis on tests, but does not disablecargo check on them (maybe this is something we should change). To disablecargo check too, you need to setrust-analyzer.check.overrideCommand.
1

According to experimentation, to silence errors in tests in the "Problems" section of VS Code, what works is setting

{    "rust-analyzer.check.allTargets": false}

though I'm not yet sure if that may perhaps be a little too strict and perhaps will disable other configurations as well.

You can use this until the changes by @Chayim Friedman are available to you

answeredJul 21 at 19:29
NoBullsh1t's user avatar

1 Comment

This disables checking binaries, benchmarks and examples as well.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.