Skip to content

Commit 0042ba8

Browse files
author
Henri Lunnikivi
committed
cargo dev fmt
1 parent 9fcff0e commit 0042ba8

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

clippy_lints/src/field_reassign_with_default.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::utils::{match_def_path, paths, qpath_res, snippet, span_lint_and_note};
22
use if_chain::if_chain;
3-
use rustc_hir::def::Res;
43
use rustc_data_structures::fx::FxHashMap;
4+
use rustc_hir::def::Res;
55
use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
6-
use rustc_span::symbol::{Ident, Symbol};
76
use rustc_middle::ty::{self, Adt, TyS};
7+
use rustc_span::symbol::{Ident, Symbol};
88

99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -41,11 +41,11 @@ declare_lint_pass!(FieldReassignWithDefault => [FIELD_REASSIGN_WITH_DEFAULT]);
4141

4242
impl LateLintPass<'_> for FieldReassignWithDefault {
4343
fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) {
44-
4544
// find all binding statements like `let mut _ = T::default()` where `T::default()` is the
4645
// `default` method of the `Default` trait. and store statement index in current block being
4746
// checked and the name of the bound variable
48-
let binding_statements_using_default: Vec<(usize, Symbol, &TyS<'_>)> = enumerate_bindings_using_default(cx, block);
47+
let binding_statements_using_default: Vec<(usize, Symbol, &TyS<'_>)> =
48+
enumerate_bindings_using_default(cx, block);
4949

5050
// start from the `let mut _ = _::default();` and look at all the following
5151
// statements, see if they re-assign the fields of the binding
@@ -69,7 +69,9 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
6969
// shadow a binding
7070
else {
7171
// find out if and which field was set by this `consequtive_statement`
72-
if let Some((field_ident, assign_rhs)) = field_reassigned_by_stmt(consequtive_statement, &binding_name) {
72+
if let Some((field_ident, assign_rhs)) =
73+
field_reassigned_by_stmt(consequtive_statement, &binding_name)
74+
{
7375
// extract and store the assigned value for help message
7476
let value_snippet = snippet(cx, assign_rhs.span, "..");
7577

@@ -91,9 +93,10 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
9193
let stmt = &block.stmts[stmt_idx];
9294

9395
if let StmtKind::Local(preceding_local) = &stmt.kind {
94-
9596
// if all fields of the struct are not assigned, add `.. Default::default()` to the suggestion.
96-
let ext_with_default = !fields_of_type(&binding_type).iter().all(|field| assigned_fields.contains_key(&field.name));
97+
let ext_with_default = !fields_of_type(&binding_type)
98+
.iter()
99+
.all(|field| assigned_fields.contains_key(&field.name));
97100

98101
let field_list = assigned_fields
99102
.into_iter()
@@ -116,15 +119,18 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
116119
Some(preceding_local.span),
117120
&format!("consider initializing the variable with `{}`", sugg),
118121
);
119-
120122
}
121123
}
122124
}
123125
}
124126
}
125127

126-
/// Returns the block indices, identifiers and types of bindings set as `Default::default()`, except for when the pattern type is a tuple.
127-
fn enumerate_bindings_using_default<'cx, 'hir>(cx: &LateContext<'cx>, block: &Block<'hir>) -> Vec<(usize, Symbol, &'cx TyS<'cx>)> {
128+
/// Returns the block indices, identifiers and types of bindings set as `Default::default()`, except
129+
/// 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>)> {
128134
block
129135
.stmts
130136
.iter()
@@ -155,7 +161,8 @@ fn enumerate_bindings_using_default<'cx, 'hir>(cx: &LateContext<'cx>, block: &Bl
155161
None
156162
}
157163
}
158-
}).collect()
164+
})
165+
.collect()
159166
}
160167

161168
fn stmt_shadows_binding(this: &Stmt<'_>, shadowed: &Symbol) -> bool {
@@ -198,4 +205,4 @@ fn fields_of_type<'a>(ty: &'a TyS<'_>) -> Vec<Ident> {
198205
}
199206
}
200207
vec![]
201-
}
208+
}

0 commit comments

Comments
 (0)