rust-lang / rust-lang/rust-analyzer

False positive "unused variable" with recursive macro

Open
#15,679 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-macro C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: 0.4.1676-standalone

rustc version: rustc 1.72.0 (5680fa18f 2023-08-23)

relevant settings: Not sure of any relevant settings. I typically use "rust-analyzer.procMacro.enable": true in the settings, but this doesn't seem to affect the issue.

I'm getting false positive "unused variable" warnings from rust-analyzer in the following recursive macro:

// Simplified example:
struct Node;

impl Node {
    fn new() -> Self {
        Node {}
    }
    fn add_child(&self, _child: Node) {
        unimplemented!()
    }
}

macro_rules! assemble_tree {
    ($base:expr => { $($other:tt)* }) => {
        assemble_tree!( @recurse, $base, $($other)*)
    };

    // Patterns for 'child' syntax
    (@recurse, $base:expr, $child:expr $(,)?) => {
        $base.add_child($child)
    };
    (@recurse, $base:expr, $child:expr, $($other:tt)+) => {
        $base.add_child($child);
        assemble_tree!( @recurse, $base, $($other)*)
    };

    // Patterns for 'child => { ... }' syntax
    (@recurse, $base:expr, $child:expr => { $($children:tt)* } $(,)?) => {
        let temp = $child;
        assemble_tree!( temp => { $($children)* });
        $base.add_child(temp)
    };
    (@recurse, $base:expr, $child:expr => { $($children:tt)* }, $($other:tt)+) => {
        let temp = $child;
        assemble_tree!( temp => { $($children)* });
        $base.add_child(temp);
        assemble_tree!( @recurse, $base, $($other)*)
    };
}

fn main() {
    // No warnings in this case:
    assemble_tree!(
        Node::new() => {
            Node::new() => {
                Node::new()
            }
        }
    );

    // But "unused variable" warnings in this case:
    assemble_tree!(
        Node::new() => {
            Node::new() => {
                Node::new() => {
                    Node::new()
                }
            }
        }
    );
}

image

I don't think there is an unused variable here, cargo build and cargo clippy don't complain about anything.

On first glance it looks like the issue has to do with the depth of the assembled tree, i.e., the depth of the macro recursion, because usages that only involve up to 3 level do not produce a warning, but usages that go 4 levels deep start to produce the warning.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report's simplified recursive assemble_tree! macro, especially the four-level case, and compare rust-analyzer's unused-variable diagnostics with cargo build and cargo clippy. Done means the recursive macro no longer produces a false unused-variable warning while valid diagnostics remain.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.