]> git.lizzy.rs Git - rust.git/commit
Allow pass by reference if we return a reference
authorRyan Cumming <etaoins@gmail.com>
Mon, 23 Jul 2018 08:33:47 +0000 (18:33 +1000)
committerRyan Cumming <etaoins@gmail.com>
Mon, 23 Jul 2018 08:44:40 +0000 (18:44 +1000)
commit58459abd0cbc998c6c3544ec43b94d4371d8782d
tree764cfd03fab777bb3d007cbbd37b5aad0d579fbe
parent1f656173723dce4efc3fc90ff5763a2186ec9089
Allow pass by reference if we return a reference

Currently this code will trigger `trivally_copy_pass_by_ref`:

```
struct OuterStruct {
    field: [u8; 8],
}

fn return_inner(outer: &OuterStruct) -> &[u8] {
    &outer.field
}
```

If we change the `outer` to be pass-by-value it will not live long
enough for us to return the reference. The above example is trivial but
I've hit this in real code that either returns a reference to either the
argument or in to `self`.

This suppresses the `trivally_copy_pass_by_ref` lint if we return a
reference and it has the same lifetime as the argument. This will likely
miss complex cases with multiple lifetimes bounded by each other but it
should cover the majority of cases with little effort.
clippy_lints/src/trivially_copy_pass_by_ref.rs
tests/ui/trivially_copy_pass_by_ref.rs
tests/ui/trivially_copy_pass_by_ref.stderr