rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.26k stars 1.61k forks source link

analyzer: code is inactive due to #[cfg] directives: doc is disabled #14395

Open nipunn1313 opened 1 year ago

nipunn1313 commented 1 year ago

The inactive-code check appears to false positive (IMO) on #[cfg(doc)].

Sample code

#[cfg(doc)]
use std::future::Future;

/// Handy struct that pairs well with [`Future`].
pub struct Past;

One could potentially argue that this is behaving-as-designed, since it's analyzing the code, but not the docs - but I found it a bit un-useful.

Some possible proposed solutions

bjorn3 commented 1 year ago

Rust-analyzer can only analyze one cfg() configuration at a time. Any code inside an inactive #[cfg] will have most ide functionality like autocompletion, rust-analyzer native diagnostics and inlay hints disabled. We have a warning to prevent confusion about why ide functionality doesn't work in these blocks.

nipunn1313 commented 1 year ago

cool - that makes sense. It's a bit of a bummer, but an understandable one. Out of curiosity, what does rust-analyzer use as a default cfg?

bjorn3 commented 1 year ago

I believe it uses the default of rustc for the respective target + all features enabled when the root is compiled with --all-features + cfg(test) (unless the rust-analyzer.cargo.unsetTest config contains the name of the respective crate).

nipunn1313 commented 1 year ago

Is there an easy way to enable --all-features + cfg(test) + cfg(doc) ?

I tried setting --cfg doc in cargo.extraEnv but it seemed to disable some of the other cfgs (like proc macro)

doc feel like a special language-endorsed cfg (like test) and I wonder if there's a discussion/argument for including it in the default.

threema-donat commented 1 year ago

I'm also looking for a way to enable all features, since rust-analyzer.cargo.allFeatures apparently does not exist anymore (as suggested here).

bjorn3 commented 1 year ago

You can set rust-analyzer.cargo.features to "all" rather than an array of features to enable all features.

nine9ths commented 11 months ago

When I set "rust-analyzer.cargo.features": ["all"] in VS Code I get the following error:

[ERROR rust_analyzer::main_loop] FetchWorkspaceError:
rust-analyzer failed to load workspace: Failed to load the project at ...snip.../rust/Cargo.toml: Failed to read Cargo metadata from Cargo.toml file ...snip.../rust/Cargo.toml, Some(Version { major: 1, minor: 71, patch: 0 }): Failed to run `cd "...snip.../rust" && "cargo" "metadata" "--format-version" "1" "--features" "all" "--manifest-path" "...snip.../rust/Cargo.toml" "--filter-platform" "aarch64-apple-darwin"`: `cargo metadata` exited with an error: error: none of the selected packages contains these features: all

If I set it to a specific feature it seems to work fine. Not sure what I'm missing.

flodiebold commented 11 months ago

@nine9ths Try "rust-analyzer.cargo.features": "all".

jcalabro commented 6 months ago

Perhaps there could be something similar to #[allow(dead_code)] for circumstances such as these? It's really nice to have no noise from linters in the editor.