What does the rust-analyzer error "could not resolve macro `$crate::format_args`" mean and how do I fix it?

7.3k views Asked by At

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.

3

There are 3 answers

1
Ibraheem Ahmed On BEST ANSWER

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
0
amishpanda On

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.

0
punkbit On

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