internal: add => () rule; emphasize n_items rule

This commit is contained in:
Aleksey Kladov 2021-09-25 14:10:25 +03:00
parent 1567bbb73e
commit d72f7cf3af
2 changed files with 24 additions and 4 deletions

View file

@ -439,10 +439,10 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
let eq_check = let eq_check =
make::expr_bin_op(lhs, BinaryOp::CmpOp(CmpOp::Eq { negated: false }), rhs); make::expr_bin_op(lhs, BinaryOp::CmpOp(CmpOp::Eq { negated: false }), rhs);
let mut case_count = 0; let mut n_cases = 0;
let mut arms = vec![]; let mut arms = vec![];
for variant in enum_.variant_list()?.variants() { for variant in enum_.variant_list()?.variants() {
case_count += 1; n_cases += 1;
match variant.field_list() { match variant.field_list() {
// => (Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin, // => (Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
Some(ast::FieldList::RecordFieldList(list)) => { Some(ast::FieldList::RecordFieldList(list)) => {
@ -517,7 +517,7 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
let expr = match arms.len() { let expr = match arms.len() {
0 => eq_check, 0 => eq_check,
_ => { _ => {
if case_count > arms.len() { if n_cases > arms.len() {
let lhs = make::wildcard_pat().into(); let lhs = make::wildcard_pat().into();
arms.push(make::match_arm(Some(lhs), None, eq_check)); arms.push(make::match_arm(Some(lhs), None, eq_check));
} }

View file

@ -849,7 +849,7 @@ Default names:
* `res` -- "result of the function" local variable * `res` -- "result of the function" local variable
* `it` -- I don't really care about the name * `it` -- I don't really care about the name
* `n_foo` -- number of foos * `n_foos` -- number of foos (prefer this to `foo_count`)
* `foo_idx` -- index of `foo` * `foo_idx` -- index of `foo`
Many names in rust-analyzer conflict with keywords. Many names in rust-analyzer conflict with keywords.
@ -969,6 +969,26 @@ Don't use the `ref` keyword.
Today, it is redundant. Today, it is redundant.
Between `ref` and mach ergonomics, the latter is more ergonomic in most cases, and is simpler (does not require a keyword). Between `ref` and mach ergonomics, the latter is more ergonomic in most cases, and is simpler (does not require a keyword).
## Empty Match Arms
Ues `=> (),` when a match arm is intentionally empty:
```rust
// GOOD
match result {
Ok(_) => (),
Err(err) => error!("{}", err),
}
// BAD
match result {
Ok(_) => {}
Err(err) => error!("{}", err),
}
```
**Rationale:** consistency.
## Functional Combinators ## Functional Combinators
Use high order monadic combinators like `map`, `then` when they are a natural choice; don't bend the code to fit into some combinator. Use high order monadic combinators like `map`, `then` when they are a natural choice; don't bend the code to fit into some combinator.