Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions clippy_lints/src/tuple_array_conversions.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::is_from_proc_macro;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::res::MaybeResPath;
use clippy_utils::visitors::for_each_local_use_after_expr;
use clippy_utils::visitors::{for_each_expr, for_each_local_use_after_expr};
use clippy_utils::{get_enclosing_block, is_from_proc_macro};
use itertools::Itertools;
use rustc_ast::LitKind;
use rustc_hir::{Expr, ExprKind, Node, PatKind};
use rustc_hir::{Expr, ExprKind, HirId, Node, PatKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty::{self, Ty};
use rustc_session::impl_lint_pass;
Expand Down Expand Up @@ -170,6 +170,7 @@ fn all_bindings_are_for_conv<'tcx>(
&& locals
.iter()
.all(|&l| for_each_local_use_after_expr(cx, l, expr.hir_id, |_| ControlFlow::Break::<()>(())).is_continue())
&& !locals.iter().any(|&l| local_used_until_expr(cx, l, expr.hir_id))
&& local_parents.first().is_some_and(|node| {
let Some(ty) = match node {
Node::Pat(pat) => Some(pat.hir_id),
Expand Down Expand Up @@ -208,3 +209,22 @@ impl PartialEq<PatKind<'_>> for ToType {
}
}
}

fn local_used_until_expr(cx: &LateContext<'_>, local_id: HirId, expr_id: HirId) -> bool {
let Some(b) = get_enclosing_block(cx, local_id) else {
return false;
};

for_each_expr(cx, b, |e| {
if e.hir_id == expr_id {
return ControlFlow::Break(false);
}

if e.res_local_id() == Some(local_id) {
return ControlFlow::Break(true);
}

ControlFlow::Continue(())
})
.unwrap_or(false)
}
16 changes: 16 additions & 0 deletions tests/ui/tuple_array_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,19 @@ fn msrv_juust_right() {
let x = &[1, 2];
let x = (x[0], x[1]);
}

fn issue16192() {
fn do_something(tuple: (u32, u32)) {}
fn produce_array() -> [u32; 2] {
[1, 2]
}

let [a, b] = produce_array();
for tuple in [(a, b), (b, a)] {
do_something(tuple);
}

let [a, b] = produce_array();
let x = b;
do_something((a, b));
}
Loading