-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add new lint: cloned_ref_to_slice_refs
#14284
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @blyxyas (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
cloned_refs_to_slice_refs
cloned_refs_to_slice_refs
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.
Thanks for your PR. Here is a first cursory review.
I'll have to reroll this one, I'm having capacity problems right now. r? clippy |
cloned_refs_to_slice_refs
cloned_ref_to_slice_refs
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.
The following code will trigger a false positive, as the suggested fix would take a reference on x
which would be alive when x
is pushed to:
fn main() {
let mut x = String::from("Hello");
let r = &[x.clone()];
x.push('!');
println!("r = `{}', x = `{x}'", r[0]);
}
The lint should trigger only when the receiver of .clone()
is immutable, as it is reasonable to assume that .clone()
would build an identical copy from a practical point of view.
Also, the commits should be (squashed and) rebased onto master
, as some changes in the MSRV handling have happened since and the code won't compile as-is.
This comment has been minimized.
This comment has been minimized.
8b075f9
to
5f67d09
Compare
This comment has been minimized.
This comment has been minimized.
06eeaf5
to
e5163a3
Compare
Can you please squash your commits? Also I think there are utility functions to detect interior mutability, which would allow us to remove this particular false-positive. But I'm ok with doing that in a followup PR. |
remove merge removed false positive and improved tests clarify known problems for `cloned_ref_to_slice_refs`
e5163a3
to
40e1b0e
Compare
I'm unsure if the lint should even differenciate between clones that have interior mutability. These two do the same thing. fn increment(list: &[Arc<Mutex<i32>>]) {
list.iter().for_each(|i| *i.lock().unwrap() += 1);
}
{
let x = Arc::new(Mutex::new(1));
let list = std::slice::from_ref(&x);
increment(list);
}
{
let x = Arc::new(Mutex::new(1));
let list = &[x.clone()]; // same thing but with reference counting overhead
increment(list);
} |
I am completely unconcerned about |
That said, let's merge this, we can do interior mutability detection in a follow-up PR. |
Added lint for catching
&[foo.clone()]
where foo is a reference and suggestsstd::slice::from_ref(foo)
.changelog: new lint: [
cloned_ref_to_slice_refs
]