Skip to content

Commit a4cf91b

Browse files
committed
Auto merge of rust-lang#8433 - hellow554:update_default_trait, r=flip1995
Don't lint Default::default if it is the udpate syntax base changelog: Don't lint `Default::default` it is part of the update syntax Current clippy warns about this: ``` warning: calling `Foo::default()` is more clear than this expression --> src/main.rs:12:11 | 12 | ..Default::default() | ^^^^^^^^^^^^^^^^^^ help: try: `Foo::default()` | ``` With these changes, it will not lint that particular expression anymore.
2 parents b34cd79 + 504f3af commit a4cf91b

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

clippy_lints/src/default.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg};
22
use clippy_utils::source::snippet_with_macro_callsite;
33
use clippy_utils::ty::{has_drop, is_copy};
4-
use clippy_utils::{any_parent_is_automatically_derived, contains_name, match_def_path, paths};
4+
use clippy_utils::{any_parent_is_automatically_derived, contains_name, get_parent_expr, match_def_path, paths};
55
use if_chain::if_chain;
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_errors::Applicability;
@@ -88,6 +88,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
8888
if let ExprKind::Path(ref qpath) = path.kind;
8989
if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
9090
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
91+
if !is_update_syntax_base(cx, expr);
9192
// Detect and ignore <Foo as Default>::default() because these calls do explicitly name the type.
9293
if let QPath::Resolved(None, _path) = qpath;
9394
let expr_ty = cx.typeck_results().expr_ty(expr);
@@ -290,3 +291,16 @@ fn field_reassigned_by_stmt<'tcx>(this: &Stmt<'tcx>, binding_name: Symbol) -> Op
290291
}
291292
}
292293
}
294+
295+
/// Returns whether `expr` is the update syntax base: `Foo { a: 1, .. base }`
296+
fn is_update_syntax_base<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
297+
if_chain! {
298+
if let Some(parent) = get_parent_expr(cx, expr);
299+
if let ExprKind::Struct(_, _, Some(base)) = parent.kind;
300+
then {
301+
base.hir_id == expr.hir_id
302+
} else {
303+
false
304+
}
305+
}
306+
}

tests/ui/default_trait_access.fixed

+13-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ fn main() {
4646

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

49+
let s20 = UpdateSyntax {
50+
s: "foo",
51+
..Default::default()
52+
};
53+
4954
println!(
50-
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}], [{:?}]",
51-
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19,
55+
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}]",
56+
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,
5257
);
5358
}
5459

@@ -86,3 +91,9 @@ struct ArrayDerivedDefault {
8691

8792
#[derive(Debug, Default)]
8893
struct TupleStructDerivedDefault(String);
94+
95+
#[derive(Debug, Default)]
96+
struct UpdateSyntax {
97+
pub s: &'static str,
98+
pub u: u64,
99+
}

tests/ui/default_trait_access.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ fn main() {
4646

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

49+
let s20 = UpdateSyntax {
50+
s: "foo",
51+
..Default::default()
52+
};
53+
4954
println!(
50-
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}], [{:?}]",
51-
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19,
55+
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}]",
56+
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,
5257
);
5358
}
5459

@@ -86,3 +91,9 @@ struct ArrayDerivedDefault {
8691

8792
#[derive(Debug, Default)]
8893
struct TupleStructDerivedDefault(String);
94+
95+
#[derive(Debug, Default)]
96+
struct UpdateSyntax {
97+
pub s: &'static str,
98+
pub u: u64,
99+
}

0 commit comments

Comments
 (0)