Skip to content

Commit f1f5ccd

Browse files
committed
Auto merge of #7160 - flip1995:field_reassign_macros, r=xFrednet,camsteffen
Don't trigger `field_reassign_with_default` in macros Fixes #7155 Producing a good suggestion for this lint is already hard when no macros are involved. With macros the lint message and the suggestion are just confusing. Since both, producing a good suggestion and figuring out if this pattern can be re-written inside a macro is nearly impossible, just bail out. changelog: [`field_reassign_with_default`] No longer triggers in macros --- No that our reviewing queue is under control, I want to start hacking on Clippy myself again. Starting with an easy issue to get back in :)
2 parents f7d09b4 + 0854f0c commit f1f5ccd

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

clippy_lints/src/default.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_errors::Applicability;
77
use rustc_hir::def::Res;
88
use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
99
use rustc_lint::{LateContext, LateLintPass};
10-
use rustc_middle::lint::in_external_macro;
1110
use rustc_middle::ty;
1211
use rustc_session::{declare_tool_lint, impl_lint_pass};
1312
use rustc_span::symbol::{Ident, Symbol};
@@ -122,7 +121,7 @@ impl LateLintPass<'_> for Default {
122121
if let StmtKind::Local(local) = stmt.kind;
123122
if let Some(expr) = local.init;
124123
if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id);
125-
if !in_external_macro(cx.tcx.sess, expr.span);
124+
if !in_macro(expr.span);
126125
// only take bindings to identifiers
127126
if let PatKind::Binding(_, binding_id, ident, _) = local.pat.kind;
128127
// only when assigning `... = Default::default()`

tests/ui/field_reassign_with_default.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ struct C {
2929
i: Vec<i32>,
3030
j: i64,
3131
}
32+
33+
#[derive(Default)]
34+
struct D {
35+
a: Option<i32>,
36+
b: Option<i32>,
37+
}
38+
39+
macro_rules! m {
40+
($key:ident: $value:tt) => {{
41+
let mut data = $crate::D::default();
42+
data.$key = Some($value);
43+
data
44+
}};
45+
}
46+
3247
/// Implements .next() that returns a different number each time.
3348
struct SideEffect(i32);
3449

@@ -143,6 +158,11 @@ fn main() {
143158

144159
let mut a: WrapperMulti<i32, i64> = Default::default();
145160
a.i = 42;
161+
162+
// Don't lint in macros
163+
m! {
164+
a: 42
165+
};
146166
}
147167

