1 use crate::utils::{any_parent_is_automatically_derived, contains_name, match_def_path, paths, qpath_res, snippet};
2 use crate::utils::{span_lint_and_note, span_lint_and_sugg};
3 use if_chain::if_chain;
4 use rustc_data_structures::fx::FxHashSet;
5 use rustc_errors::Applicability;
6 use rustc_hir::def::Res;
7 use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
8 use rustc_lint::{LateContext, LateLintPass};
10 use rustc_session::{declare_tool_lint, impl_lint_pass};
11 use rustc_span::symbol::{Ident, Symbol};
14 declare_clippy_lint! {
15 /// **What it does:** Checks for literal calls to `Default::default()`.
17 /// **Why is this bad?** It's more clear to the reader to use the name of the type whose default is
18 /// being gotten than the generic `Default`.
20 /// **Known problems:** None.
25 /// let s: String = Default::default();
28 /// let s = String::default();
30 pub DEFAULT_TRAIT_ACCESS,
32 "checks for literal calls to `Default::default()`"
35 declare_clippy_lint! {
36 /// **What it does:** Checks for immediate reassignment of fields initialized
37 /// with Default::default().
39 /// **Why is this bad?**It's more idiomatic to use the [functional update syntax](https://doc.rust-lang.org/reference/expressions/struct-expr.html#functional-update-syntax).
41 /// **Known problems:** Assignments to patterns that are of tuple type are not linted.
46 /// # #[derive(Default)]
47 /// # struct A { i: i32 }
48 /// let mut a: A = Default::default();
53 /// # #[derive(Default)]
54 /// # struct A { i: i32 }
57 /// .. Default::default()
60 pub FIELD_REASSIGN_WITH_DEFAULT,
62 "binding initialized with Default should have its fields set in the initializer"
67 // Spans linted by `field_reassign_with_default`.
68 reassigned_linted: FxHashSet<Span>,
71 impl_lint_pass!(Default => [DEFAULT_TRAIT_ACCESS, FIELD_REASSIGN_WITH_DEFAULT]);
73 impl LateLintPass<'_> for Default {
74 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
76 // Avoid cases already linted by `field_reassign_with_default`
77 if !self.reassigned_linted.contains(&expr.span);
78 if let ExprKind::Call(ref path, ..) = expr.kind;
79 if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id);
80 if let ExprKind::Path(ref qpath) = path.kind;
81 if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
82 if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
83 // Detect and ignore <Foo as Default>::default() because these calls do explicitly name the type.
84 if let QPath::Resolved(None, _path) = qpath;
86 let expr_ty = cx.typeck_results().expr_ty(expr);
87 if let ty::Adt(def, ..) = expr_ty.kind() {
88 // TODO: Work out a way to put "whatever the imported way of referencing
89 // this type in this file" rather than a fully-qualified type.
90 let replacement = format!("{}::default()", cx.tcx.def_path_str(def.did));
95 &format!("calling `{}` is more clear than this expression", replacement),
98 Applicability::Unspecified, // First resolve the TODO above
105 fn check_block<'tcx>(&mut self, cx: &LateContext<'tcx>, block: &Block<'tcx>) {
106 // start from the `let mut _ = _::default();` and look at all the following
107 // statements, see if they re-assign the fields of the binding
108 let stmts_head = match block.stmts {
109 // Skip the last statement since there cannot possibly be any following statements that re-assign fields.
110 [head @ .., _] if !head.is_empty() => head,
113 for (stmt_idx, stmt) in stmts_head.iter().enumerate() {
114 // find all binding statements like `let mut _ = T::default()` where `T::default()` is the
115 // `default` method of the `Default` trait, and store statement index in current block being
116 // checked and the name of the bound variable
117 let (local, variant, binding_name, binding_type, span) = if_chain! {
118 // only take `let ...` statements
119 if let StmtKind::Local(local) = stmt.kind;
120 if let Some(expr) = local.init;
121 // only take bindings to identifiers
122 if let PatKind::Binding(_, binding_id, ident, _) = local.pat.kind;
123 // only when assigning `... = Default::default()`
124 if is_expr_default(expr, cx);
125 let binding_type = cx.typeck_results().node_type(binding_id);
126 if let Some(adt) = binding_type.ty_adt_def();
128 let variant = adt.non_enum_variant();
129 if adt.did.is_local() || !variant.is_field_list_non_exhaustive();
130 let module_did = cx.tcx.parent_module(stmt.hir_id).to_def_id();
134 .all(|field| field.vis.is_accessible_from(module_did, cx.tcx));
136 (local, variant, ident.name, binding_type, expr.span)
142 // find all "later statement"'s where the fields of the binding set as
143 // Default::default() get reassigned, unless the reassignment refers to the original binding
144 let mut first_assign = None;
145 let mut assigned_fields = Vec::new();
146 let mut cancel_lint = false;
147 for consecutive_statement in &block.stmts[stmt_idx + 1..] {
148 // find out if and which field was set by this `consecutive_statement`
149 if let Some((field_ident, assign_rhs)) = field_reassigned_by_stmt(consecutive_statement, binding_name) {
150 // interrupt and cancel lint if assign_rhs references the original binding
151 if contains_name(binding_name, assign_rhs) {
156 // if the field was previously assigned, replace the assignment, otherwise insert the assignment
157 if let Some(prev) = assigned_fields
159 .find(|(field_name, _)| field_name == &field_ident.name)
161 *prev = (field_ident.name, assign_rhs);
163 assigned_fields.push((field_ident.name, assign_rhs));
166 // also set first instance of error for help message
167 if first_assign.is_none() {
168 first_assign = Some(consecutive_statement);
171 // interrupt if no field was assigned, since we only want to look at consecutive statements
177 // if there are incorrectly assigned fields, do a span_lint_and_note to suggest
178 // construction using `Ty { fields, ..Default::default() }`
179 if !assigned_fields.is_empty() && !cancel_lint {
180 // if all fields of the struct are not assigned, add `.. Default::default()` to the suggestion.
181 let ext_with_default = !variant
184 .all(|field| assigned_fields.iter().any(|(a, _)| a == &field.ident.name));
186 let field_list = assigned_fields
188 .map(|(field, rhs)| {
189 // extract and store the assigned value for help message
190 let value_snippet = snippet(cx, rhs.span, "..");
191 format!("{}: {}", field, value_snippet)
193 .collect::<Vec<String>>()
196 let sugg = if ext_with_default {
197 if field_list.is_empty() {
198 format!("{}::default()", binding_type)
200 format!("{} {{ {}, ..Default::default() }}", binding_type, field_list)
203 format!("{} {{ {} }}", binding_type, field_list)
206 // span lint once per statement that binds default
209 FIELD_REASSIGN_WITH_DEFAULT,
210 first_assign.unwrap().span,
211 "field assignment outside of initializer for an instance created with Default::default()",
214 "consider initializing the variable with `{}` and removing relevant reassignments",
218 self.reassigned_linted.insert(span);
224 /// Checks if the given expression is the `default` method belonging to the `Default` trait.
225 fn is_expr_default<'tcx>(expr: &'tcx Expr<'tcx>, cx: &LateContext<'tcx>) -> bool {
227 if let ExprKind::Call(ref fn_expr, _) = &expr.kind;
228 if let ExprKind::Path(qpath) = &fn_expr.kind;
229 if let Res::Def(_, def_id) = qpath_res(cx, qpath, fn_expr.hir_id);
231 // right hand side of assignment is `Default::default`
232 match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD)
239 /// Returns the reassigned field and the assigning expression (right-hand side of assign).
240 fn field_reassigned_by_stmt<'tcx>(this: &Stmt<'tcx>, binding_name: Symbol) -> Option<(Ident, &'tcx Expr<'tcx>)> {
242 // only take assignments
243 if let StmtKind::Semi(ref later_expr) = this.kind;
244 if let ExprKind::Assign(ref assign_lhs, ref assign_rhs, _) = later_expr.kind;
245 // only take assignments to fields where the left-hand side field is a field of
246 // the same binding as the previous statement
247 if let ExprKind::Field(ref binding, field_ident) = assign_lhs.kind;
248 if let ExprKind::Path(QPath::Resolved(_, path)) = binding.kind;
249 if let Some(second_binding_name) = path.segments.last();
250 if second_binding_name.ident.name == binding_name;
252 Some((field_ident, assign_rhs))