]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/wrong_self_convention.txt
Rollup merge of #101826 - andrewpollack:fix-joined-without-noop, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / src / docs / wrong_self_convention.txt
1 ### What it does
2 Checks for methods with certain name prefixes and which
3 doesn't match how self is taken. The actual rules are:
4
5 |Prefix |Postfix     |`self` taken                   | `self` type  |
6 |-------|------------|-------------------------------|--------------|
7 |`as_`  | none       |`&self` or `&mut self`         | any          |
8 |`from_`| none       | none                          | any          |
9 |`into_`| none       |`self`                         | any          |
10 |`is_`  | none       |`&mut self` or `&self` or none | any          |
11 |`to_`  | `_mut`     |`&mut self`                    | any          |
12 |`to_`  | not `_mut` |`self`                         | `Copy`       |
13 |`to_`  | not `_mut` |`&self`                        | not `Copy`   |
14
15 Note: Clippy doesn't trigger methods with `to_` prefix in:
16 - Traits definition.
17 Clippy can not tell if a type that implements a trait is `Copy` or not.
18 - Traits implementation, when `&self` is taken.
19 The method signature is controlled by the trait and often `&self` is required for all types that implement the trait
20 (see e.g. the `std::string::ToString` trait).
21
22 Clippy allows `Pin<&Self>` and `Pin<&mut Self>` if `&self` and `&mut self` is required.
23
24 Please find more info here:
25 https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv
26
27 ### Why is this bad?
28 Consistency breeds readability. If you follow the
29 conventions, your users won't be surprised that they, e.g., need to supply a
30 mutable reference to a `as_..` function.
31
32 ### Example
33 ```
34 impl X {
35     fn as_str(self) -> &'static str {
36         // ..
37     }
38 }
39 ```