-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Declutter statements #3408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Declutter statements #3408
Conversation
The `else` block adds unnecessary indentation and verbosity. To avoid this, we can simply move `return None` statement outside the `if` statement. This fixes the `redundant_else` clippy lint.
`if !..else` is a confusing syntax, as it does the inverse of what the rest of the lines says. By reordering the `if` statement we can make the `if` statement more clear. This fixes the `if_not_else` clippy lint.
Calling `.iter()` on items that are already iterable is redundant and makes the code more difficult to read. This commit also makes a couple of other related style changes to make the code easier to read. For example, removing the `iter` call on `self.routes` helped clippy find a violation of `for_kv_map`, which I also fixed. Clippy also recommended changing `["one", "two", "three", "four"].iter()` to `&["one", "two", "three", "four"]`, which does indeed match the previous behaviour. However, we can make the code easier to read by removing the unnecessary dereference of `*n` in the last `assert_eq` call by turning the slice into an array (`["one", "two", "three", "four"]`). This makes the code more readable, for free! This fixes the `explicit_iter_loop` clippy lint.
This is likely a leftover from a refactor. This fixes the `unused_peekable` clippy lint.
Remove unnecessary semicolons at the end of control flow statements (`if` and `match`). These are not needed, and can safely be removed to avoid confusion, visual clutter, and improve readability of the code. This fixes the `unnecessary_semicolon` clippy lint.
`assert!` is much more concise and readable than manually triggering a panic via an `if` statement. Once I did this, clippy also found a couple of conditions that could be simplified. I applied those simplifications. This fixes the `manual_assert` clippy lint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the first commit, doesn't seem like a improvement to me. Actually instead of if ... { return Some(_); } else { return None; }
we could do return (...).then(|| _);
.
!path.is_empty(), | ||
"Paths must start with a `/`. Use \"/\" for root routes" | ||
); | ||
// `assert_eq!` is not allowed in constant functions so we have to do it manually. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I commented on this before, but I wouldn't want assert_eq
here even if it was possible, since it adds a LHS-RHS comparison to the output, which would not be helpful here.
panic!( | ||
"Overlapping method route. Cannot add two method routes that both handle \ | ||
assert!( | ||
!out.is_some(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
out.is_none()
"Overlapping method route. Cannot add two method routes that both handle \ | ||
`{method_name}`", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this fit the line as a single line string literal now? If no, please reduce indentation on the second line by 4 spaces.
panic!("Nesting at the root is no longer supported. Use merge instead."); | ||
} | ||
assert!( | ||
!(path.is_empty() || path == "/"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!(path.is_empty() || path == "/"), | |
!path.is_empty() && path != "/"), |
panic!("Nesting at the root is no longer supported. Use fallback_service instead."); | ||
} | ||
assert!( | ||
!(path.is_empty() || path == "/"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!(path.is_empty() || path == "/"), | |
!path.is_empty() && path != "/"), |
Motivation
This PR is part of the broader PR #3394 (Activate more lints)
This is an extension to the PR #3399 (Simplify
if
statements)This PR adds additionally stylistic Clippy lints to improve readability and cleans up redundant, old, leftover code in order to improve maintainability.
Solution
This PR adds 6 new lints to
Cargo.toml
and also fixes them in the code:redundant_else
if_not_else
explicit_iter_loop
unused_peekable
unnecessary_semicolon
manual_assert
See each individual commit's message for more information.