Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ empty_enum = "warn"
enum_glob_use = "warn"
equatable_if_let = "warn"
exit = "warn"
explicit_iter_loop = "warn"
filter_map_next = "warn"
fn_params_excessive_bools = "warn"
if_let_mutex = "warn"
if_not_else = "warn"
implicit_clone = "warn"
imprecise_flops = "warn"
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 All @@ -46,6 +49,7 @@ needless_pass_by_ref_mut = "warn"
needless_pass_by_value = "warn"
option_option = "warn"
redundant_clone = "warn"
redundant_else = "warn"
ref_option = "warn"
rest_pat_in_fully_bound_structs = "warn"
return_self_not_must_use = "warn"
Expand All @@ -55,7 +59,9 @@ suboptimal_flops = "warn"
todo = "warn"
trivially_copy_pass_by_ref = "warn"
uninlined_format_args = "warn"
unnecessary_semicolon = "warn"
unnested_or_patterns = "warn"
unused_peekable = "warn"
unused_self = "warn"
use_self = "warn"
verbose_file_reads = "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
5 changes: 2 additions & 3 deletions axum-macros/src/debug_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ fn check_input_order(item_fn: &ItemFn, kind: FunctionKind) -> Option<TokenStream

if types_that_consume_the_request.is_empty() {
return None;
};
}

// exactly one type that consumes the request
if types_that_consume_the_request.len() == 1 {
Expand All @@ -536,9 +536,8 @@ fn check_input_order(item_fn: &ItemFn, kind: FunctionKind) -> Option<TokenStream
return Some(quote_spanned! {*span=>
compile_error!(#error);
});
} else {
return None;
}
return None;
}

if types_that_consume_the_request.len() == 2 {
Expand Down
16 changes: 8 additions & 8 deletions axum-macros/src/with_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ impl<I: Iterator> Iterator for WithPosition<I> {
fn next(&mut self) -> Option<Self::Item> {
match self.peekable.next() {
Some(item) => {
if !self.handled_first {
if self.handled_first {
// Have seen the first item, and there's something left.
// Peek to see if this is the last item.
match self.peekable.peek() {
Some(_) => Some(Position::Middle(item)),
None => Some(Position::Last(item)),
}
} else {
// Haven't seen the first item yet, and there is one to give.
self.handled_first = true;
// Peek to see if this is also the last item,
Expand All @@ -92,13 +99,6 @@ impl<I: Iterator> Iterator for WithPosition<I> {
Some(_) => Some(Position::First(item)),
None => Some(Position::Only(item)),
}
} else {
// Have seen the first item, and there's something left.
// Peek to see if this is the last item.
match self.peekable.peek() {
Some(_) => Some(Position::Middle(item)),
None => Some(Position::Last(item)),
}
}
}
// Iterator is finished.
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
2 changes: 1 addition & 1 deletion axum/src/extract/nested_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where
} else {
req.extensions_mut()
.insert(NestedPath(Arc::clone(&self.path)));
};
}

self.inner.call(req)
}
Expand Down
2 changes: 1 addition & 1 deletion axum/src/extract/path/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ impl<'de> Deserializer<'de> for ValueDeserializer<'de> {
));
}
None => {}
};
}

self.value
.take()
Expand Down
37 changes: 21 additions & 16 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 Expand Up @@ -746,7 +751,7 @@ mod tests {
fn parse_event(payload: &str) -> HashMap<String, String> {
let mut fields = HashMap::new();

let mut lines = payload.lines().peekable();
let mut lines = payload.lines();
while let Some(line) = lines.next() {
if line.is_empty() {
assert!(lines.next().is_none());
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
16 changes: 9 additions & 7 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 Expand Up @@ -264,7 +266,7 @@ where
(false, false) => {
panic!("Cannot merge two `Router`s that both have a fallback")
}
};
}

panic_on_err!(this.path_router.merge(path_router));

Expand Down
11 changes: 5 additions & 6 deletions axum/src/routing/path_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
H: Handler<T, S>,
T: 'static,
{
for (_, endpoint) in self.routes.iter_mut() {
for endpoint in self.routes.values_mut() {
if let Endpoint::MethodRouter(rt) = endpoint {
*rt = rt.clone().default_fallback(handler.clone());
}
Expand Down 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
4 changes: 2 additions & 2 deletions axum/src/routing/tests/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ async fn multiple_ors_balanced_differently() {
async fn test(name: &str, app: Router) {
let client = TestClient::new(app);

for n in ["one", "two", "three", "four"].iter() {
for n in ["one", "two", "three", "four"] {
println!("running: {name} / {n}");
let res = client.get(&format!("/{n}")).await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, *n);
assert_eq!(res.text().await, n);
}
}
}
Expand Down
Loading