diff options
Diffstat (limited to 'docs/dev')
-rw-r--r-- | docs/dev/README.md | 7 | ||||
-rw-r--r-- | docs/dev/lsp-extensions.md | 10 | ||||
-rw-r--r-- | docs/dev/style.md | 44 |
3 files changed, 60 insertions, 1 deletions
diff --git a/docs/dev/README.md b/docs/dev/README.md index 36edddc70..90e74f226 100644 --- a/docs/dev/README.md +++ b/docs/dev/README.md | |||
@@ -35,7 +35,12 @@ https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0 | |||
35 | * [E-easy](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-easy), | 35 | * [E-easy](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-easy), |
36 | [E-medium](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-medium), | 36 | [E-medium](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-medium), |
37 | [E-hard](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-hard), | 37 | [E-hard](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-hard), |
38 | labels are *estimates* for how hard would be to write a fix. | 38 | [E-unknown](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-unknown), |
39 | labels are *estimates* for how hard would be to write a fix. Each triaged issue should have one of these labels. | ||
40 | * [S-actionable](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AS-actionable) and | ||
41 | [S-unactionable](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AS-unactionable) | ||
42 | specify if there are concrete steps to resolve or advance an issue. Roughly, actionable issues need only work to be fixed, | ||
43 | while unactionable ones are effectively wont-fix. Each triaged issue should have one of these labels. | ||
39 | * [fun](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3Afun) | 44 | * [fun](https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3Afun) |
40 | is for cool, but probably hard stuff. | 45 | is for cool, but probably hard stuff. |
41 | 46 | ||
diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 3f861f3e0..43a69d6ce 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md | |||
@@ -1,3 +1,13 @@ | |||
1 | <!--- | ||
2 | lsp_ext.rs hash: 286f8bbac885531a | ||
3 | |||
4 | If you need to change the above hash to make the test pass, please check if you | ||
5 | need to adjust this doc as well and ping this issue: | ||
6 | |||
7 | https://github.com/rust-analyzer/rust-analyzer/issues/4604 | ||
8 | |||
9 | ---> | ||
10 | |||
1 | # LSP Extensions | 11 | # LSP Extensions |
2 | 12 | ||
3 | This document describes LSP extensions used by rust-analyzer. | 13 | This document describes LSP extensions used by rust-analyzer. |
diff --git a/docs/dev/style.md b/docs/dev/style.md index 59067d234..883a6845d 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md | |||
@@ -186,6 +186,31 @@ impl Person { | |||
186 | } | 186 | } |
187 | ``` | 187 | ``` |
188 | 188 | ||
189 | ## Constructors | ||
190 | |||
191 | Prefer `Default` to zero-argument `new` function | ||
192 | |||
193 | ```rust | ||
194 | // Good | ||
195 | #[derive(Default)] | ||
196 | struct Foo { | ||
197 | bar: Option<Bar> | ||
198 | } | ||
199 | |||
200 | // Not as good | ||
201 | struct Foo { | ||
202 | bar: Option<Bar> | ||
203 | } | ||
204 | |||
205 | impl Foo { | ||
206 | fn new() -> Foo { | ||
207 | Foo { bar: None } | ||
208 | } | ||
209 | } | ||
210 | ``` | ||
211 | |||
212 | Prefer `Default` even it has to be implemented manually. | ||
213 | |||
189 | ## Avoid Monomorphization | 214 | ## Avoid Monomorphization |
190 | 215 | ||
191 | Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*. | 216 | Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*. |
@@ -223,6 +248,8 @@ fn frbonicate(f: impl AsRef<Path>) { | |||
223 | 248 | ||
224 | # Premature Pessimization | 249 | # Premature Pessimization |
225 | 250 | ||
251 | ## Avoid Allocations | ||
252 | |||
226 | Avoid writing code which is slower than it needs to be. | 253 | Avoid writing code which is slower than it needs to be. |
227 | Don't allocate a `Vec` where an iterator would do, don't allocate strings needlessly. | 254 | Don't allocate a `Vec` where an iterator would do, don't allocate strings needlessly. |
228 | 255 | ||
@@ -242,6 +269,8 @@ if words.len() != 2 { | |||
242 | } | 269 | } |
243 | ``` | 270 | ``` |
244 | 271 | ||
272 | ## Push Allocations to the Call Site | ||
273 | |||
245 | If allocation is inevitable, let the caller allocate the resource: | 274 | If allocation is inevitable, let the caller allocate the resource: |
246 | 275 | ||
247 | ```rust | 276 | ```rust |
@@ -257,6 +286,9 @@ fn frobnicate(s: &str) { | |||
257 | } | 286 | } |
258 | ``` | 287 | ``` |
259 | 288 | ||
289 | This is better because it reveals the costs. | ||
290 | It is also more efficient when the caller already owns the allocation. | ||
291 | |||
260 | ## Collection types | 292 | ## Collection types |
261 | 293 | ||
262 | Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`. | 294 | Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`. |
@@ -371,6 +403,18 @@ Default names: | |||
371 | * `n_foo` -- number of foos | 403 | * `n_foo` -- number of foos |
372 | * `foo_idx` -- index of `foo` | 404 | * `foo_idx` -- index of `foo` |
373 | 405 | ||
406 | Many names in rust-analyzer conflict with keywords. | ||
407 | We use mangled names instead of `r#ident` syntax: | ||
408 | |||
409 | ``` | ||
410 | struct -> strukt | ||
411 | crate -> krate | ||
412 | impl -> imp | ||
413 | trait -> trait_ | ||
414 | fn -> func | ||
415 | enum -> enum_ | ||
416 | mod -> module | ||
417 | ``` | ||
374 | 418 | ||
375 | ## Early Returns | 419 | ## Early Returns |
376 | 420 | ||