rust-lang / rust-lang/rust-clippy

`tuple_array_conversions` suggested code erases binding names and hides asymmetry

Open
#11,085 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

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

Start at the tuple_array_conversions lint entry point and reproduce the Vec<PathBuf> to (PathBuf, PathBuf) example from the issue. Determine the intended behavior for asymmetric tuple bindings, then verify that the resulting diagnostic or suggestion matches that behavior and no longer obscures the element names.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.