-
Notifications
You must be signed in to change notification settings - Fork 13.3k
#[warn(unused_mut)] does not always warn #25049
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
Comments
Triage: here's the smallest reproduction: pub fn foo(mut buf: &mut [u8]) {
&mut buf[..];
} |
Small update while looking to this: the borrow checker looks like it's doing a ref adjustment when the types are already compatible. Another minimal example is this:
I might be misunderstanding the borrow checking code, but it looks the adjustment means there's a second, implicit borrow, equivalent to a Working on a fix, but it's not clear if the issue is with what's being marked as used, or what the conditions are for it happening. |
Fixed mutable vars being marked used when they weren't #### NB : bootstrapping is slow on my machine, even with `keep-stage` - fixes for occurances in the current codebase are <s>in the pipeline</s> done. This PR is being put up for review of the fix of the issue. Fixes #43526, Fixes #30280, Fixes #25049 ### Issue Whenever the compiler detected a mutable deref being used mutably, it marked an associated value as being used mutably as well. In the case of derefencing local variables which were mutable references, this incorrectly marked the reference itself being used mutably, instead of its contents - with the consequence of making the following code emit no warnings ``` fn do_thing<T>(mut arg : &mut T) { ... // don't touch arg - just deref it to access the T } ``` ### Fix Make dereferences not be counted as a mutable use, but only when they're on borrows on local variables. #### Why not on things other than local variables? * Whenever you capture a variable in a closure, it gets turned into a hidden reference - when you use it in the closure, it gets dereferenced. If the closure uses the variable mutably, that is actually a mutable use of the thing being dereffed to, so it has to be counted. * If you deref a mutable `Box` to access the contents mutably, you are using the `Box` mutably - so it has to be counted.
rustc fails to warn about unused
mut
forbuf
in this case:The text was updated successfully, but these errors were encountered: