libcnb.rs icon indicating copy to clipboard operation
libcnb.rs copied to clipboard

`assert_contains!` takes ownership of passed variable

Open edmorley opened this issue 2 years ago • 1 comments

If I use assert_eq!, this works:

                    let response = ureq::get(&url).call().unwrap();
                    assert_eq!(response.into_string().unwrap(), "Expected response body");

However if I instead use assert_contains!:

                    let response = ureq::get(&url).call().unwrap();
                    assert_contains!(response.into_string().unwrap(), "Expected response body");

...I get this error:

error[E0382]: use of moved value: `response`
   --> examples/basics/tests/integration_test.rs:33:38
    |
31  |                     let response = ureq::get(&url).call().unwrap();
    |                         -------- move occurs because `response` has type `Response`, which does not implement the `Copy` trait
32  |                     // let body = response.into_string().unwrap();
33  |                     assert_contains!(response.into_string().unwrap(), "Expected response body");
    |                                      ^^^^^^^^ ------------- `response` moved due to this method call
    |                                      |
    |                                      value used here after move
    |
note: this function takes ownership of the receiver `self`, which moves `response`
   --> /Users/emorley/.cargo/registry/src/github.com-1ecc6299db9ec823/ureq-2.5.0/src/response.rs:380:24
    |
380 |     pub fn into_string(self) -> io::Result<String> {
    |                        ^^^^

error[E0382]: use of moved value: `response`
  --> examples/basics/tests/integration_test.rs:33:38
   |
31 |                     let response = ureq::get(&url).call().unwrap();
   |                         -------- move occurs because `response` has type `Response`, which does not implement the `Copy` trait
32 |                     // let body = response.into_string().unwrap();
33 |                     assert_contains!(response.into_string().unwrap(), "Expected response body");
   |                                      ^^^^^^^^ ------------- `response` moved due to this method call
   |                                      |
   |                                      value used here after move

For more information about this error, try `rustc --explain E0382`.

This forces me to have to write the assertion with an intermediate variable instead:

                    let response = ureq::get(&url).call().unwrap();
                    let body = response.into_string().unwrap();
                    assert_contains!(body, "Expected response body");

It would be great if assert_contains! could be adjusted so this workaround was not necessary.

edmorley avatar Jul 21 '22 11:07 edmorley

xref #483

edmorley avatar Feb 14 '24 11:02 edmorley