Skip to content

tuple_array_conversions suggested code erases binding names and hides asymmetry #11085

Open
@oxalica

Description

@oxalica

Summary

In case of tuple elements are asymmetric, eg. (src, dst), simply tuple.into() erase all binding names and make it seem like all elements are interchangeable, also hiding the asymmetry.

let [src, dest]: [_; 2] = args.try_into().ok()?;
operations.push((src, dest)); // <- warning

Suggested code is not obvious what each element means. It seems like to be an iterator which is general over array length, but is actually not.

operations.push(<[_; 2]>::try_from(args).ok()?.into());

Lint Name

tuple_array_conversions

Reproducer

I tried this code:

use std::path::PathBuf;

pub fn parse_ops_from_args_warn(args: Vec<PathBuf>, operations: &mut Vec<(PathBuf, PathBuf)>) -> Option<()> {
    let [src, dest]: [_; 2] = args.try_into().ok()?;
    operations.push((src, dest)); // <- warning
    Some(())
}

pub fn parse_ops_from_args_suggested(args: Vec<PathBuf>, operations: &mut Vec<(PathBuf, PathBuf)>) -> Option<()> {
    operations.push(<[_; 2]>::try_from(args).ok()?.into());
    Some(())
}

I saw this happen:

warning: it looks like you're trying to convert an array to a tuple
 --> src/lib.rs:5:21
  |
5 |     operations.push((src, dest));
  |                     ^^^^^^^^^^^
  |
  = 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: no warnings.

Version

Clippy 0.1.72 (2023-07-02 839e9a6)

On Rust playground nightly.

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