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

Extra tests for move and autoderef #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,36 @@ mod tests {
};
assert_eq!(x, 3);
}

#[test]
fn magic_deref() {
fn f(y: &'static str) -> (&'static str, bool) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified:

#[test]
fn magic_deref() {
    struct Wrapped<T>(T);
    let s = "hello";
    if_chain! {
        then { Wrapped(s) }
        else { Wrapped(&s) }
    };
}

let b: [&str; 1] = ["a"];
if_chain! {
if false;
if let Some(x) = b.get(1);
then { (x, true) }
else { (y, false) }
}
}
f("foo");
}

#[test]
fn no_move() {
struct NoCopy;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This as well:

#[test]
fn no_move() {
    struct NoCopy;
    let x = NoCopy;
    if_chain! {
        then { drop(x); }
        else { drop(x); }
    }
}

let new_record = NoCopy;
let mut store = None;
let returned = if_chain!{
if true;
then {
store = Some(new_record);
None
} else {
Some(new_record)
}
};
drop(returned);
drop(store);
}
}