Skip to content

Suggest .clone() when we have &T, T was expected and T: Clone #106443

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

Closed
estebank opened this issue Jan 4, 2023 · 2 comments · Fixed by #106497
Closed

Suggest .clone() when we have &T, T was expected and T: Clone #106443

estebank opened this issue Jan 4, 2023 · 2 comments · Fixed by #106497
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jan 4, 2023

Given

#[derive(Clone)]
struct S;
fn foo(_: S) {}
fn main() {
    let s = &S;
    foo(s);
}

we should suggest cloning.

Also, given

#[derive(Clone)]
struct S;
trait X {}
impl X for S {}
fn foo<T: X>(_: T) {}
fn bar<T: X>(s: &T) {
    foo(s);
}
fn main() {}

we should suggest

fn bar<T: X + Clone>(s: &T) {
    foo(s.clone());
}
@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Jan 4, 2023
@chenyukang
Copy link
Member

@rustbot claim

@chenyukang
Copy link
Member

If we suggest add Clone bound, we don't need struct S dervie Clone,
This code will compile:

struct S;
trait X {}
impl X for S {}
fn foo<T: X>(_: T) {}
fn bar<T: X + Clone>(s: &T) {
    foo(s.clone());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants