Skip to content

tuple_array_conversions misses implicit &mut -> & conversion subtelty #11100

Closed
@HadrienG2

Description

@HadrienG2

Summary

I have code that uses a double buffering pattern, and as a result has multiple instances of...

  • Starting from &mut [T; 2] (representing a borrow of the underlying data store) and turning it into the heterogeneous tuple (&T, &mut T) (representing input and output accessors).
  • Starting from such a heterogeneous tuple and downgrading it into a [&T; 2] (in cases where the distinction between input and output is not necessary and I just want read only access to everything).

These conversions are performed using pairs of array and slice patterns, which clippy's tuple_array_conversions lint will incorrectly suggest turning into from() or into() statements. The tuple <-> array From/Into impl actually are not powerful enough to do this.

Lint Name

tuple_array_conversions

Reproducer

I tried this code:

pub struct Evolving([usize; 2]);

impl Evolving {
    pub fn in_out(&mut self) -> (&usize, &mut usize) {
        let [input, output] = &mut self.0;
        (input, output)
    }
}

I saw this happen:

warning: it looks like you're trying to convert an array to a tuple
   --> data/src/concentration/mod.rs:124:9
    |
124 |         (input, output)
    |         ^^^^^^^^^^^^^^^
    |
    = help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::from` if type annotations are needed
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tuple_array_conversions
    = note: `#[warn(clippy::tuple_array_conversions)]` on by default

I expected to see this happen: clippy understands that this conversion cannot be done via into() and does not suggest it.

Version

rustc 1.72.0-nightly (0ab38e95b 2023-07-03)
binary: rustc
commit-hash: 0ab38e95bb1cbf0bd038d359bdecbfa501f003a7
commit-date: 2023-07-03
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5

Additional Labels

@rustbot label +I-suggestion-causes-error +L-complexity

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 haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-complexityLint: Belongs in the complexity lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions