Skip to content

Commit

Permalink
Don't lint Default::default if it is the udpate syntax base
Browse files Browse the repository at this point in the history
An Update Syntax looks like this:

   Foo {
      a: 3,
      ..Default::default()
    }

Don't lint `Default::default` here
  • Loading branch information
hellow554 committed Feb 14, 2022
1 parent 4931cab commit 0623806
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
16 changes: 15 additions & 1 deletion clippy_lints/src/default.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg};
use clippy_utils::source::snippet_with_macro_callsite;
use clippy_utils::ty::{has_drop, is_copy};
use clippy_utils::{any_parent_is_automatically_derived, contains_name, match_def_path, paths};
use clippy_utils::{any_parent_is_automatically_derived, contains_name, get_parent_expr, match_def_path, paths};
use if_chain::if_chain;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -81,6 +81,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if_chain! {
if !expr.span.from_expansion();
if !is_update_syntax_base(cx, expr);
// Avoid cases already linted by `field_reassign_with_default`
if !self.reassigned_linted.contains(&expr.span);
if let ExprKind::Call(path, ..) = expr.kind;
Expand Down Expand Up @@ -290,3 +291,16 @@ fn field_reassigned_by_stmt<'tcx>(this: &Stmt<'tcx>, binding_name: Symbol) -> Op
}
}
}

/// Returns whether `expr` is the update syntax base: `Foo { a: 1, .. base }`
fn is_update_syntax_base<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
if_chain! {
if let Some(parent) = get_parent_expr(cx, expr);
if let ExprKind::Struct(_, _, Some(base)) = parent.kind;
then {
base.hir_id == expr.hir_id
} else {
false
}
}
}
15 changes: 13 additions & 2 deletions tests/ui/default_trait_access.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ fn main() {

let s19 = <DerivedDefault as Default>::default();

let s20 = UpdateSyntax {
s: "foo",
..Default::default()
};

println!(
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}], [{:?}]",
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19,
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}]",
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,
);
}

Expand Down Expand Up @@ -86,3 +91,9 @@ struct ArrayDerivedDefault {

#[derive(Debug, Default)]
struct TupleStructDerivedDefault(String);

#[derive(Debug, Default)]
struct UpdateSyntax {
pub s: &'static str,
pub u: u64,
}
15 changes: 13 additions & 2 deletions tests/ui/default_trait_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ fn main() {

let s19 = <DerivedDefault as Default>::default();

let s20 = UpdateSyntax {
s: "foo",
..Default::default()
};

println!(
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}], [{:?}]",
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19,
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}]",
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,
);
}

Expand Down Expand Up @@ -86,3 +91,9 @@ struct ArrayDerivedDefault {

#[derive(Debug, Default)]
struct TupleStructDerivedDefault(String);

#[derive(Debug, Default)]
struct UpdateSyntax {
pub s: &'static str,
pub u: u64,
}

0 comments on commit 0623806

Please sign in to comment.