From f19488064a4752703fde8a3596a9d90ca2095534 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Tue, 13 Dec 2022 18:55:00 -0800 Subject: Suggest constraining type parameter with `Clone` Fix #34896. --- ...lone-on-unconstrained-borrowed-type-param.fixed | 8 +++++++ .../clone-on-unconstrained-borrowed-type-param.rs | 8 +++++++ ...one-on-unconstrained-borrowed-type-param.stderr | 25 ++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed create mode 100644 src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs create mode 100644 src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr (limited to 'src/test') diff --git a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed new file mode 100644 index 00000000000..3e5ded6738b --- /dev/null +++ b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed @@ -0,0 +1,8 @@ +// run-rustfix +fn wat(t: &T) -> T { + t.clone() //~ ERROR E0308 +} + +fn main() { + wat(&42); +} diff --git a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs new file mode 100644 index 00000000000..1a5a38369ec --- /dev/null +++ b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs @@ -0,0 +1,8 @@ +// run-rustfix +fn wat(t: &T) -> T { + t.clone() //~ ERROR E0308 +} + +fn main() { + wat(&42); +} diff --git a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr new file mode 100644 index 00000000000..01246955fae --- /dev/null +++ b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr @@ -0,0 +1,25 @@ +error[E0308]: mismatched types + --> $DIR/clone-on-unconstrained-borrowed-type-param.rs:3:5 + | +LL | fn wat(t: &T) -> T { + | - - expected `T` because of return type + | | + | this type parameter +LL | t.clone() + | ^^^^^^^^^ expected type parameter `T`, found `&T` + | + = note: expected type parameter `T` + found reference `&T` +note: `T` does not implement `Clone`, so `&T` was cloned instead + --> $DIR/clone-on-unconstrained-borrowed-type-param.rs:3:5 + | +LL | t.clone() + | ^ +help: consider restricting type parameter `T` + | +LL | fn wat(t: &T) -> T { + | +++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. -- cgit 1.4.1-3-g733a5