Skip to content

Commit 6957d73

Browse files
hegzaebroto
andcommitted
Apply suggestions from code review
Code style issues. Co-authored-by: Eduardo Broto <[email protected]>
1 parent 94d0922 commit 6957d73

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

clippy_lints/src/field_reassign_with_default.rs

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

99
use rustc_lint::{LateContext, LateLintPass};
@@ -44,7 +44,7 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
4444
// find all binding statements like `let mut _ = T::default()` where `T::default()` is the
4545
// `default` method of the `Default` trait, and store statement index in current block being
4646
// checked and the name of the bound variable
47-
let binding_statements_using_default: Vec<(usize, Symbol, &TyS<'_>)> =
47+
let binding_statements_using_default =
4848
enumerate_bindings_using_default(cx, block);
4949

5050
// start from the `let mut _ = _::default();` and look at all the following
@@ -94,7 +94,7 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
9494

9595
if let StmtKind::Local(preceding_local) = &stmt.kind {
9696
// if all fields of the struct are not assigned, add `.. Default::default()` to the suggestion.
97-
let ext_with_default = !fields_of_type(&binding_type)
97+
let ext_with_default = !fields_of_type(binding_type)
9898
.iter()
9999
.all(|field| assigned_fields.contains_key(&field.name));
100100

@@ -114,7 +114,7 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
114114
span_lint_and_note(
115115
cx,
116116
FIELD_REASSIGN_WITH_DEFAULT,
117-
first_assign.unwrap_or_else(|| unreachable!()).span,
117+
first_assign.unwrap().span,
118118
"field assignment outside of initializer for an instance created with Default::default()",
119119
Some(preceding_local.span),
120120
&format!("consider initializing the variable with `{}`", sugg),
@@ -127,10 +127,7 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
127127

128128
/// Returns the block indices, identifiers and types of bindings set as `Default::default()`, except
129129
/// for when the pattern type is a tuple.
130-
fn enumerate_bindings_using_default<'cx, 'hir>(
131-
cx: &LateContext<'cx>,
132-
block: &Block<'hir>,
133-
) -> Vec<(usize, Symbol, &'cx TyS<'cx>)> {
130+
fn enumerate_bindings_using_default<'tcx>(cx: &LateContext<'tcx>, block: &Block<'_>) -> Vec<(usize, Symbol, Ty<'tcx>)> {
134131
block
135132
.stmts
136133
.iter()
@@ -141,6 +138,9 @@ fn enumerate_bindings_using_default<'cx, 'hir>(
141138
if let StmtKind::Local(ref local) = stmt.kind;
142139
// only take bindings to identifiers
143140
if let PatKind::Binding(_, _, ident, _) = local.pat.kind;
141+
// that are not tuples
142+
let ty = cx.typeck_results().pat_ty(local.pat);
143+
if !matches!(ty.kind(), ty::Tuple(_));
144144
// only when assigning `... = Default::default()`
145145
if let Some(ref expr) = local.init;
146146
if let ExprKind::Call(ref fn_expr, _) = &expr.kind;
@@ -149,12 +149,6 @@ fn enumerate_bindings_using_default<'cx, 'hir>(
149149
// right hand side of assignment is `Default::default`
150150
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
151151
then {
152-
// Get the type of the pattern
153-
let ty = cx.typeck_results().pat_ty(local.pat);
154-
// Ignore tuples
155-
if let ty::Tuple(_) = ty.kind() {
156-
return None;
157-
}
158152
Some((idx, ident.name, ty))
159153
} else {
160154
None
@@ -174,7 +168,7 @@ fn stmt_shadows_binding(this: &Stmt<'_>, shadowed: Symbol) -> bool {
174168
}
175169

176170
/// Returns the reassigned field and the assigning expression (right-hand side of assign).
177-
fn field_reassigned_by_stmt<'hir>(this: &Stmt<'hir>, binding_name: Symbol) -> Option<(Ident, &'hir Expr<'hir>)> {
171+
fn field_reassigned_by_stmt<'tcx>(this: &Stmt<'tcx>, binding_name: Symbol) -> Option<(Ident, &'tcx Expr<'tcx>)> {
178172
if_chain! {
179173
// only take assignments
180174
if let StmtKind::Semi(ref later_expr) = this.kind;
@@ -195,11 +189,11 @@ fn field_reassigned_by_stmt<'hir>(this: &Stmt<'hir>, binding_name: Symbol) -> Op
195189
}
196190

197191
/// Returns the vec of fields for a struct and an empty vec for non-struct ADTs.
198-
fn fields_of_type<'a>(ty: &'a TyS<'_>) -> Vec<Ident> {
192+
fn fields_of_type(ty: Ty<'_>) -> Vec<Ident> {
199193
if let Adt(adt, _) = ty.kind() {
200194
if adt.is_struct() {
201195
// unwrap is safe, because this is a struct and structs have only one variant
202-
let variant = &adt.variants.get(0_usize.into()).unwrap();
196+
let variant = &adt.non_enum_variant();
203197
return variant.fields.iter().map(|f| f.ident).collect();
204198
}
205199
}

0 commit comments

Comments
 (0)