148168
mod m {

tests/ui/field_reassign_with_default.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,108 @@
11
error: field assignment outside of initializer for an instance created with Default::default()
2-
--> $DIR/field_reassign_with_default.rs:48:5
2+
--> $DIR/field_reassign_with_default.rs:63:5
33
|
44
LL | a.i = 42;
55
| ^^^^^^^^^
66
|
77
= note: `-D clippy::field-reassign-with-default` implied by `-D warnings`
88
note: consider initializing the variable with `main::A { i: 42, ..Default::default() }` and removing relevant reassignments
9-
--> $DIR/field_reassign_with_default.rs:47:5
9+
--> $DIR/field_reassign_with_default.rs:62:5
1010
|
1111
LL | let mut a: A = Default::default();
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

1414
error: field assignment outside of initializer for an instance created with Default::default()
15-
--> $DIR/field_reassign_with_default.rs:88:5
15+
--> $DIR/field_reassign_with_default.rs:103:5
1616
|
1717
LL | a.j = 43;
1818
| ^^^^^^^^^
1919
|
2020
note: consider initializing the variable with `main::A { j: 43, i: 42 }` and removing relevant reassignments
21-
--> $DIR/field_reassign_with_default.rs:87:5
21+
--> $DIR/field_reassign_with_default.rs:102:5
2222
|
2323
LL | let mut a: A = Default::default();
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525

2626
error: field assignment outside of initializer for an instance created with Default::default()
27-
--> $DIR/field_reassign_with_default.rs:93:5
27+
--> $DIR/field_reassign_with_default.rs:108:5
2828
|
2929
LL | a.i = 42;
3030
| ^^^^^^^^^
3131
|
3232
note: consider initializing the variable with `main::A { i: 42, j: 44 }` and removing relevant reassignments
33-
--> $DIR/field_reassign_with_default.rs:92:5
33+
--> $DIR/field_reassign_with_default.rs:107:5
3434
|
3535
LL | let mut a: A = Default::default();
3636
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3737

3838
error: field assignment outside of initializer for an instance created with Default::default()
39-
--> $DIR/field_reassign_with_default.rs:99:5
39+
--> $DIR/field_reassign_with_default.rs:114:5
4040
|
4141
LL | a.i = 42;
4242
| ^^^^^^^^^
4343
|
4444
note: consider initializing the variable with `main::A { i: 42, ..Default::default() }` and removing relevant reassignments
45-
--> $DIR/field_reassign_with_default.rs:98:5
45+
--> $DIR/field_reassign_with_default.rs:113:5
4646
|
4747
LL | let mut a = A::default();
4848
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4949

5050
error: field assignment outside of initializer for an instance created with Default::default()
51-
--> $DIR/field_reassign_with_default.rs:109:5
51+
--> $DIR/field_reassign_with_default.rs:124:5
5252
|
5353
LL | a.i = Default::default();
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5555
|
5656
note: consider initializing the variable with `main::A { i: Default::default(), ..Default::default() }` and removing relevant reassignments
57-
--> $DIR/field_reassign_with_default.rs:108:5
57+
--> $DIR/field_reassign_with_default.rs:123:5
5858
|
5959
LL | let mut a: A = Default::default();
6060
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6161

6262
error: field assignment outside of initializer for an instance created with Default::default()
63-
--> $DIR/field_reassign_with_default.rs:113:5
63+
--> $DIR/field_reassign_with_default.rs:128:5
6464
|
6565
LL | a.i = Default::default();
6666
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6767
|
6868
note: consider initializing the variable with `main::A { i: Default::default(), j: 45 }` and removing relevant reassignments
69-
--> $DIR/field_reassign_with_default.rs:112:5
69+
--> $DIR/field_reassign_with_default.rs:127:5
7070
|
7171
LL | let mut a: A = Default::default();
7272
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7373

7474
error: field assignment outside of initializer for an instance created with Default::default()
75-
--> $DIR/field_reassign_with_default.rs:135:5
75+
--> $DIR/field_reassign_with_default.rs:150:5
7676
|
7777
LL | a.i = vec![1];
7878
| ^^^^^^^^^^^^^^
7979
|
8080
note: consider initializing the variable with `C { i: vec![1], ..Default::default() }` and removing relevant reassignments
81-
--> $DIR/field_reassign_with_default.rs:134:5
81+
--> $DIR/field_reassign_with_default.rs:149:5
8282
|
8383
LL | let mut a: C = C::default();
8484
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8585

8686
error: field assignment outside of initializer for an instance created with Default::default()
87-
--> $DIR/field_reassign_with_default.rs:142:5
87+
--> $DIR/field_reassign_with_default.rs:157:5
8888
|
8989
LL | a.i = true;
9090
| ^^^^^^^^^^^
9191
|
9292
note: consider initializing the variable with `Wrapper::<bool> { i: true }` and removing relevant reassignments
93-
--> $DIR/field_reassign_with_default.rs:141:5
93+
--> $DIR/field_reassign_with_default.rs:156:5
9494
|
9595
LL | let mut a: Wrapper<bool> = Default::default();
9696
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9797

9898
error: field assignment outside of initializer for an instance created with Default::default()
99-
--> $DIR/field_reassign_with_default.rs:145:5
99+
--> $DIR/field_reassign_with_default.rs:160:5
100100
|
101101
LL | a.i = 42;
102102
| ^^^^^^^^^
103103
|
104104
note: consider initializing the variable with `WrapperMulti::<i32, i64> { i: 42, ..Default::default() }` and removing relevant reassignments
105-
--> $DIR/field_reassign_with_default.rs:144:5
105+
--> $DIR/field_reassign_with_default.rs:159:5
106106
|
107107
LL | let mut a: WrapperMulti<i32, i64> = Default::default();
108108
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)