Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ inefficient_to_string = "warn"
linkedlist = "warn"
lossy_float_literal = "warn"
macro_use_imports = "warn"
manual_assert = "warn"
manual_let_else = "warn"
match_same_arms = "warn"
match_wildcard_for_single_variants = "warn"
Expand Down
19 changes: 10 additions & 9 deletions axum-extra/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ pub use self::typed::{SecondElementIs, TypedPath};
#[doc(hidden)]
#[must_use]
pub const fn __private_validate_static_path(path: &'static str) -> &'static str {
if path.is_empty() {
panic!("Paths must start with a `/`. Use \"/\" for root routes")
}
if path.as_bytes()[0] != b'/' {
panic!("Paths must start with /");
}
assert!(
!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.

assert!(path.as_bytes()[0] == b'/', "Paths must start with /");
path
}

Expand Down Expand Up @@ -356,9 +356,10 @@ where

#[track_caller]
fn validate_tsr_path(path: &str) {
if path == "/" {
panic!("Cannot add a trailing slash redirect route for `/`")
}
assert!(
path != "/",
"Cannot add a trailing slash redirect route for `/`"
);
}

fn add_tsr_redirect_route<S>(router: Router<S>, path: &str) -> Router<S>
Expand Down
4 changes: 1 addition & 3 deletions axum/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ fn install_rewrk() {
let status = cmd
.status()
.unwrap_or_else(|_| panic!("failed to install rewrk"));
if !status.success() {
panic!("failed to install rewrk");
}
assert!(status.success(), "failed to install rewrk");
}

fn ensure_rewrk_is_installed() {
Expand Down
35 changes: 20 additions & 15 deletions axum/src/response/sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ impl Event {
where
T: AsRef<str>,
{
if self.flags.contains(EventFlags::HAS_DATA) {
panic!("Called `Event::data` multiple times");
}
assert!(
!self.flags.contains(EventFlags::HAS_DATA),
"Called `Event::data` multiple times"
);

for line in memchr_split(b'\n', data.as_ref().as_bytes()) {
self.field("data", line);
Expand Down Expand Up @@ -246,9 +247,10 @@ impl Event {
self.0.flush()
}
}
if self.flags.contains(EventFlags::HAS_DATA) {
panic!("Called `Event::json_data` multiple times");
}
assert!(
!self.flags.contains(EventFlags::HAS_DATA),
"Called `Event::json_data` multiple times"
);

let buffer = self.buffer.as_mut();
buffer.extend_from_slice(b"data: ");
Expand Down Expand Up @@ -297,9 +299,10 @@ impl Event {
where
T: AsRef<str>,
{
if self.flags.contains(EventFlags::HAS_EVENT) {
panic!("Called `Event::event` multiple times");
}
assert!(
!self.flags.contains(EventFlags::HAS_EVENT),
"Called `Event::event` multiple times"
);
self.flags.insert(EventFlags::HAS_EVENT);

self.field("event", event.as_ref());
Expand All @@ -317,9 +320,10 @@ impl Event {
///
/// Panics if this function has already been called on this event.
pub fn retry(mut self, duration: Duration) -> Self {
if self.flags.contains(EventFlags::HAS_RETRY) {
panic!("Called `Event::retry` multiple times");
}
assert!(
!self.flags.contains(EventFlags::HAS_RETRY),
"Called `Event::retry` multiple times"
);
self.flags.insert(EventFlags::HAS_RETRY);

let buffer = self.buffer.as_mut();
Expand Down Expand Up @@ -364,9 +368,10 @@ impl Event {
where
T: AsRef<str>,
{
if self.flags.contains(EventFlags::HAS_ID) {
panic!("Called `Event::id` multiple times");
}
assert!(
!self.flags.contains(EventFlags::HAS_ID),
"Called `Event::id` multiple times"
);
self.flags.insert(EventFlags::HAS_ID);

let id = id.as_ref().as_bytes();
Expand Down
9 changes: 4 additions & 5 deletions axum/src/routing/method_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,12 +835,11 @@ where
S: Clone,
{
if endpoint_filter.contains(filter) {
if out.is_some() {
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()

"Overlapping method route. Cannot add two method routes that both handle \
`{method_name}`",
Comment on lines +840 to 841
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.

)
}
);
*out = endpoint.clone();
for method in methods {
append_allow_header(allow_header, method);
Expand Down
14 changes: 8 additions & 6 deletions axum/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ where
#[doc(alias = "scope")] // Some web frameworks like actix-web use this term
#[track_caller]
pub fn nest(self, path: &str, router: Self) -> Self {
if path.is_empty() || path == "/" {
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 != "/"),

"Nesting at the root is no longer supported. Use merge instead."
);

let RouterInner {
path_router,
Expand All @@ -229,9 +230,10 @@ where
T::Response: IntoResponse,
T::Future: Send + 'static,
{
if 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 != "/"),

"Nesting at the root is no longer supported. Use fallback_service instead."
);

tap_inner!(self, mut this => {
panic_on_err!(this.path_router.nest_service(path, service));
Expand Down
9 changes: 4 additions & 5 deletions axum/src/routing/path_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,11 @@ where
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
{
if self.routes.is_empty() {
panic!(
"Adding a route_layer before any routes is a no-op. \
assert!(
!self.routes.is_empty(),
"Adding a route_layer before any routes is a no-op. \
Add the routes you want the layer to apply to first."
);
}
);

let routes = self
.routes
Expand Down
Loading