summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/suspicious_to_owned.txt
blob: 8cbf61dc7175beff26516bc6237ba917328be94f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
### What it does
Checks for the usage of `_.to_owned()`, on a `Cow<'_, _>`.

### Why is this bad?
Calling `to_owned()` on a `Cow` creates a clone of the `Cow`
itself, without taking ownership of the `Cow` contents (i.e.
it's equivalent to calling `Cow::clone`).
The similarly named `into_owned` method, on the other hand,
clones the `Cow` contents, effectively turning any `Cow::Borrowed`
into a `Cow::Owned`.

Given the potential ambiguity, consider replacing `to_owned`
with `clone` for better readability or, if getting a `Cow::Owned`
was the original intent, using `into_owned` instead.

### Example
```
let s = "Hello world!";
let cow = Cow::Borrowed(s);

let data = cow.to_owned();
assert!(matches!(data, Cow::Borrowed(_)))
```
Use instead:
```
let s = "Hello world!";
let cow = Cow::Borrowed(s);

let data = cow.clone();
assert!(matches!(data, Cow::Borrowed(_)))
```
or
```
let s = "Hello world!";
let cow = Cow::Borrowed(s);

let data = cow.into_owned();
assert!(matches!(data, String))
```