-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
declare_interior_mutable_const for Option #5050
Comments
When I try to compile pub trait SomeTrait {
const VAL: Option<Self>;
} I get an compilation error that this isn't Can you provide a minimal playground example, that produces this lint message? |
Here's my attempt at a SSCCE: Playground |
@flip1995 There you go: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9dfdb8ddad2227c8ca6a48235bb23630 (I also removed the What I simply don't understand is why this lint fires Adding any trait bounds like |
I guess it is because it could be interior mutable, when you implement this trait for a struct with a But I agree, that this is a little bit too conservative. We should special case |
I'm not sure about this change. Will warnings continue to be fired if you impl the trait with something unsafe? Also, can't the trait in the example just be changed to
the way the Default trait does it? |
I actually ran into a problem this would have warned for the other day. I had a global array of atomic booleans and marked const instead of static, so the changes were never seen (because each change update a different copy of the array). const KILL_SWITCHES: &[AtomicBool] = [AtomicBool::new(false)]; |
I found a related case at #3962 (comment). In that case, a trait constant, whose type is an associated type, gets linted due to the same reason. However, both of cases have existed in the test since the first PR: rust-clippy/tests/ui/non_copy_const.rs Lines 36 to 63 in 656b26e
And the comments on it imply these behaviours were the author's conscious decision, ...which appears to turn out too restrictive. Besides |
…p1995 Change the criteria of `interior_mutable_const` This implements my suggestion [here](#5050 (comment)), and so hopefully fixes #5050. * stop linting associated types and generic type parameters * start linting ones in trait impls whose corresponding definitions in the traits are generic * remove the `is_copy` check as presumably the only purpose of it is to allow generics with `Copy` bounds as `Freeze` is internal and generics are no longer linted * remove the term 'copy' from the tests as being `Copy` no longer have meaning --- changelog: Change the criteria of `declare_interior_mutable_const` and `borrow_interior_mutable_const` to narrow the lints to only lint things that defenitly is a interior mutable type, not potentially.
Hello!
This trait
triggers
which is this lint: https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
Apparently the corresponding test is this? https://github.com/rust-lang/rust-clippy/blob/master/tests/ui/non_copy_const.rs (not sure!)
Thing is, it doesnt test
Option<T>
and additionally: is interior mutability for const Options even such a big deal?I'm simply trying to figure out if I'm implementing a foot-gun right now if I
allow
this. I don't intent to ever manipulate this const and will only use it forPartialEq
andEq
purposes.EDIT: @frozolotl on Discord suggested removing
Option
from the trait and it should indeed still trigger. And it does. Note: The suggestion to require Copy is not possible in my case.The text was updated successfully, but these errors were encountered: