Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Code style issues.

Co-authored-by: Eduardo Broto <[email protected]>
  • Loading branch information
hegza and ebroto authored Sep 15, 2020
1 parent e1fe36d commit a42ed12
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions clippy_lints/src/field_reassign_with_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use if_chain::if_chain;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::Res;
use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
use rustc_middle::ty::{self, Adt, TyS};
use rustc_middle::ty::{self, Adt, Ty};
use rustc_span::symbol::{Ident, Symbol};

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

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

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

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

/// Returns the block indices, identifiers and types of bindings set as `Default::default()`, except
/// for when the pattern type is a tuple.
fn enumerate_bindings_using_default<'cx, 'hir>(
cx: &LateContext<'cx>,
block: &Block<'hir>,
) -> Vec<(usize, Symbol, &'cx TyS<'cx>)> {
fn enumerate_bindings_using_default<'tcx>(cx: &LateContext<'tcx>, block: &Block<'_>) -> Vec<(usize, Symbol, Ty<'tcx>)> {
block
.stmts
.iter()
Expand All @@ -141,6 +138,9 @@ fn enumerate_bindings_using_default<'cx, 'hir>(
if let StmtKind::Local(ref local) = stmt.kind;
// only take bindings to identifiers
if let PatKind::Binding(_, _, ident, _) = local.pat.kind;
// that are not tuples
let ty = cx.typeck_results().pat_ty(local.pat);
if !matches!(ty.kind(), ty::Tuple(_));
// only when assigning `... = Default::default()`
if let Some(ref expr) = local.init;
if let ExprKind::Call(ref fn_expr, _) = &expr.kind;
Expand All @@ -149,12 +149,6 @@ fn enumerate_bindings_using_default<'cx, 'hir>(
// right hand side of assignment is `Default::default`
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
then {
// Get the type of the pattern
let ty = cx.typeck_results().pat_ty(local.pat);
// Ignore tuples
if let ty::Tuple(_) = ty.kind() {
return None;
}
Some((idx, ident.name, ty))
} else {
None
Expand All @@ -174,7 +168,7 @@ fn stmt_shadows_binding(this: &Stmt<'_>, shadowed: Symbol) -> bool {
}

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

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

0 comments on commit a42ed12

Please sign in to comment.