Skip to content

VsCode: spurious "x is never used" if Rust-analyzer > Check On Save > All Targets, is selected in rust-analyzer options #8626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DanielJoyce opened this issue Apr 22, 2021 · 18 comments
Labels
A-diagnostics diagnostics / error reporting C-support Category: support questions S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@DanielJoyce
Copy link

VSCode:

Version: 1.54.3
Commit: 2b9aebd5354a3629c3aba0a5f5df49f43d6689f8
Date: 2021-03-15T10:55:24.277Z
Electron: 11.3.0
Chrome: 87.0.4280.141
Node.js: 12.18.3
V8: 8.7.220.31-electron.0
OS: Linux x64 5.8.0-7642-generic

Rust Analyzer Version: 0.2.565

Rust Version: 1.51

If I disable Rust-analyzer > Check On Save > All Targets, the spurious warnings go away.

Cargo check also does not show any matching warnings.

It seems that when merging results between targets, especially when checking the test target and the default target, rust analyzer gets confused or doesn't do it properly?

@DanielJoyce
Copy link
Author

This has caused me to remove what I thought was dead code, then having cargo build fail because I deleted code that was in use. 😛

@lnicola
Copy link
Member

lnicola commented Apr 22, 2021

Do you have a code sample?

@DanielJoyce
Copy link
Author

DanielJoyce commented Apr 22, 2021

If I disable the feature, then rust-analyzer stops checking tests.

@DanielJoyce
Copy link
Author

I can attach logs if someone wants, then I check it off, here is the config shown in the output of rust analyzer

INFO [4/22/2021, 11:22:16 AM]: Extension version: 0.2.565
INFO [4/22/2021, 11:22:16 AM]: Using configuration {
  cargoRunner: null,
  runnableEnv: null,
  inlayHints: {
    enable: true,
    chainingHints: true,
    maxLength: 25,
    parameterHints: false,
    typeHints: false
  },
  updates: { channel: 'stable', askBeforeDownload: true },
  server: { path: null, extraEnv: null },
  trace: { server: 'off', extension: false },
  debug: {
    engine: 'auto',
    sourceFileMap: {
      '/rustc/<id>': '${env:USERPROFILE}/.rustup/toolchains/<toolchain-id>/lib/rustlib/src/rust'
    },
    openDebugPane: false,
    engineSettings: {}
  },
  assist: {
    importMergeBehavior: 'last',
    importPrefix: 'plain',
    importGroup: true
  },
  callInfo: { full: true },
  cargo: {
    autoreload: true,
    allFeatures: true,
    features: [],
    runBuildScripts: true,
    useRustcWrapperForBuildScripts: true,
    noDefaultFeatures: false,
    target: null,
    noSysroot: false,
    loadOutDirsFromCheck: true
  },
  checkOnSave: {
    enable: true,
    allFeatures: null,
    allTargets: true,
    command: 'check',
    noDefaultFeatures: null,
    target: null,
    extraArgs: [],
    features: null,
    overrideCommand: null
  },
  completion: {
    addCallArgumentSnippets: true,
    addCallParenthesis: true,
    postfix: { enable: true },
    autoimport: { enable: true }
  },
  diagnostics: {
    enable: true,
    enableExperimental: true,
    disabled: [],
    warningsAsHint: [],
    warningsAsInfo: []
  },
  files: { watcher: 'client', excludeDirs: [] },
  hoverActions: {
    debug: true,
    enable: true,
    gotoTypeDef: true,
    implementations: true,
    run: true,
    linksInHover: true
  },
  lens: {
    debug: true,
    enable: true,
    implementations: true,
    run: true,
    methodReferences: false,
    references: false
  },
  linkedProjects: [],
  lruCapacity: null,
  notifications: { cargoTomlNotFound: true },
  procMacro: { enable: true, server: null },
  runnables: { overrideCargo: null, cargoExtraArgs: [] },
  rustcSource: null,
  rustfmt: { extraArgs: [], overrideCommand: null }
}

When I turn it off:

INFO [4/22/2021, 11:24:43 AM]: Extension version: 0.2.565
INFO [4/22/2021, 11:24:43 AM]: Using configuration {
  cargoRunner: null,
  runnableEnv: null,
  inlayHints: {
    enable: true,
    chainingHints: true,
    maxLength: 25,
    parameterHints: false,
    typeHints: false
  },
  updates: { channel: 'stable', askBeforeDownload: true },
  server: { path: null, extraEnv: null },
  trace: { server: 'off', extension: false },
  debug: {
    engine: 'auto',
    sourceFileMap: {
      '/rustc/<id>': '${env:USERPROFILE}/.rustup/toolchains/<toolchain-id>/lib/rustlib/src/rust'
    },
    openDebugPane: false,
    engineSettings: {}
  },
  assist: {
    importMergeBehavior: 'last',
    importPrefix: 'plain',
    importGroup: true
  },
  callInfo: { full: true },
  cargo: {
    autoreload: true,
    allFeatures: true,
    features: [],
    runBuildScripts: true,
    useRustcWrapperForBuildScripts: true,
    noDefaultFeatures: false,
    target: null,
    noSysroot: false,
    loadOutDirsFromCheck: true
  },
  checkOnSave: {
    enable: true,
    allFeatures: null,
    allTargets: false,
    command: 'check',
    noDefaultFeatures: null,
    target: null,
    extraArgs: [],
    features: null,
    overrideCommand: null
  },
  completion: {
    addCallArgumentSnippets: true,
    addCallParenthesis: true,
    postfix: { enable: true },
    autoimport: { enable: true }
  },
  diagnostics: {
    enable: true,
    enableExperimental: true,
    disabled: [],
    warningsAsHint: [],
    warningsAsInfo: []
  },
  files: { watcher: 'client', excludeDirs: [] },
  hoverActions: {
    debug: true,
    enable: true,
    gotoTypeDef: true,
    implementations: true,
    run: true,
    linksInHover: true
  },
  lens: {
    debug: true,
    enable: true,
    implementations: true,
    run: true,
    methodReferences: false,
    references: false
  },
  linkedProjects: [],
  lruCapacity: null,
  notifications: { cargoTomlNotFound: true },
  procMacro: { enable: true, server: null },
  runnables: { overrideCargo: null, cargoExtraArgs: [] },
  rustcSource: null,
  rustfmt: { extraArgs: [], overrideCommand: null }
}

@DanielJoyce
Copy link
Author

DanielJoyce commented Apr 22, 2021

Okay, I've tracked this down to the use of mockall_double::double macro, which is an attribute macro used to rewrite imports for testing.

When run in release, it uses one impl, in test it uses another.

It basically emits in test mode:

#[cfg(test)]
pub use crat::some_module::MockClient as Client

and in debug/release:

#[cfg(not(test))]
pub use crate::some_module::Client

So rust analyzer is facing two mutually exclusive module imports on test vs debug/release

I don't know if this is 'fixable', I admit it's a bit weird. But I suspect this might come up with rust_analyzer facing any mutually exclusive of cfg based features as well.

@DanielJoyce
Copy link
Author

I'll see what the behavior is with explicit cfg values.

@DanielJoyce
Copy link
Author

DanielJoyce commented Apr 22, 2021

Manually expanded:

    #[cfg(test)] // These two lines are bright and highlighted, telling me this is the config rust-analyzer is using
    pub use super::some_crate::MockClient as Client; // Rust analyzer complains here, says it can't find it
    #[cfg(not(test))] // Greyed out/pale in vsCode
    pub use super::some_crate::HbaseQueryClient;

Cargo check is still happy, but rust_analyzer now says it can't find MockClient, though MockClient should be created from Client via the mock_all macro.

If I get some time, I will see if I can gin up a simple POC. I believe the fundamental issue might be support of #[cfg(not(test))] ?

@lnicola
Copy link
Member

lnicola commented Apr 22, 2021

Note that we don't support attribute proc macros, nor crate splitting for your case (#7459).

@DanielJoyce
Copy link
Author

DanielJoyce commented Apr 22, 2021

I think I have a fix for mockall_double.

EDIT: Nope. Pebkac, but I just learned something...

@flodiebold
Copy link
Member

Do you not get the warning with cargo check --all-targets?

@DanielJoyce
Copy link
Author

DanielJoyce commented Apr 22, 2021

I get the warning with --all-targets, I do not get it with it turned off.

EDIT: I mean with --all-targets enabled for rust-analyzer. Cargo checks them correctly, Rust-analyzer produces more warnings.

@flodiebold
Copy link
Member

Then this is a cargo bug, if at all. (This might very well be expected behavior though.)

@DanielJoyce
Copy link
Author

@lnicola So #8486 refernced in #7459 won't fix it?

@flodiebold
Copy link
Member

No.

@DanielJoyce
Copy link
Author

Sorry, I mean, cargo check --all-targets works correctly. rust-analyzer with check-all-targets reports more unused variables (spurious warnings) than cargo does with the same flag.

@flodiebold
Copy link
Member

@DanielJoyce Can you provide an example project that shows this behavior?

@matklad matklad added the S-unactionable Issue requires feedback, design decisions or is blocked on other work label Apr 27, 2021
@flodiebold
Copy link
Member

To be clear, rust-analyzer currently doesn't emit "unused variable" warnings from its own analysis, so these warnings can only come from cargo check. So the only question here is what's the difference between how RA is calling cargo check, vs. how you are calling it on the command line. You might try cargo check --all-targets --all-features.

@Veykril Veykril added the A-diagnostics diagnostics / error reporting label Dec 14, 2021
@flodiebold flodiebold added the C-support Category: support questions label Jun 28, 2022
@lnicola
Copy link
Member

lnicola commented Jul 22, 2024

Not enough info to reproduce this, closing.

@lnicola lnicola closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics diagnostics / error reporting C-support Category: support questions S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests

5 participants