Skip to content

Commit

Permalink
Fix FP
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jun 27, 2023
1 parent d9f8dee commit 91c452a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
15 changes: 11 additions & 4 deletions clippy_lints/src/tuple_array_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clippy_utils::{
path_to_local,
};
use itertools::Itertools;
use rustc_ast::LitKind;
use rustc_hir::{Expr, ExprKind, Node, Pat};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::{lint::in_external_macro, ty};
Expand Down Expand Up @@ -82,8 +83,9 @@ fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {

if let Some(elements) = elements
.iter()
.map(|expr| {
if let ExprKind::Field(path, _) = expr.kind {
.enumerate()
.map(|(i, expr)| {
if let ExprKind::Field(path, field) = expr.kind && field.as_str() == i.to_string() {
return Some(path);
};

Expand Down Expand Up @@ -146,8 +148,13 @@ fn check_tuple<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {

if let Some(elements) = elements
.iter()
.map(|expr| {
if let ExprKind::Index(path, _) = expr.kind {
.enumerate()
.map(|(i, expr)| {
if let ExprKind::Index(path, index) = expr.kind
&& let ExprKind::Lit(lit) = index.kind
&& let LitKind::Int(val, _) = lit.node
&& val as usize == i
{
return Some(path);
};

Expand Down
2 changes: 0 additions & 2 deletions tests/ui/tuple_array_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ fn main() {
let y = (1, 2);
[x.0, y.0];
[x.0, y.1];
// FP
let x = [x.0, x.0];
let x = (x[0], x[0]);
// How can this be fixed?
external! {
let t1: &[(u32, u32)] = &[(1, 2), (3, 4)];
let v1: Vec<[u32; 2]> = t1.iter().map(|&(a, b)| [a, b]).collect();
Expand Down
18 changes: 1 addition & 17 deletions tests/ui/tuple_array_conversions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,6 @@ LL | t1.iter().for_each(|&(a, b)| _ = [a, b]);
|
= help: use `.into()` instead, or `<[T; N]>::from` if type annotations are needed

error: it looks like you're trying to convert a tuple to an array
--> $DIR/tuple_array_conversions.rs:43:13
|
LL | let x = [x.0, x.0];
| ^^^^^^^^^^
|
= help: use `.into()` instead, or `<[T; N]>::from` if type annotations are needed

error: it looks like you're trying to convert an array to a tuple
--> $DIR/tuple_array_conversions.rs:44:13
|
LL | let x = (x[0], x[0]);
| ^^^^^^^^^^^^
|
= help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::from` if type annotations are needed

error: it looks like you're trying to convert an array to a tuple
--> $DIR/tuple_array_conversions.rs:71:13
|
Expand All @@ -95,5 +79,5 @@ LL | let x = (x[0], x[1]);
|
= help: use `.into()` instead, or `<(T0, T1, ..., Tn)>::from` if type annotations are needed

error: aborting due to 12 previous errors
error: aborting due to 10 previous errors

0 comments on commit 91c452a

Please sign in to comment.