Closed
Description
Summary
A lint about tuple-array conversions triggers when bindings created by tuple destructuring are used to create an array (or the other way around). However, the lint doesn't check whether this is the only use of such bindings, in which case using into()
would harm readability by forcing changes to other binding uses.
Lint Name
tuple_array_conversions
Reproducer
I tried this code:
fn get_tuple() -> (i32, i32) {
(1, 2)
}
fn iter_arrays() -> impl Iterator<Item = [i32; 2]> {
[].into_iter()
}
fn do_something(_: i32) -> i32 {
3
}
fn main() {
let (a, b) = get_tuple();
for x in [a, b] {
println!("use both: {x}");
}
do_something(a);
iter_arrays()
.map(|[a, b]| (do_something(a), (a, b)))
.for_each(drop);
}
I saw this happen:
$ cargo clippy
Checking clt v0.1.0 (/home/mwk/clt)
warning: it looks like you're trying to convert a tuple to an array
--> src/main.rs:15:14
|
15 | for x in [a, b] {
| ^^^^^^
|
= help: use `.into()` instead, or `<[T; N]>::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
warning: it looks like you're trying to convert an array to a tuple
--> src/main.rs:20:41
|
20 | .map(|[a, b]| (do_something(a), (a, b)))
| ^^^^^^
|
= 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
warning: `clt` (bin "clt") generated 2 warnings
Finished dev [unoptimized + debuginfo] target(s) in 0.08s
I expected to see this happen:
(no warning)
Version
rustc 1.72.0-nightly (85bf07972 2023-07-06)
binary: rustc
commit-hash: 85bf07972a1041b9e25393b803d0e006bec3eaaf
commit-date: 2023-07-06
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5
Additional Labels
No response