rust-lang / rust-analyzer

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

False positive "unused variable" with recursive macro #15679

Open bluenote10 opened 11 months ago

bluenote10 commented 11 months ago

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.

HKalbasi commented 11 months ago

Ah, this one is a macro hygienic issue. Here is the expanded code:

let temp = (Node::new());
let temp = (Node::new());
temp.add_child((Node::new()));
temp.add_child(temp);
(Node::new()).add_child(temp)

In reality, those temps are different, but r-a treats them as equal, so it thinks the first temp is an unused variable.