rustrust-analyzer

Is there a way to stop rust-analyzer from dimming "inactive" code?


I am currently making an embedded system. For that I have 2 different panic handlers based on whether it is run normally or as a test. #[cfg(test)] for the test panic handler and #[cfg(not(test))] for the normal panic handler.

rust-analyzer says: code is inactive due to #[cfg] directives: test is enabled

and grays out the function. Test is never explicitly set so I cant just change it and I don't want to disable the graying out inactive code across the workspace.

Is there a way to either disable rust-analyzer checking the test cfg, or to disable the gray out just for this function

I tried finding infos about the test flag but I couldn't find any, i'm using VS Code if it is important


Solution

  • In VSCode's workspaces settings, set the following:

    "rust-analyzer.cargo.extraEnv": {
      "RUSTFLAGS": "--cfg rust_analyzer"
    }
    

    This will enable the #[cfg(rust_analyzer)] for rust-analyzer metadata inspection.

    Then, replace #[cfg(not(test))] with #[cfg(any(not(test), rust_analyzer))], and #[cfg(test)] with #[cfg(all(test, not(rust_analyzer)))]. This will disable and enable the functions for testing as usual, but when running in rust-analyzer, only the no-test function will be enabled.

    Important: this will only work if you don't need to set special RUSTFLAGS for your workspace.