rust-lang / rust-lang/rust-clippy

`tuple_array_conversions` lint makes code less readable when slice is involved

Open
#11,082 1 comment 0 reactions 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

I tried this code:

fn main() {
    let (a, b) = (1.0f64, 2.0f64);
    let _foo: &[f64] = &[a, b];
}

However, using .into() in this situation doesn't work, as the compiler infers the unsized type [f64]:

fn main() {
    let tuple = (1.0f64, 2.0f64);
    let _foo: &[f64] = &tuple.into();
}

produces:

error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): the trait bound `[f64]: From<(f64, f64)>` is not satisfied
 --> src/main.rs:3:31
  |
3 |     let _foo: &[f64] = &tuple.into();
  |                               ^^^^ the trait `From<(f64, f64)>` is not implemented for `[f64]`
  |

Using <[T; N]>::from works - however, IMO this code is significantly less readable:

fn main() {
    let tuple = (1.0f64, 2.0f64);
    let _foo: &[f64] = &<[f64; 2]>::from(tuple);
}

I would argue that the original code is the most readable in this situation (constructing a slice from a tuple), and this lint should not fire when a slice is involved.

Lint Name

tuple_array_conversions

Reproducer

I tried this code:

fn main() {
    let (a, b) = (1.0f64, 2.0f64);
    let _foo: &[f64] = &[a, b];
}

However, using .into() in this situation doesn't work, as the compiler infers the unsized type [f64]:

fn main() {
    let tuple = (1.0f64, 2.0f64);
    let _foo: &[f64] = &tuple.into();
}

produces:

error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): the trait bound `[f64]: From<(f64, f64)>` is not satisfied
 --> src/main.rs:3:31
  |
3 |     let _foo: &[f64] = &tuple.into();
  |                               ^^^^ the trait `From<(f64, f64)>` is not implemented for `[f64]`
  |
Version
rustc 1.72.0-nightly (839e9a6e1 2023-07-02)
binary: rustc
commit-hash: 839e9a6e1210934fd24b15548b811a97c77138fc
commit-date: 2023-07-02
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5
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 by locating the tuple_array_conversions lint implementation and its existing tests, then run the Rust reproducer from the issue. Update the lint behavior so the slice-involved case is not flagged, and verify the reproducer no longer receives the readability-reducing suggestion while other tuple-to-array conversions remain covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.