Skip to content

Commit 850a881

Browse files
committed
fix: clippy fixes for collapsing if-statements
1 parent a02cd76 commit 850a881

15 files changed

Lines changed: 293 additions & 314 deletions

File tree

crates/rmcp-macros/src/common.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,15 @@ pub fn extract_doc_line(
5555
/// Returns the full Parameters<T> type if found
5656
pub fn find_parameters_type_in_sig(sig: &Signature) -> Option<Box<Type>> {
5757
sig.inputs.iter().find_map(|input| {
58-
if let FnArg::Typed(pat_type) = input {
59-
if let Type::Path(type_path) = &*pat_type.ty {
60-
if type_path
61-
.path
62-
.segments
63-
.last()
64-
.is_some_and(|type_name| type_name.ident == "Parameters")
65-
{
66-
return Some(pat_type.ty.clone());
67-
}
68-
}
58+
if let FnArg::Typed(pat_type) = input
59+
&& let Type::Path(type_path) = &*pat_type.ty
60+
&& type_path
61+
.path
62+
.segments
63+
.last()
64+
.is_some_and(|type_name| type_name.ident == "Parameters")
65+
{
66+
return Some(pat_type.ty.clone());
6967
}
7068
None
7169
})

crates/rmcp-macros/src/prompt.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ pub fn prompt(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream>
132132
// 3. make body: { Box::pin(async move { #body }) }
133133
let new_output = syn::parse2::<ReturnType>({
134134
let mut lt = quote! { 'static };
135-
if let Some(receiver) = fn_item.sig.receiver() {
136-
if let Some((_, receiver_lt)) = receiver.reference.as_ref() {
137-
if let Some(receiver_lt) = receiver_lt {
138-
lt = quote! { #receiver_lt };
139-
} else {
140-
lt = quote! { '_ };
141-
}
135+
if let Some(receiver) = fn_item.sig.receiver()
136+
&& let Some((_, receiver_lt)) = receiver.reference.as_ref()
137+
{
138+
if let Some(receiver_lt) = receiver_lt {
139+
lt = quote! { #receiver_lt };
140+
} else {
141+
lt = quote! { '_ };
142142
}
143143
}
144144
match &fn_item.sig.output {

crates/rmcp-macros/src/tool.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ use crate::common::extract_doc_line;
77

88
/// Check if a type is Json<T> and extract the inner type T
99
fn extract_json_inner_type(ty: &syn::Type) -> Option<&syn::Type> {
10-
if let syn::Type::Path(type_path) = ty {
11-
if let Some(last_segment) = type_path.path.segments.last() {
12-
if last_segment.ident == "Json" {
13-
if let syn::PathArguments::AngleBracketed(args) = &last_segment.arguments {
14-
if let Some(syn::GenericArgument::Type(inner_type)) = args.args.first() {
15-
return Some(inner_type);
16-
}
17-
}
18-
}
19-
}
10+
if let syn::Type::Path(type_path) = ty
11+
&& let Some(last_segment) = type_path.path.segments.last()
12+
&& last_segment.ident == "Json"
13+
&& let syn::PathArguments::AngleBracketed(args) = &last_segment.arguments
14+
&& let Some(syn::GenericArgument::Type(inner_type)) = args.args.first()
15+
{
16+
return Some(inner_type);
2017
}
2118
None
2219
}
@@ -286,13 +283,13 @@ pub fn tool(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
286283
let omit_send = cfg!(feature = "local") || attribute.local;
287284
let new_output = syn::parse2::<ReturnType>({
288285
let mut lt = quote! { 'static };
289-
if let Some(receiver) = fn_item.sig.receiver() {
290-
if let Some((_, receiver_lt)) = receiver.reference.as_ref() {
291-
if let Some(receiver_lt) = receiver_lt {
292-
lt = quote! { #receiver_lt };
293-
} else {
294-
lt = quote! { '_ };
295-
}
286+
if let Some(receiver) = fn_item.sig.receiver()
287+
&& let Some((_, receiver_lt)) = receiver.reference.as_ref()
288+
{
289+
if let Some(receiver_lt) = receiver_lt {
290+
lt = quote! { #receiver_lt };
291+
} else {
292+
lt = quote! { '_ };
296293
}
297294
}
298295
match &fn_item.sig.output {

crates/rmcp/src/handler/server/router/tool.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,10 @@ where
550550
}
551551

552552
fn notify_if_visible(&self, name: &str) {
553-
if self.map.contains_key(name) {
554-
if let Some(notifier) = &self.notifier {
555-
notifier();
556-
}
553+
if self.map.contains_key(name)
554+
&& let Some(notifier) = &self.notifier
555+
{
556+
notifier();
557557
}
558558
}
559559

crates/rmcp/src/model/elicitation_schema.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -983,17 +983,15 @@ impl EnumSchemaBuilder<MultiSelect> {
983983
return Err("One of the provided default values is not in enum values".to_string());
984984
}
985985
}
986-
if let Some(min) = self.min_items {
987-
if (default_values.len() as u64) < min {
988-
return Err("Number of provided default values is less than min_items".to_string());
989-
}
986+
if let Some(min) = self.min_items
987+
&& (default_values.len() as u64) < min
988+
{
989+
return Err("Number of provided default values is less than min_items".to_string());
990990
}
991-
if let Some(max) = self.max_items {
992-
if (default_values.len() as u64) > max {
993-
return Err(
994-
"Number of provided default values is greater than max_items".to_string(),
995-
);
996-
}
991+
if let Some(max) = self.max_items
992+
&& (default_values.len() as u64) > max
993+
{
994+
return Err("Number of provided default values is greater than max_items".to_string());
997995
}
998996
self.default = default_values;
999997
Ok(self)

crates/rmcp/src/service.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,10 @@ impl<R: ServiceRole> RequestHandle<R> {
504504
None => None,
505505
}
506506
}, if reset_timeout_on_progress && idle_sleep.is_some() && self.progress_reset_rx.is_some() => {
507-
if progress.is_some() {
508-
if let Some((timeout, sleep)) = idle_sleep.as_mut() {
507+
if progress.is_some()
508+
&& let Some((timeout, sleep)) = idle_sleep.as_mut() {
509509
sleep.as_mut().reset(tokio::time::Instant::now() + *timeout);
510510
}
511-
}
512511
}
513512
}
514513
}
@@ -1291,11 +1290,10 @@ where
12911290
tracing::trace!(?evt, "new event");
12921291
match evt {
12931292
Event::SendTaskResult(SendTaskResult::Request { id, result }) => {
1294-
if let Err(e) = result {
1295-
if let Some(responder) = local_responder_pool.remove(&id) {
1293+
if let Err(e) = result
1294+
&& let Some(responder) = local_responder_pool.remove(&id) {
12961295
let _ = responder.send(Err(ServiceError::TransportSend(e)));
12971296
}
1298-
}
12991297
}
13001298
Event::SendTaskResult(SendTaskResult::Notification {
13011299
responder,
@@ -1308,16 +1306,14 @@ where
13081306
Ok(())
13091307
};
13101308
let _ = responder.send(response);
1311-
if let Some(param) = cancellation_param {
1312-
if let Some(request_id) = &param.request_id {
1313-
if let Some(responder) = local_responder_pool.remove(request_id) {
1309+
if let Some(param) = cancellation_param
1310+
&& let Some(request_id) = &param.request_id
1311+
&& let Some(responder) = local_responder_pool.remove(request_id) {
13141312
tracing::info!(id = %request_id, reason = param.reason, "cancelled");
13151313
let _response_result = responder.send(Err(ServiceError::Cancelled {
13161314
reason: param.reason.clone(),
13171315
}));
13181316
}
1319-
}
1320-
}
13211317
}
13221318
Event::ResponseSendTaskResult(result) => {
13231319
if let Err(error) = result {

crates/rmcp/src/transport/async_rw.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,12 @@ fn try_parse_with_compatibility<T: serde::de::DeserializeOwned>(
320320
Ok(item) => Ok(Some(item)),
321321
Err(e) => {
322322
// Check if this is a notification that should be ignored for compatibility
323-
if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(line_str) {
324-
if let Some(method) =
323+
if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(line_str)
324+
&& let Some(method) =
325325
json_value.get("method").and_then(serde_json::Value::as_str)
326-
{
327-
if should_ignore_notification(&json_value, method) {
328-
return Ok(None);
329-
}
330-
}
326+
&& should_ignore_notification(&json_value, method)
327+
{
328+
return Ok(None);
331329
}
332330

333331
tracing::debug!(

crates/rmcp/src/transport/auth.rs

Lines changed: 68 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,56 +1091,56 @@ impl AuthorizationManager {
10911091
/// the client if credentials are found. Returns `false` when credentials
10921092
/// are absent or discarded after an authorization-server change.
10931093
pub async fn initialize_from_store(&mut self) -> Result<bool, AuthError> {
1094-
if let Some(stored) = self.credential_store.load().await? {
1095-
if stored.token_response.is_some() {
1096-
if self.metadata.is_none() {
1097-
let metadata = self.discover_metadata().await?;
1098-
self.metadata = Some(metadata);
1099-
}
1100-
1101-
if let (Some(stored_issuer), Some(current_issuer)) =
1102-
(stored.issuer.as_deref(), self.metadata_issuer().as_deref())
1103-
{
1104-
// A CIMD client ID is the client's metadata URL, so it is
1105-
// portable across authorization servers and exempt here.
1106-
if stored_issuer != current_issuer {
1107-
if is_https_url(&stored.client_id) {
1108-
// A CIMD client ID is the client's metadata URL, so it is
1109-
// portable across authorization servers — but the tokens
1110-
// were minted by the previous AS and must not be reused.
1111-
tracing::warn!(
1112-
stored_issuer,
1113-
current_issuer,
1114-
"authorization server issuer changed; discarding tokens but keeping portable CIMD client ID"
1115-
);
1116-
self.credential_store
1117-
.save(
1118-
StoredCredentials::new(
1119-
stored.client_id.clone(),
1120-
None,
1121-
vec![],
1122-
None,
1123-
)
1124-
.with_issuer(self.metadata_issuer()),
1125-
)
1126-
.await?;
1127-
self.configure_client_id(&stored.client_id)?;
1128-
return Ok(false);
1129-
}
1094+
if let Some(stored) = self.credential_store.load().await?
1095+
&& stored.token_response.is_some()
1096+
{
1097+
if self.metadata.is_none() {
1098+
let metadata = self.discover_metadata().await?;
1099+
self.metadata = Some(metadata);
1100+
}
11301101

1102+
if let (Some(stored_issuer), Some(current_issuer)) =
1103+
(stored.issuer.as_deref(), self.metadata_issuer().as_deref())
1104+
{
1105+
// A CIMD client ID is the client's metadata URL, so it is
1106+
// portable across authorization servers and exempt here.
1107+
if stored_issuer != current_issuer {
1108+
if is_https_url(&stored.client_id) {
1109+
// A CIMD client ID is the client's metadata URL, so it is
1110+
// portable across authorization servers — but the tokens
1111+
// were minted by the previous AS and must not be reused.
11311112
tracing::warn!(
11321113
stored_issuer,
11331114
current_issuer,
1134-
"authorization server issuer changed; clearing stored credentials bound to the previous issuer"
1115+
"authorization server issuer changed; discarding tokens but keeping portable CIMD client ID"
11351116
);
1136-
self.credential_store.clear().await?;
1117+
self.credential_store
1118+
.save(
1119+
StoredCredentials::new(
1120+
stored.client_id.clone(),
1121+
None,
1122+
vec![],
1123+
None,
1124+
)
1125+
.with_issuer(self.metadata_issuer()),
1126+
)
1127+
.await?;
1128+
self.configure_client_id(&stored.client_id)?;
11371129
return Ok(false);
11381130
}
1139-
}
11401131

1141-
self.configure_client_id(&stored.client_id)?;
1142-
return Ok(true);
1132+
tracing::warn!(
1133+
stored_issuer,
1134+
current_issuer,
1135+
"authorization server issuer changed; clearing stored credentials bound to the previous issuer"
1136+
);
1137+
self.credential_store.clear().await?;
1138+
return Ok(false);
1139+
}
11431140
}
1141+
1142+
self.configure_client_id(&stored.client_id)?;
1143+
return Ok(true);
11441144
}
11451145
Ok(false)
11461146
}
@@ -1268,10 +1268,10 @@ impl AuthorizationManager {
12681268

12691269
// RFC 8414 RECOMMENDS response_types_supported in the metadata. This field is optional,
12701270
// but if present and does not include the flow we use ("code"), bail out early with a clear error.
1271-
if let Some(response_types_supported) = metadata.response_types_supported.as_ref() {
1272-
if !response_types_supported.contains(&response_type.to_string()) {
1273-
return Err(AuthError::InvalidScope(response_type.to_string()));
1274-
}
1271+
if let Some(response_types_supported) = metadata.response_types_supported.as_ref()
1272+
&& !response_types_supported.contains(&response_type.to_string())
1273+
{
1274+
return Err(AuthError::InvalidScope(response_type.to_string()));
12751275
}
12761276

12771277
// The client always sends an S256 challenge. A server that advertises
@@ -1556,12 +1556,11 @@ impl AuthorizationManager {
15561556
}
15571557

15581558
// nothing requested or challenged yet: seed from AS metadata, then caller defaults
1559-
if let Some(metadata) = &self.metadata {
1560-
if let Some(scopes_supported) = &metadata.scopes_supported {
1561-
if !scopes_supported.is_empty() {
1562-
return scopes_supported.clone();
1563-
}
1564-
}
1559+
if let Some(metadata) = &self.metadata
1560+
&& let Some(scopes_supported) = &metadata.scopes_supported
1561+
&& !scopes_supported.is_empty()
1562+
{
1563+
return scopes_supported.clone();
15651564
}
15661565

15671566
default_scopes.iter().map(|s| s.to_string()).collect()
@@ -1573,12 +1572,11 @@ impl AuthorizationManager {
15731572
if scopes.is_empty() || scopes.iter().any(|s| s == "offline_access") {
15741573
return;
15751574
}
1576-
if let Some(metadata) = &self.metadata {
1577-
if let Some(supported) = &metadata.scopes_supported {
1578-
if supported.iter().any(|s| s == "offline_access") {
1579-
scopes.push("offline_access".to_string());
1580-
}
1581-
}
1575+
if let Some(metadata) = &self.metadata
1576+
&& let Some(supported) = &metadata.scopes_supported
1577+
&& supported.iter().any(|s| s == "offline_access")
1578+
{
1579+
scopes.push("offline_access".to_string());
15821580
}
15831581
}
15841582

@@ -2098,10 +2096,10 @@ impl AuthorizationManager {
20982096
self.validate_resource_metadata_resource(&resource_metadata)?;
20992097

21002098
// store scopes_supported from protected resource metadata for select_scopes()
2101-
if let Some(scopes) = resource_metadata.scopes_supported {
2102-
if !scopes.is_empty() {
2103-
*self.resource_scopes.write().await = scopes;
2104-
}
2099+
if let Some(scopes) = resource_metadata.scopes_supported
2100+
&& !scopes.is_empty()
2101+
{
2102+
*self.resource_scopes.write().await = scopes;
21052103
}
21062104

21072105
let mut candidates = Vec::new();
@@ -2544,20 +2542,18 @@ impl AuthorizationManager {
25442542
if let ClientCredentialsConfig::PrivateKeyJwt {
25452543
signing_algorithm, ..
25462544
} = config
2547-
{
2548-
if let Some(algs) = metadata
2545+
&& let Some(algs) = metadata
25492546
.additional_fields
25502547
.get("token_endpoint_auth_signing_alg_values_supported")
25512548
.and_then(|v| v.as_array())
2552-
{
2553-
let alg_str = signing_algorithm.as_str();
2554-
if !algs.iter().any(|a| a.as_str() == Some(alg_str)) {
2555-
let supported: Vec<&str> = algs.iter().filter_map(|a| a.as_str()).collect();
2556-
return Err(AuthError::ClientCredentialsError(format!(
2557-
"Authorization server does not support signing algorithm '{}'. Supported: {:?}",
2558-
alg_str, supported
2559-
)));
2560-
}
2549+
{
2550+
let alg_str = signing_algorithm.as_str();
2551+
if !algs.iter().any(|a| a.as_str() == Some(alg_str)) {
2552+
let supported: Vec<&str> = algs.iter().filter_map(|a| a.as_str()).collect();
2553+
return Err(AuthError::ClientCredentialsError(format!(
2554+
"Authorization server does not support signing algorithm '{}'. Supported: {:?}",
2555+
alg_str, supported
2556+
)));
25612557
}
25622558
}
25632559

0 commit comments

Comments
 (0)