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

Suggest Arc::unwrap_or_clone() when appropriate #13917

Open
cdisselkoen opened this issue Dec 31, 2024 · 2 comments · May be fixed by #13921
Open

Suggest Arc::unwrap_or_clone() when appropriate #13917

cdisselkoen opened this issue Dec 31, 2024 · 2 comments · May be fixed by #13921
Labels
A-lint Area: New lints

Comments

@cdisselkoen
Copy link

What it does

Suppose foo is an Arc<T>. The lint would suggest replacing things like (*foo).clone() with Arc::unwrap_or_clone(foo).

Advantage

As noted in the official docs on Arc::unwrap_or_clone, this is entirely equivalent except that it avoids cloning in the case this was already the last reference to the Arc.

Drawbacks

Arc::unwrap_or_clone was only stabilized in Rust 1.76, I'm not sure if Clippy avoids making suggestions that require recent Rust for some definition of "recent".

Example

let foo: Arc<String> = Arc::new("foo".into());
let bar: String = (*foo).clone();

Could be written as:

let foo: Arc<String> = Arc::new("foo".into());
let bar: String = Arc::unwrap_or_clone(foo);
@y21
Copy link
Member

y21 commented Dec 31, 2024

Arc::unwrap_or_clone was only stabilized in Rust 1.76, I'm not sure if Clippy avoids making suggestions that require recent Rust for some definition of "recent".

The MSRV for lints can be configured, so this should be fine as long as the lint respects that.

@lukaslueg
Copy link
Contributor

Same for Rc::unwrap_or_clone

@lapla-cogito lapla-cogito linked a pull request Jan 1, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants