Skip to content

Commit 5d48b91

Browse files
committed
field_reassign_with_default: don't expand macros in suggestion
fixes rust-lang#6522 changelog: field_reassign_with_default: don't expand macros in lint suggestion (rust-lang#6522)
1 parent 60919e4 commit 5d48b91

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

clippy_lints/src/default.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::utils::{any_parent_is_automatically_derived, contains_name, match_def_path, paths, qpath_res, snippet};
1+
use crate::utils::{
2+
any_parent_is_automatically_derived, contains_name, match_def_path, paths, qpath_res, snippet_with_macro_callsite,
3+
};
24
use crate::utils::{span_lint_and_note, span_lint_and_sugg};
35
use if_chain::if_chain;
46
use rustc_data_structures::fx::FxHashSet;
@@ -187,7 +189,7 @@ impl LateLintPass<'_> for Default {
187189
.into_iter()
188190
.map(|(field, rhs)| {
189191
// extract and store the assigned value for help message
190-
let value_snippet = snippet(cx, rhs.span, "..");
192+
let value_snippet = snippet_with_macro_callsite(cx, rhs.span, "..");
191193
format!("{}: {}", field, value_snippet)
192194
})
193195
.collect::<Vec<String>>()

tests/ui/field_reassign_with_default.rs

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ struct B {
1111
j: i64,
1212
}
1313

14+
#[derive(Default)]
15+
struct C {
16+
i: Vec<i32>,
17+
j: i64,
18+
}
1419
/// Implements .next() that returns a different number each time.
1520
struct SideEffect(i32);
1621

@@ -111,6 +116,10 @@ fn main() {
111116
// don't lint - some private fields
112117
let mut x = m::F::default();
113118
x.a = 1;
119+
120+
// don't expand macros in the suggestion (#6522)
121+
let mut a: C = C::default();
122+
a.i = vec![1];
114123
}
115124

116125
mod m {
+25-13
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,87 @@
11
error: field assignment outside of initializer for an instance created with Default::default()
2-
--> $DIR/field_reassign_with_default.rs:30:5
2+
--> $DIR/field_reassign_with_default.rs:35: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 `A { i: 42, ..Default::default() }` and removing relevant reassignments
9-
--> $DIR/field_reassign_with_default.rs:29:5
9+
--> $DIR/field_reassign_with_default.rs:34: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:70:5
15+
--> $DIR/field_reassign_with_default.rs:75:5
1616
|
1717
LL | a.j = 43;
1818
| ^^^^^^^^^
1919
|
2020
note: consider initializing the variable with `A { j: 43, i: 42 }` and removing relevant reassignments
21-
--> $DIR/field_reassign_with_default.rs:69:5
21+
--> $DIR/field_reassign_with_default.rs:74: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:75:5
27+
--> $DIR/field_reassign_with_default.rs:80:5
2828
|
2929
LL | a.i = 42;
3030
| ^^^^^^^^^
3131
|
3232
note: consider initializing the variable with `A { i: 42, j: 44 }` and removing relevant reassignments
33-
--> $DIR/field_reassign_with_default.rs:74:5
33+
--> $DIR/field_reassign_with_default.rs:79: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:81:5
39+
--> $DIR/field_reassign_with_default.rs:86:5
4040
|
4141
LL | a.i = 42;
4242
| ^^^^^^^^^
4343
|
4444
note: consider initializing the variable with `A { i: 42, ..Default::default() }` and removing relevant reassignments
45-
--> $DIR/field_reassign_with_default.rs:80:5
45+
--> $DIR/field_reassign_with_default.rs:85: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:91:5
51+
--> $DIR/field_reassign_with_default.rs:96:5
5252
|
5353
LL | a.i = Default::default();
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5555
|
5656
note: consider initializing the variable with `A { i: Default::default(), ..Default::default() }` and removing relevant reassignments
57-
--> $DIR/field_reassign_with_default.rs:90:5
57+
--> $DIR/field_reassign_with_default.rs:95: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:95:5
63+
--> $DIR/field_reassign_with_default.rs:100:5
6464
|
6565
LL | a.i = Default::default();
6666
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6767
|
6868
note: consider initializing the variable with `A { i: Default::default(), j: 45 }` and removing relevant reassignments
69-
--> $DIR/field_reassign_with_default.rs:94:5
69+
--> $DIR/field_reassign_with_default.rs:99:5
7070
|
7171
LL | let mut a: A = Default::default();
7272
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7373

74-
error: aborting due to 6 previous errors
74+
error: field assignment outside of initializer for an instance created with Default::default()
75+
--> $DIR/field_reassign_with_default.rs:122:5
76+
|
77+
LL | a.i = vec![1];
78+
| ^^^^^^^^^^^^^^
79+
|
80+
note: consider initializing the variable with `C { i: vec![1], ..Default::default() }` and removing relevant reassignments
81+
--> $DIR/field_reassign_with_default.rs:121:5
82+
|
83+
LL | let mut a: C = C::default();
84+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85+
86+
error: aborting due to 7 previous errors
7587

0 commit comments

Comments
 (0)