You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A lint for taking a mutable reference to a constant #829
This is currently legal and prints 0 1 because a and ARR[0] are actually references to temporaries and not to C and ARR, but this is confusing as hell.
I think the let a = &mut C case should not be caught by linting against taking a mutable reference to a constant, but by improving the unused_assignments lint. This would also catch ARR[0] = 4. But I fear we need to do this analysis in MIR.
As an intermediate step, simply linting against having a constant on the lhs of an assignment op or taking a mutable reference to it would be good.
@oli-obk
With &mut 1 or &mut (a + b) it's not so bad because it's fairly evident, that the reference is bound to a temporary, in case of &mut C or ARR[0] it looks completely like taking a reference to a lvalue.
Activity
mcarton commentedon Apr 2, 2016
😕 Rust
oli-obk commentedon Apr 3, 2016
haha great catch!
oli-obk commentedon Apr 3, 2016
*&mut 1 = 5;is legal, too xDI think the
let a = &mut Ccase should not be caught by linting against taking a mutable reference to a constant, but by improving theunused_assignmentslint. This would also catchARR[0] = 4. But I fear we need to do this analysis in MIR.As an intermediate step, simply linting against having a constant on the lhs of an assignment op or taking a mutable reference to it would be good.
petrochenkov commentedon Apr 3, 2016
@oli-obk
With
&mut 1or&mut (a + b)it's not so bad because it's fairly evident, that the reference is bound to a temporary, in case of&mut CorARR[0]it looks completely like taking a reference to a lvalue.tgnottingham commentedon Jul 19, 2020
This is probably obvious, but just want to point out that this applies to calling methods that take
&mut self. Silly example:Also, I think that this would be more appropriate as a compiler warning or error, FWIW.
camsteffen commentedon Dec 16, 2020
This is resolved by the new rustc lint 👆