Skip to content

Commit e019277

Browse files
authored
chore: fix clippy lints (#2351)
1 parent aa9c8cb commit e019277

File tree

13 files changed

+78
-89
lines changed

13 files changed

+78
-89
lines changed

grpc/src/client/load_balancing/mod.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl ParsedJsonLbConfig {
8989
pub fn new(json: &str) -> Result<Self, String> {
9090
match serde_json::from_str(json) {
9191
Ok(value) => Ok(ParsedJsonLbConfig { value }),
92-
Err(e) => Err(format!("failed to parse LB config JSON: {}", e)),
92+
Err(e) => Err(format!("failed to parse LB config JSON: {e}")),
9393
}
9494
}
9595

@@ -108,7 +108,7 @@ impl ParsedJsonLbConfig {
108108
let res: T = match serde_json::from_value(self.value.clone()) {
109109
Ok(v) => v,
110110
Err(e) => {
111-
return Err(format!("{}", e).into());
111+
return Err(format!("{e}").into());
112112
}
113113
};
114114
Ok(res)
@@ -209,7 +209,7 @@ impl Display for SubchannelState {
209209
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
210210
write!(f, "connectivity_state: {}", self.connectivity_state)?;
211211
if let Some(err) = &self.last_connection_error {
212-
write!(f, ", last_connection_error: {}", err)?;
212+
write!(f, ", last_connection_error: {err}")?;
213213
}
214214
Ok(())
215215
}
@@ -301,8 +301,8 @@ impl Display for PickResult {
301301
match self {
302302
Self::Pick(_) => write!(f, "Pick"),
303303
Self::Queue => write!(f, "Queue"),
304-
Self::Fail(st) => write!(f, "Fail({})", st),
305-
Self::Drop(st) => write!(f, "Drop({})", st),
304+
Self::Fail(st) => write!(f, "Fail({st})"),
305+
Self::Drop(st) => write!(f, "Drop({st})"),
306306
}
307307
}
308308
}
@@ -324,17 +324,21 @@ impl LbState {
324324
}
325325
}
326326

327+
/// Type alias for the completion callback function.
328+
pub type CompletionCallback = Box<dyn Fn(&Response) + Send + Sync>;
329+
327330
/// A collection of data used by the channel for routing a request.
328331
pub struct Pick {
329332
/// The Subchannel for the request.
330333
pub subchannel: Arc<dyn Subchannel>,
331334
// Metadata to be added to existing outgoing metadata.
332335
pub metadata: MetadataMap,
333336
// Callback to be invoked once the RPC completes.
334-
pub on_complete: Option<Box<dyn Fn(&Response) + Send + Sync>>,
337+
pub on_complete: Option<CompletionCallback>,
335338
}
336339

337340
pub trait DynHash {
341+
#[allow(clippy::redundant_allocation)]
338342
fn dyn_hash(&self, state: &mut Box<&mut dyn Hasher>);
339343
}
340344

@@ -502,7 +506,7 @@ impl Subchannel for ExternalSubchannel {
502506
}
503507

504508
fn connect(&self) {
505-
println!("connect called for subchannel: {}", self);
509+
println!("connect called for subchannel: {self}");
506510
self.isc.as_ref().unwrap().connect(false);
507511
}
508512
}
@@ -517,7 +521,7 @@ impl Drop for ExternalSubchannel {
517521
let isc = self.isc.take();
518522
let _ = self.work_scheduler.send(WorkQueueItem::Closure(Box::new(
519523
move |c: &mut InternalChannelController| {
520-
println!("unregistering connectivity state watcher for {:?}", address);
524+
println!("unregistering connectivity state watcher for {address:?}");
521525
isc.as_ref()
522526
.unwrap()
523527
.unregister_connectivity_state_watcher(watcher.unwrap());

grpc/src/client/name_resolution/backoff.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ mod tests {
125125
#[test]
126126
fn default_config_is_valid() {
127127
let result = ExponentialBackoff::new(DEFAULT_EXPONENTIAL_CONFIG.clone());
128-
assert_eq!(result.is_ok(), true);
128+
assert!(result.is_ok());
129129
}
130130

131131
#[test]
@@ -149,7 +149,7 @@ mod tests {
149149
max_delay: Duration::from_secs(10),
150150
};
151151
let result = ExponentialBackoff::new(config);
152-
assert_eq!(result.is_err(), true);
152+
assert!(result.is_err());
153153
}
154154

155155
#[test]
@@ -161,7 +161,7 @@ mod tests {
161161
max_delay: Duration::from_secs(100),
162162
};
163163
let result = ExponentialBackoff::new(config);
164-
assert_eq!(result.is_err(), true);
164+
assert!(result.is_err());
165165
}
166166

167167
#[test]
@@ -173,7 +173,7 @@ mod tests {
173173
max_delay: Duration::from_secs(100),
174174
};
175175
let result = ExponentialBackoff::new(config);
176-
assert_eq!(result.is_err(), true);
176+
assert!(result.is_err());
177177
}
178178

179179
#[test]
@@ -185,7 +185,7 @@ mod tests {
185185
max_delay: Duration::from_secs(100),
186186
};
187187
let result = ExponentialBackoff::new(config);
188-
assert_eq!(result.is_err(), true);
188+
assert!(result.is_err());
189189
}
190190

191191
#[test]
@@ -227,15 +227,15 @@ mod tests {
227227
let mut backoff = ExponentialBackoff::new(config.clone()).unwrap();
228228
// 0.8 <= duration <= 1.2.
229229
let duration = backoff.backoff_duration();
230-
assert_eq!(duration.gt(&Duration::from_secs_f64(0.8 - EPSILON)), true);
231-
assert_eq!(duration.lt(&Duration::from_secs_f64(1.2 + EPSILON)), true);
230+
assert!(duration.gt(&Duration::from_secs_f64(0.8 - EPSILON)));
231+
assert!(duration.lt(&Duration::from_secs_f64(1.2 + EPSILON)));
232232
// 1.6 <= duration <= 2.4.
233233
let duration = backoff.backoff_duration();
234-
assert_eq!(duration.gt(&Duration::from_secs_f64(1.6 - EPSILON)), true);
235-
assert_eq!(duration.lt(&Duration::from_secs_f64(2.4 + EPSILON)), true);
234+
assert!(duration.gt(&Duration::from_secs_f64(1.6 - EPSILON)));
235+
assert!(duration.lt(&Duration::from_secs_f64(2.4 + EPSILON)));
236236
// 3.2 <= duration <= 4.8.
237237
let duration = backoff.backoff_duration();
238-
assert_eq!(duration.gt(&Duration::from_secs_f64(3.2 - EPSILON)), true);
239-
assert_eq!(duration.lt(&Duration::from_secs_f64(4.8 + EPSILON)), true);
238+
assert!(duration.gt(&Duration::from_secs_f64(3.2 - EPSILON)));
239+
assert!(duration.lt(&Duration::from_secs_f64(4.8 + EPSILON)));
240240
}
241241
}

grpc/src/client/name_resolution/dns/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl DnsResolver {
162162
work_scheduler.schedule_work();
163163
channel_updated_rx.notified().await;
164164
let channel_response = { state.lock().channel_response.take() };
165-
let next_resoltion_time = if let Some(_) = channel_response {
165+
let next_resoltion_time = if channel_response.is_some() {
166166
SystemTime::now()
167167
.checked_add(backoff.backoff_duration())
168168
.unwrap()
@@ -233,7 +233,7 @@ impl ResolverBuilder for Builder {
233233

234234
fn is_valid_uri(&self, target: &Target) -> bool {
235235
if let Err(err) = parse_endpoint_and_authority(target) {
236-
eprintln!("{}", err);
236+
eprintln!("{err}");
237237
false
238238
} else {
239239
true
@@ -311,7 +311,7 @@ fn parse_endpoint_and_authority(target: &Target) -> Result<ParseResult, String>
311311
let endpoint = target.path();
312312
let endpoint = endpoint.strip_prefix("/").unwrap_or(endpoint);
313313
let parse_result = parse_host_port(endpoint, DEFAULT_PORT)
314-
.map_err(|err| format!("Failed to parse target {}: {}", target, err))?;
314+
.map_err(|err| format!("Failed to parse target {target}: {err}"))?;
315315
let endpoint = parse_result.ok_or("Received empty endpoint host.".to_string())?;
316316

317317
// Parse the authority.
@@ -323,7 +323,7 @@ fn parse_endpoint_and_authority(target: &Target) -> Result<ParseResult, String>
323323
});
324324
}
325325
let parse_result = parse_host_port(&authority, DEFAULT_DNS_PORT)
326-
.map_err(|err| format!("Failed to parse DNS authority {}: {}", target, err))?;
326+
.map_err(|err| format!("Failed to parse DNS authority {target}: {err}"))?;
327327
let Some(authority) = parse_result else {
328328
return Ok(ParseResult {
329329
endpoint,
@@ -351,7 +351,7 @@ fn parse_host_port(host_and_port: &str, default_port: u16) -> Result<Option<Host
351351
// We need to use the https scheme otherwise url::Url::parse doesn't convert
352352
// IP addresses to Host::Ipv4 or Host::Ipv6 if they could represent valid
353353
// domains.
354-
let url = format!("https://{}", host_and_port);
354+
let url = format!("https://{host_and_port}");
355355
let url = url.parse::<url::Url>().map_err(|err| err.to_string())?;
356356
let port = url.port().unwrap_or(default_port);
357357
let host = match url.host() {

grpc/src/client/name_resolution/dns/test.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub async fn dns_basic() {
207207
let mut resolver = builder.build(target, opts);
208208

209209
// Wait for schedule work to be called.
210-
let _ = work_rx.recv().await.unwrap();
210+
work_rx.recv().await.unwrap();
211211
let (update_tx, mut update_rx) = mpsc::unbounded_channel();
212212
let mut channel_controller = FakeChannelController {
213213
update_tx,
@@ -216,7 +216,7 @@ pub async fn dns_basic() {
216216
resolver.work(&mut channel_controller);
217217
// A successful endpoint update should be received.
218218
let update = update_rx.recv().await.unwrap();
219-
assert_eq!(update.endpoints.unwrap().len() > 1, true);
219+
assert!(update.endpoints.unwrap().len() > 1);
220220
}
221221

222222
#[tokio::test]
@@ -236,7 +236,7 @@ pub async fn invalid_target() {
236236
let mut resolver = builder.build(target, opts);
237237

238238
// Wait for schedule work to be called.
239-
let _ = work_rx.recv().await.unwrap();
239+
work_rx.recv().await.unwrap();
240240
let (update_tx, mut update_rx) = mpsc::unbounded_channel();
241241
let mut channel_controller = FakeChannelController {
242242
update_tx,
@@ -245,14 +245,11 @@ pub async fn invalid_target() {
245245
resolver.work(&mut channel_controller);
246246
// An error endpoint update should be received.
247247
let update = update_rx.recv().await.unwrap();
248-
assert_eq!(
249-
update
250-
.endpoints
251-
.err()
252-
.unwrap()
253-
.contains(&target.to_string()),
254-
true
255-
);
248+
assert!(update
249+
.endpoints
250+
.err()
251+
.unwrap()
252+
.contains(&target.to_string()));
256253
}
257254

258255
#[derive(Clone)]
@@ -319,7 +316,7 @@ pub async fn dns_lookup_error() {
319316
let mut resolver = builder.build(target, opts);
320317

321318
// Wait for schedule work to be called.
322-
let _ = work_rx.recv().await.unwrap();
319+
work_rx.recv().await.unwrap();
323320
let (update_tx, mut update_rx) = mpsc::unbounded_channel();
324321
let mut channel_controller = FakeChannelController {
325322
update_tx,
@@ -328,7 +325,7 @@ pub async fn dns_lookup_error() {
328325
resolver.work(&mut channel_controller);
329326
// An error endpoint update should be received.
330327
let update = update_rx.recv().await.unwrap();
331-
assert_eq!(update.endpoints.err().unwrap().contains("test_error"), true);
328+
assert!(update.endpoints.err().unwrap().contains("test_error"));
332329
}
333330

334331
#[tokio::test]
@@ -360,7 +357,7 @@ pub async fn dns_lookup_timeout() {
360357
let mut resolver = DnsResolver::new(Box::new(dns_client), opts, dns_opts);
361358

362359
// Wait for schedule work to be called.
363-
let _ = work_rx.recv().await.unwrap();
360+
work_rx.recv().await.unwrap();
364361
let (update_tx, mut update_rx) = mpsc::unbounded_channel();
365362
let mut channel_controller = FakeChannelController {
366363
update_tx,
@@ -370,7 +367,7 @@ pub async fn dns_lookup_timeout() {
370367

371368
// An error endpoint update should be received.
372369
let update = update_rx.recv().await.unwrap();
373-
assert_eq!(update.endpoints.err().unwrap().contains("Timed out"), true);
370+
assert!(update.endpoints.err().unwrap().contains("Timed out"));
374371
}
375372

376373
#[tokio::test]
@@ -398,7 +395,7 @@ pub async fn rate_limit() {
398395
let mut resolver = DnsResolver::new(dns_client, opts, dns_opts);
399396

400397
// Wait for schedule work to be called.
401-
let event = work_rx.recv().await.unwrap();
398+
work_rx.recv().await.unwrap();
402399
let (update_tx, mut update_rx) = mpsc::unbounded_channel();
403400
let mut channel_controller = FakeChannelController {
404401
update_tx,
@@ -407,14 +404,14 @@ pub async fn rate_limit() {
407404
resolver.work(&mut channel_controller);
408405
// A successful endpoint update should be received.
409406
let update = update_rx.recv().await.unwrap();
410-
assert_eq!(update.endpoints.unwrap().len() > 1, true);
407+
assert!(update.endpoints.unwrap().len() > 1);
411408

412409
// Call resolve_now repeatedly, new updates should not be produced.
413410
for _ in 0..5 {
414411
resolver.resolve_now();
415412
tokio::select! {
416413
_ = work_rx.recv() => {
417-
panic!("Received unexpected work request from resolver: {:?}", event);
414+
panic!("Received unexpected work request from resolver");
418415
}
419416
_ = tokio::time::sleep(DEFAULT_TEST_SHORT_TIMEOUT) => {
420417
println!("No work requested from resolver.");
@@ -448,7 +445,7 @@ pub async fn re_resolution_after_success() {
448445
let mut resolver = DnsResolver::new(dns_client, opts, dns_opts);
449446

450447
// Wait for schedule work to be called.
451-
let _ = work_rx.recv().await.unwrap();
448+
work_rx.recv().await.unwrap();
452449
let (update_tx, mut update_rx) = mpsc::unbounded_channel();
453450
let mut channel_controller = FakeChannelController {
454451
update_tx,
@@ -457,14 +454,14 @@ pub async fn re_resolution_after_success() {
457454
resolver.work(&mut channel_controller);
458455
// A successful endpoint update should be received.
459456
let update = update_rx.recv().await.unwrap();
460-
assert_eq!(update.endpoints.unwrap().len() > 1, true);
457+
assert!(update.endpoints.unwrap().len() > 1);
461458

462459
// Call resolve_now, a new update should be produced.
463460
resolver.resolve_now();
464-
let _ = work_rx.recv().await.unwrap();
461+
work_rx.recv().await.unwrap();
465462
resolver.work(&mut channel_controller);
466463
let update = update_rx.recv().await.unwrap();
467-
assert_eq!(update.endpoints.unwrap().len() > 1, true);
464+
assert!(update.endpoints.unwrap().len() > 1);
468465
}
469466

470467
#[tokio::test]
@@ -507,18 +504,18 @@ pub async fn backoff_on_error() {
507504
// As the channel returned an error to the resolver, the resolver will
508505
// backoff and re-attempt resolution.
509506
for _ in 0..5 {
510-
let _ = work_rx.recv().await.unwrap();
507+
work_rx.recv().await.unwrap();
511508
resolver.work(&mut channel_controller);
512509
let update = update_rx.recv().await.unwrap();
513-
assert_eq!(update.endpoints.unwrap().len() > 1, true);
510+
assert!(update.endpoints.unwrap().len() > 1);
514511
}
515512

516513
// This time the channel accepts the resolver update.
517514
channel_controller.update_result = Ok(());
518-
let _ = work_rx.recv().await.unwrap();
515+
work_rx.recv().await.unwrap();
519516
resolver.work(&mut channel_controller);
520517
let update = update_rx.recv().await.unwrap();
521-
assert_eq!(update.endpoints.unwrap().len() > 1, true);
518+
assert!(update.endpoints.unwrap().len() > 1);
522519

523520
// Since the channel controller returns Ok(), the resolver will stop
524521
// producing more updates.

grpc/src/client/name_resolution/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Target {
107107
let host = self.authority_host();
108108
let port = self.authority_port();
109109
if let Some(port) = port {
110-
format!("{}:{}", host, port)
110+
format!("{host}:{port}")
111111
} else {
112112
host.to_owned()
113113
}
@@ -319,6 +319,7 @@ impl Hash for Address {
319319
}
320320

321321
impl Display for Address {
322+
#[allow(clippy::to_string_in_format_args)]
322323
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
323324
write!(f, "{}:{}", self.network_type, self.address.to_string())
324325
}

grpc/src/client/name_resolution/registry.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ impl ResolverRegistry {
6666
let scheme = builder.scheme();
6767
if scheme.chars().any(|c| c.is_ascii_uppercase()) {
6868
return Err(format!(
69-
"Scheme must not contain uppercase characters: {}",
70-
scheme
69+
"Scheme must not contain uppercase characters: {scheme}"
7170
));
7271
}
7372
self.inner
7473
.lock()
7574
.unwrap()
7675
.insert(scheme.to_string(), Arc::from(builder));
77-
return Ok(());
76+
Ok(())
7877
}
7978

8079
/// Returns the resolver builder registered for the given scheme, if any.

0 commit comments

Comments
 (0)