summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/wrong_self_convention.txt
blob: d6b69ab87f862d8335e8e81e91ebc592dc5d6fc2 (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 methods with certain name prefixes and which
doesn't match how self is taken. The actual rules are:

|Prefix |Postfix     |`self` taken                   | `self` type  |
|-------|------------|-------------------------------|--------------|
|`as_`  | none       |`&self` or `&mut self`         | any          |
|`from_`| none       | none                          | any          |
|`into_`| none       |`self`                         | any          |
|`is_`  | none       |`&mut self` or `&self` or none | any          |
|`to_`  | `_mut`     |`&mut self`                    | any          |
|`to_`  | not `_mut` |`self`                         | `Copy`       |
|`to_`  | not `_mut` |`&self`                        | not `Copy`   |

Note: Clippy doesn't trigger methods with `to_` prefix in:
- Traits definition.
Clippy can not tell if a type that implements a trait is `Copy` or not.
- Traits implementation, when `&self` is taken.
The method signature is controlled by the trait and often `&self` is required for all types that implement the trait
(see e.g. the `std::string::ToString` trait).

Clippy allows `Pin<&Self>` and `Pin<&mut Self>` if `&self` and `&mut self` is required.

Please find more info here:
https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv

### Why is this bad?
Consistency breeds readability. If you follow the
conventions, your users won't be surprised that they, e.g., need to supply a
mutable reference to a `as_..` function.

### Example
```
impl X {
    fn as_str(self) -> &'static str {
        // ..
    }
}
```