diff --git a/Cargo.toml b/Cargo.toml index d5c6f25abf..6fd486957a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ await_holding_lock = "warn" dbg_macro = "warn" empty_enum = "warn" enum_glob_use = "warn" +equatable_if_let = "warn" exit = "warn" filter_map_next = "warn" fn_params_excessive_bools = "warn" @@ -33,6 +34,8 @@ inefficient_to_string = "warn" linkedlist = "warn" lossy_float_literal = "warn" macro_use_imports = "warn" +manual_let_else = "warn" +match_same_arms = "warn" match_wildcard_for_single_variants = "warn" mem_forget = "warn" must_use_candidate = "warn" @@ -41,6 +44,7 @@ needless_continue = "warn" option_option = "warn" rest_pat_in_fully_bound_structs = "warn" return_self_not_must_use = "warn" +single_match_else = "warn" str_to_string = "warn" suboptimal_flops = "warn" todo = "warn" diff --git a/axum-extra/src/extract/cached.rs b/axum-extra/src/extract/cached.rs index 64b4c3056f..8dfde30d30 100644 --- a/axum-extra/src/extract/cached.rs +++ b/axum-extra/src/extract/cached.rs @@ -88,13 +88,14 @@ where type Rejection = T::Rejection; async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { - match Extension::>::from_request_parts(parts, state).await { - Ok(Extension(CachedEntry(value))) => Ok(Self(value)), - Err(_) => { - let value = T::from_request_parts(parts, state).await?; - parts.extensions.insert(CachedEntry(value.clone())); - Ok(Self(value)) - } + if let Ok(Extension(CachedEntry(value))) = + Extension::>::from_request_parts(parts, state).await + { + Ok(Self(value)) + } else { + let value = T::from_request_parts(parts, state).await?; + parts.extensions.insert(CachedEntry(value.clone())); + Ok(Self(value)) } } } diff --git a/axum-extra/src/extract/json_deserializer.rs b/axum-extra/src/extract/json_deserializer.rs index 051ab0f1bd..8d53101999 100644 --- a/axum-extra/src/extract/json_deserializer.rs +++ b/axum-extra/src/extract/json_deserializer.rs @@ -183,21 +183,15 @@ composite_rejection! { } fn json_content_type(headers: &HeaderMap) -> bool { - let content_type = if let Some(content_type) = headers.get(header::CONTENT_TYPE) { - content_type - } else { + let Some(content_type) = headers.get(header::CONTENT_TYPE) else { return false; }; - let content_type = if let Ok(content_type) = content_type.to_str() { - content_type - } else { + let Ok(content_type) = content_type.to_str() else { return false; }; - let mime = if let Ok(mime) = content_type.parse::() { - mime - } else { + let Ok(mime) = content_type.parse::() else { return false; }; diff --git a/axum-macros/src/from_request/mod.rs b/axum-macros/src/from_request/mod.rs index 451ba9bfb1..f967fd105f 100644 --- a/axum-macros/src/from_request/mod.rs +++ b/axum-macros/src/from_request/mod.rs @@ -111,23 +111,22 @@ pub(crate) fn expand(item: syn::Item, tr: Trait) -> syn::Result { state, } = parse_attrs("from_request", &attrs)?; - let state = match state { - Some((_, state)) => State::Custom(state), - None => { - let mut inferred_state_types: HashSet<_> = - infer_state_type_from_field_types(&fields) - .chain(infer_state_type_from_field_attributes(&fields)) - .collect(); - - if let Some((_, via)) = &via { - inferred_state_types.extend(state_from_via(&ident, via)); - } + let state = if let Some((_, state)) = state { + State::Custom(state) + } else { + let mut inferred_state_types: HashSet<_> = + infer_state_type_from_field_types(&fields) + .chain(infer_state_type_from_field_attributes(&fields)) + .collect(); - match inferred_state_types.len() { - 0 => State::Default(syn::parse_quote!(S)), - 1 => State::Custom(inferred_state_types.iter().next().unwrap().to_owned()), - _ => State::CannotInfer, - } + if let Some((_, via)) = &via { + inferred_state_types.extend(state_from_via(&ident, via)); + } + + match inferred_state_types.len() { + 0 => State::Default(syn::parse_quote!(S)), + 1 => State::Custom(inferred_state_types.iter().next().unwrap().to_owned()), + _ => State::CannotInfer, } }; @@ -147,7 +146,7 @@ pub(crate) fn expand(item: syn::Item, tr: Trait) -> syn::Result { } }; - if let State::CannotInfer = state { + if matches!(state, State::CannotInfer) { let attr_name = match tr { Trait::FromRequest => "from_request", Trait::FromRequestParts => "from_request_parts", @@ -335,17 +334,16 @@ fn impl_struct_by_extracting_each_field( state: &State, tr: Trait, ) -> syn::Result { - let trait_fn_body = match state { - State::CannotInfer => quote! { + let trait_fn_body = if matches!(state, State::CannotInfer) { + quote! { ::std::unimplemented!() - }, - _ => { - let extract_fields = extract_fields(&fields, &rejection, tr)?; - quote! { - ::std::result::Result::Ok(Self { - #(#extract_fields)* - }) - } + } + } else { + let extract_fields = extract_fields(&fields, &rejection, tr)?; + quote! { + ::std::result::Result::Ok(Self { + #(#extract_fields)* + }) } }; @@ -417,15 +415,14 @@ fn extract_fields( tr: Trait, ) -> syn::Result> { fn member(field: &syn::Field, index: usize) -> TokenStream { - match &field.ident { - Some(ident) => quote! { #ident }, - _ => { - let member = syn::Member::Unnamed(syn::Index { - index: index as u32, - span: field.span(), - }); - quote! { #member } - } + if let Some(ident) = &field.ident { + quote! { #ident } + } else { + let member = syn::Member::Unnamed(syn::Index { + index: index as u32, + span: field.span(), + }); + quote! { #member } } } @@ -642,9 +639,7 @@ fn extract_fields( } fn peel_option(ty: &syn::Type) -> Option<&syn::Type> { - let type_path = if let syn::Type::Path(type_path) = ty { - type_path - } else { + let syn::Type::Path(type_path) = ty else { return None; }; @@ -673,9 +668,7 @@ fn peel_option(ty: &syn::Type) -> Option<&syn::Type> { } fn peel_result_ok(ty: &syn::Type) -> Option<&syn::Type> { - let type_path = if let syn::Type::Path(type_path) = ty { - type_path - } else { + let syn::Type::Path(type_path) = ty else { return None; }; diff --git a/axum/src/extract/matched_path.rs b/axum/src/extract/matched_path.rs index 1dda92354c..eb6ef49929 100644 --- a/axum/src/extract/matched_path.rs +++ b/axum/src/extract/matched_path.rs @@ -103,9 +103,7 @@ pub(crate) fn set_matched_path_for_request( route_id_to_path: &HashMap>, extensions: &mut http::Extensions, ) { - let matched_path = if let Some(matched_path) = route_id_to_path.get(&id) { - matched_path - } else { + let Some(matched_path) = route_id_to_path.get(&id) else { #[cfg(debug_assertions)] panic!("should always have a matched path for a route id"); #[cfg(not(debug_assertions))] diff --git a/axum/src/extract/mod.rs b/axum/src/extract/mod.rs index 8a0af3da49..d2b19b684d 100644 --- a/axum/src/extract/mod.rs +++ b/axum/src/extract/mod.rs @@ -81,15 +81,11 @@ pub use self::ws::WebSocketUpgrade; // this is duplicated in `axum-extra/src/extract/form.rs` pub(super) fn has_content_type(headers: &HeaderMap, expected_content_type: &mime::Mime) -> bool { - let content_type = if let Some(content_type) = headers.get(header::CONTENT_TYPE) { - content_type - } else { + let Some(content_type) = headers.get(header::CONTENT_TYPE) else { return false; }; - let content_type = if let Ok(content_type) = content_type.to_str() { - content_type - } else { + let Ok(content_type) = content_type.to_str() else { return false; }; diff --git a/axum/src/extract/path/de.rs b/axum/src/extract/path/de.rs index ca78bb9e23..aae2ac7b75 100644 --- a/axum/src/extract/path/de.rs +++ b/axum/src/extract/path/de.rs @@ -639,8 +639,7 @@ enum KeyOrIdx<'de> { impl<'de> KeyOrIdx<'de> { fn key(&self) -> &'de str { match &self { - Self::Key(key) => key, - Self::Idx { key, .. } => key, + Self::Idx { key, .. } | Self::Key(key) => key, } } } diff --git a/axum/src/extract/ws.rs b/axum/src/extract/ws.rs index 67d9247c8f..83d336cb92 100644 --- a/axum/src/extract/ws.rs +++ b/axum/src/extract/ws.rs @@ -488,9 +488,7 @@ fn header_eq(headers: &HeaderMap, key: HeaderName, value: &'static str) -> bool } fn header_contains(headers: &HeaderMap, key: HeaderName, value: &'static str) -> bool { - let header = if let Some(header) = headers.get(&key) { - header - } else { + let Some(header) = headers.get(&key) else { return false; }; diff --git a/axum/src/routing/method_routing.rs b/axum/src/routing/method_routing.rs index 4a39542bd1..62f43595e9 100644 --- a/axum/src/routing/method_routing.rs +++ b/axum/src/routing/method_routing.rs @@ -573,8 +573,7 @@ impl AllowHeader { match (self, other) { (Self::Skip, _) | (_, Self::Skip) => Self::Skip, (Self::None, Self::None) => Self::None, - (Self::None, Self::Bytes(pick)) => Self::Bytes(pick), - (Self::Bytes(pick), Self::None) => Self::Bytes(pick), + (Self::None, Self::Bytes(pick)) | (Self::Bytes(pick), Self::None) => Self::Bytes(pick), (Self::Bytes(mut a), Self::Bytes(b)) => { a.extend_from_slice(b","); a.extend_from_slice(&b); diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index ce2425f1bc..1f88480ffb 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -187,14 +187,11 @@ where T::Response: IntoResponse, T::Future: Send + 'static, { - let service = match try_downcast::(service) { - Ok(_) => { - panic!( - "Invalid route: `Router::route_service` cannot be used with `Router`s. \ - Use `Router::nest` instead" - ); - } - Err(service) => service, + let Err(service) = try_downcast::(service) else { + panic!( + "Invalid route: `Router::route_service` cannot be used with `Router`s. \ + Use `Router::nest` instead" + ); }; tap_inner!(self, mut this => { @@ -256,16 +253,13 @@ where map_inner!(self, mut this => { match (this.default_fallback, default_fallback) { - // both have the default fallback + // other has a default fallback // use the one from other - (true, true) => {} + (_, true) => {} // this has default fallback, other has a custom fallback (true, false) => { this.default_fallback = false; } - // this has a custom fallback, other has a default - (false, true) => { - } // both have a custom fallback, not allowed (false, false) => { panic!("Cannot merge two `Router`s that both have a fallback") @@ -707,8 +701,9 @@ where { fn merge(self, other: Self) -> Option { match (self, other) { - (Self::Default(_), pick @ Self::Default(_)) => Some(pick), + // If either are `Default`, return the opposite one. (Self::Default(_), pick) | (pick, Self::Default(_)) => Some(pick), + // Otherwise, return None _ => None, } }