Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A lint for taking a mutable reference to a constant #829

Closed
petrochenkov opened this issue Apr 2, 2016 · 6 comments
Closed

A lint for taking a mutable reference to a constant #829

petrochenkov opened this issue Apr 2, 2016 · 6 comments
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience.

Comments

@petrochenkov
Copy link
Contributor

i.e. rust-lang/rfcs#885

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.

const C: u8 = 0;
const ARR: [u8; 3] = [1, 2, 3];

fn main() {
    let a = &mut C;
    *a = 1;
    ARR[0] = 4;
    println!("{} {}", C, ARR[0]);
}
@mcarton
Copy link
Member

mcarton commented Apr 2, 2016

😕 Rust

@mcarton mcarton added good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. and removed good-first-issue These issues are a good way to get started with Clippy labels Apr 2, 2016
@oli-obk
Copy link
Contributor

oli-obk commented Apr 3, 2016

haha great catch!

@oli-obk
Copy link
Contributor

oli-obk commented Apr 3, 2016

*&mut 1 = 5; is legal, too xD

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.

@petrochenkov
Copy link
Contributor Author

@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.

@tgnottingham
Copy link

tgnottingham commented 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:

const VEC: Vec<i32> = Vec::new();                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                            
fn main() {                                                                                                                                                                                                                                                                                 
    VEC.push(1);                                                                                                                                                                                                                                                                            
}

Also, I think that this would be more appropriate as a compiler warning or error, FWIW.

@camsteffen
Copy link
Contributor

This is resolved by the new rustc lint 👆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience.
Projects
None yet
Development

No branches or pull requests

5 participants