Skip to content

Only using a parameter in a macro call followed by an assignment causes the only_used_in_recursion lint to trigger #8556

Closed
@ghost

Description

Summary

The only_used_in_recursion lint seems to cause false positives when the parameter is used as the index into an array stored inside a struct, passing both into a macro, and the result of the macro is assigned to. Using just an array instead of a struct or tuple as the parameter, or not having an assignment in that position, makes the issue go away, and so does writing the same code without using an intermediate macro (which would be less ergonomic in the use case this issue came up in).

Lint Name

only_used_in_recursion

Reproducer

I tried this code:

macro_rules! do_index {
    ($arr: expr, $index: expr) => {
        $arr.1[$index as usize]
    };
}

fn do_thing(s: &mut (u8, [u32; 4]), index: u8, value: u32) {
    do_index!(s, index) = value;
}

I saw this happen:

warning: parameter is only used in recursion
 --> src/lib.rs:7:39
  |
7 | fn do_thing(arr: &mut (u8, [u32; 4]), index: u8, value: u32) {
  |                                       ^^^^^ help: if this is intentional, prefix with an underscore: `_index`
  |
  = note: `#[warn(clippy::only_used_in_recursion)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#only_used_in_recursion

I expected to see this happen: (no warning)

Version

rustc 1.61.0-nightly (461e80780 2022-03-16)
binary: rustc
commit-hash: 461e8078010433ff7de2db2aaae8a3cfb0847215
commit-date: 2022-03-16
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions