Skip to content

Commit 9fcff0e

Browse files
author
Henri Lunnikivi
committed
Ignore tuples
1 parent 8907903 commit 9fcff0e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

clippy_lints/src/field_reassign_with_default.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hir::def::Res;
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
66
use rustc_span::symbol::{Ident, Symbol};
7-
use rustc_middle::ty::{Adt, TyS};
7+
use rustc_middle::ty::{self, Adt, TyS};
88

99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -123,7 +123,7 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
123123
}
124124
}
125125

126-
/// Returns the block indices, identifiers and types of bindings set as `Default::default()`.
126+
/// Returns the block indices, identifiers and types of bindings set as `Default::default()`, except for when the pattern type is a tuple.
127127
fn enumerate_bindings_using_default<'cx, 'hir>(cx: &LateContext<'cx>, block: &Block<'hir>) -> Vec<(usize, Symbol, &'cx TyS<'cx>)> {
128128
block
129129
.stmts
@@ -145,6 +145,10 @@ fn enumerate_bindings_using_default<'cx, 'hir>(cx: &LateContext<'cx>, block: &Bl
145145
then {
146146
// Get the type of the pattern
147147
let ty = cx.typeck_results().pat_ty(local.pat);
148+
// Ignore tuples
149+
if let ty::Tuple(_) = ty.kind() {
150+
return None;
151+
}
148152
Some((idx, ident.name, ty))
149153
}
150154
else {

tests/ui/field_reassign_with_default.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,10 @@ fn main() {
6666
// wrong, produces fourth error in stderr
6767
let mut a = A::default();
6868
a.i = 42;
69+
70+
// wrong, but does not produce an error in stderr, because we can't produce a correct kind of
71+
// suggestion with current implementation
72+
let mut c: (i32, i32) = Default::default();
73+
c.0 = 42;
74+
c.1 = 21;
6975
}

0 commit comments

Comments
 (0)