Skip to content

Commit 26fdd3f

Browse files
authored
Merge pull request #1102 from Manishearth/fix-1100
Fix position of mut in toplevel-ref-arg (fixes #1100, again)
2 parents 6a20d6a + b8c5e5a commit 26fdd3f

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

clippy_lints/src/misc.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,28 @@ impl LateLintPass for TopLevelRefPass {
6262
let PatKind::Binding(BindByRef(mt), i, None) = l.pat.node,
6363
let Some(ref init) = l.init
6464
], {
65-
let tyopt = if let Some(ref ty) = l.ty {
66-
format!(": &{}", snippet(cx, ty.span, "_"))
65+
let init = Sugg::hir(cx, init, "..");
66+
let (mutopt,initref) = if mt == Mutability::MutMutable {
67+
("mut ", init.mut_addr())
6768
} else {
68-
"".to_owned()
69+
("", init.addr())
6970
};
70-
let mutopt = if mt == Mutability::MutMutable {
71-
"mut "
71+
let tyopt = if let Some(ref ty) = l.ty {
72+
format!(": &{mutopt}{ty}", mutopt=mutopt, ty=snippet(cx, ty.span, "_"))
7273
} else {
73-
""
74+
"".to_owned()
7475
};
7576
span_lint_and_then(cx,
7677
TOPLEVEL_REF_ARG,
7778
l.pat.span,
7879
"`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead",
7980
|db| {
80-
let init = Sugg::hir(cx, init, "..");
8181
db.span_suggestion(s.span,
8282
"try",
83-
format!("let {}{}{} = {};",
84-
mutopt,
85-
snippet(cx, i.span, "_"),
86-
tyopt,
87-
init.addr()));
83+
format!("let {name}{tyopt} = {initref};",
84+
name=snippet(cx, i.span, "_"),
85+
tyopt=tyopt,
86+
initref=initref));
8887
}
8988
);
9089
}}

tests/compile-fail/toplevel_ref_arg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn main() {
3333
let ref mut z = 1 + 2;
3434
//~^ ERROR `ref` on an entire `let` pattern is discouraged
3535
//~| HELP try
36-
//~| SUGGESTION let mut z = &(1 + 2);
36+
//~| SUGGESTION let z = &mut (1 + 2);
3737

3838
let (ref x, _) = (1,2); // okay, not top level
3939
println!("The answer is {}.", x);

0 commit comments

Comments
 (0)