Skip to content

Commit

Permalink
Resolve minor issues based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
Henri Lunnikivi committed Aug 25, 2020
1 parent 9b3d64b commit 31c73fb
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions clippy_lints/src/field_reassign_with_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,27 @@ declare_clippy_lint! {
/// **What it does:** Checks for immediate reassignment of fields initialized
/// with Default::default().
///
/// **Why is this bad?** Fields should be set using
/// T { field: value, ..Default::default() } syntax instead of using a mutable binding.
/// **Why is this bad?**It's more idiomatic to use [functional update syntax](https://doc.rust-lang.org/reference/expressions/struct-expr.html#functional-update-syntax).
///
/// **Known problems:** The lint does not detect calls to Default::default()
/// if they are made via another struct implementing the Default trait. If type inference stops
/// requiring an explicit type for assignment using Default::default() this lint will not
/// trigger for cases where the type is elided.
/// **Known problems:** None.
///
/// **Example:**
/// Bad:
/// ```ignore
/// ```
/// # struct A { i: i32 }
/// let mut a: A = Default::default();
/// a.i = 42;
/// ```
/// Use instead:
/// ```ignore
/// ```
/// # struct A { i: i32 }
/// let a = A {
/// i: 42,
/// .. Default::default()
/// };
/// ```
pub FIELD_REASSIGN_WITH_DEFAULT,
pedantic,
style,
"binding initialized with Default should have its fields set in the initializer"
}

Expand All @@ -56,7 +54,7 @@ impl LateLintPass<'_> for FieldReassignWithDefault {
// only when assigning `... = Default::default()`
if let Some(ref expr) = local.init;
if let ExprKind::Call(ref fn_expr, _) = &expr.kind;
if let ExprKind::Path( qpath ) = &fn_expr.kind;
if let ExprKind::Path(qpath) = &fn_expr.kind;
if let Res::Def(_, def_id) = qpath_res(cx, qpath, fn_expr.hir_id);
// right hand side of assignment is `Default::default`
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
Expand Down

0 comments on commit 31c73fb

Please sign in to comment.