-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
[move compiler] Report unnecessary mutable references #14216
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
3 Ignored Deployments
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM just two small nits :)
} = info; | ||
if *is_mut && !*used_mutably { | ||
let msg = "Mutable reference is never used mutably, \ | ||
consider switching to an imutable reference '&' instead"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
consider switching to an imutable reference '&' instead"; | |
consider switching to an immutable reference '&' instead"; |
assert_eq!(exps.len(), 1); | ||
let infos: &mut BTreeMap<ExpBasedID, RefExpInfo> = | ||
&mut RefCell::borrow_mut(&self.mutably_used); | ||
for e in exps { | ||
let info = infos.get_mut(e).unwrap(); | ||
assert!(info.param_name.is_none()); | ||
info.param_name = Some(name) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Either there shouldn't be an assert here, or assert remains and the for
loop should be rewritten to just directly access the zero-th exp so something like
let info = infos.get_mut(&exps[0]).unwrap();
...
## Description - Report unused mutable references - The system is quite exhaustive and reports even locally created mutable references. It piggy-backs on the borrow checker but globally collects usages instead of keeping it in the abstract state. This is done mostly for simplicity - A mutable reference is considered unused if: - It is mutated - It is passed to a function (since freeze removes any subtyping issues) - It is returned - Copies share the same abstract reference ID ## Test Plan - Added new tests --- If your changes are not user-facing and not a breaking change, you can skip the following section. Otherwise, please indicate what changed, and then add to the Release Notes section as highlighted during the release process. ### Type of Change (Check all that apply) - [ ] protocol change - [X] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes With this change, Move will now suggest switching mutable references `&mut` to immutable ones `&` when the reference is not modified, or passed somewhere where the mutability is required. This can be suppressed for parameters by prefixing the parameter name with an underscore `_`.
Description
Test Plan
If your changes are not user-facing and not a breaking change, you can skip the following section. Otherwise, please indicate what changed, and then add to the Release Notes section as highlighted during the release process.
Type of Change (Check all that apply)
Release notes
With this change, Move will now suggest switching mutable references
&mut
to immutable ones&
when the reference is not modified, or passed somewhere where the mutability is required.This can be suppressed for parameters by prefixing the parameter name with an underscore
_
.