21

I'm using rust-analyzer version 0.2.408 on Visual Studio Code.

I'm writing a command line application that involves centering text in the terminal. This is the function I wrote to do this:

use console::{Alignment, pad_str};

fn get_padded_row(row: &str, width: u16, symbol: Option<char>) -> String {
    let symbol = symbol.unwrap_or(' ');
    return pad_str(row, width as usize, Alignment::Center, None)
        .to_string()
        .replace(' ', &symbol.to_string());
}

This function works perfectly fine, and there were no errors with it. Then I wrote a test:

#[cfg(test)]
mod tests {
    use crate::get_padded_row;

    #[test]
    fn row_padding_dashes() {
        let padded_row = get_padded_row("hello", 15, Some('-'));
        assert_eq!(
            padded_row, "-----hello-----".to_string(),
            "`get_padded_row` was not correct, got `{}`", padded_row
        );
    }
}

The code still works perfectly fine. Both cargo run and cargo test work, the function passes the test, and cargo check returns no issues. But rust-analyzer gives an error, highlighting everything from the tr}; in the use statement to the p right after return: "could not resolve macro $crate::format_args rust-analyzer(macro-error)". Searching for this error returns nothing. VSCode links me to rust-analyzer user manual, which says only "This diagnostic is shown for macro expansion errors". Restarting VSCode and reinstalling rust-analyzer have done nothing. The error always comes back, and highlighting the same oddly specific region. The only way I've found to get rid of it while keeping rust-analyzer installed is to remove the test.

Judging from how the error is about macro expansion, and how removing the test fixes the issue, I'd imagine it's caused by the #[test] macro, but it's strange that rustc finds no issues at all with my code while rust-analyzer is freaking out about this error. So far, I've had better experiences with rust-analyzer than with the official Rust VSCode extension, but I'm on the verge of switching back to fix this issue.

Denendaden
  • 213
  • 2
  • 7

3 Answers3

29

This is a bug in rust-analyzer. For now, you can disable the warning in your settings.json:

"rust-analyzer.diagnostics.disabled": [
  "macro-error"
]

The bug was fixed on nightly, so you could install the nightly binary of rust-analyzer from GitHub, or you could just wait a couple days for the fix to land on stable.

Alternatively, you could downgrade to rls version 0.2.400, because the bug was caused by a commit in version 0.2.408:

Extensions Icon -> rust-analyzer -> Manage (gear icon) -> Install Another Version
Ibraheem Ahmed
  • 11,652
  • 2
  • 48
  • 54
  • installing nightly rust analyzer fixed the issue, but you need to have procMacro to `True` in your settings for rust analyzer – alexzander Feb 22 '22 at 17:53
5

Three months later and there seems to be a bug with Nightly release? Unsure.

I added unresolved-macro-call to Diagnostics: Disabled settings for rust-analyzer.

amishpanda
  • 91
  • 1
  • 8
4

I've tried many things, read the open issue on github, etc which is tagged as solved, but persists here.

For vscode users, open settings (json) and disable by adding:

    "rust-analyzer.procMacro.enable": false
punkbit
  • 7,347
  • 10
  • 55
  • 89