From 772996feb0f9f8ad51c689079cdb1bb64a5eed75 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 21 Aug 2022 20:25:22 -0700 Subject: [PATCH 1/3] Add regression test for issue 85 error[E0507]: cannot move out of a shared reference --> tests/test_expr.rs:269:5 | 269 | clone!(&A).consume_self(); | ^^^^^^^^^^^-------------- | | | | | value moved due to this method call | move occurs because value has type `A`, which does not implement the `Copy` trait | note: this function takes ownership of the receiver `self`, which moves value --> tests/test_expr.rs:266:25 | 266 | fn consume_self(self) {} | ^^^^ --- tests/test_expr.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_expr.rs b/tests/test_expr.rs index 3d3b977..fcb6e00 100644 --- a/tests/test_expr.rs +++ b/tests/test_expr.rs @@ -247,3 +247,24 @@ mod test_local_setter { assert_eq!(a.val, 42); } } + +// https://github.com/dtolnay/paste/issues/85 +#[test] +fn test_top_level_none_delimiter() { + macro_rules! clone { + ($val:expr) => { + paste! { + $val.clone() + } + }; + } + + #[derive(Clone)] + struct A; + + impl A { + fn consume_self(self) {} + } + + clone!(&A).consume_self(); +} From 19e73a010d2dca4371e48f0451b81fc2bd4c7e74 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 21 Aug 2022 20:26:06 -0700 Subject: [PATCH 2/3] Produce clone of input tokenstream if contains no paste --- src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 82f0815..983ba06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -164,8 +164,18 @@ use std::panic; pub fn paste(input: TokenStream) -> TokenStream { let mut contains_paste = false; let flatten_single_interpolation = true; - match expand(input, &mut contains_paste, flatten_single_interpolation) { - Ok(expanded) => expanded, + match expand( + input.clone(), + &mut contains_paste, + flatten_single_interpolation, + ) { + Ok(expanded) => { + if contains_paste { + expanded + } else { + input + } + } Err(err) => err.to_compile_error(), } } From 25960a47d24af04cfa1558e8ca83bbd969e226c7 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 31 Aug 2022 00:02:55 -0700 Subject: [PATCH 3/3] Avoid unused_self clippy pedantic lint in test error: unused `self` argument --> tests/test_expr.rs:266:25 | 266 | fn consume_self(self) {} | ^^^^ | = note: `-D clippy::unused-self` implied by `-D clippy::pedantic` = help: consider refactoring to a associated function = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_self --- tests/test_expr.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_expr.rs b/tests/test_expr.rs index fcb6e00..8ad5de9 100644 --- a/tests/test_expr.rs +++ b/tests/test_expr.rs @@ -263,7 +263,9 @@ fn test_top_level_none_delimiter() { struct A; impl A { - fn consume_self(self) {} + fn consume_self(self) { + let _ = self; + } } clone!(&A).consume_self();