Skip to content

Conversation

theodorebje
Copy link
Contributor

@theodorebje theodorebje commented Jul 11, 2025

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.

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.
@theodorebje theodorebje mentioned this pull request Jul 11, 2025
5 tasks
Copy link
Member

@jplatte jplatte left a 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.
Copy link
Member

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(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out.is_none()

Comment on lines +840 to 841
"Overlapping method route. Cannot add two method routes that both handle \
`{method_name}`",
Copy link
Member

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 == "/"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
!(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 == "/"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
!(path.is_empty() || path == "/"),
!path.is_empty() && path != "/"),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants