rust-lang / rust-lang/rust-clippy
`tuple_array_conversions` suggested code erases binding names and hides asymmetry
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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