Skip to content

Commit c77bf5f

Browse files
committed
fix clippy warnings
1 parent 012a123 commit c77bf5f

11 files changed

+120
-131
lines changed

src/client/config.rs

+66-68
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{Error, Result};
22
#[cfg(feature = "tls")]
33
use native_tls::{Certificate, Identity, Protocol, TlsConnector, TlsConnectorBuilder};
4-
use std::{collections::HashMap, str::FromStr, time::Duration};
4+
use std::{collections::HashMap, fmt::{self, Display, Write}, str::FromStr, time::Duration};
55
use url::Url;
66

77
const DEFAULT_PORT: u16 = 6379;
@@ -423,48 +423,46 @@ impl Config {
423423
}
424424
}
425425

426-
impl ToString for Config {
427-
fn to_string(&self) -> String {
426+
impl Display for Config {
427+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
428428
#[cfg(feature = "tls")]
429-
let mut s = if self.tls_config.is_some() {
429+
if self.tls_config.is_some() {
430430
match &self.server {
431-
ServerConfig::Standalone { host: _, port: _ } => "rediss://",
432-
ServerConfig::Sentinel(_) => "rediss+sentinel://",
433-
ServerConfig::Cluster(_) => "rediss+cluster://",
431+
ServerConfig::Standalone { host: _, port: _ } => f.write_str("rediss://")?,
432+
ServerConfig::Sentinel(_) => f.write_str("rediss+sentinel://")?,
433+
ServerConfig::Cluster(_) => f.write_str("rediss+cluster://")?,
434434
}
435435
} else {
436436
match &self.server {
437-
ServerConfig::Standalone { host: _, port: _ } => "redis://",
438-
ServerConfig::Sentinel(_) => "redis+sentinel://",
439-
ServerConfig::Cluster(_) => "redis+cluster://",
437+
ServerConfig::Standalone { host: _, port: _ } => f.write_str("redis://")?,
438+
ServerConfig::Sentinel(_) => f.write_str("redis+sentinel://")?,
439+
ServerConfig::Cluster(_) => f.write_str("redis+cluster://")?,
440440
}
441441
}
442-
.to_owned();
443442

444443
#[cfg(not(feature = "tls"))]
445-
let mut s = match &self.server {
446-
ServerConfig::Standalone { host: _, port: _ } => "redis://",
447-
ServerConfig::Sentinel(_) => "redis+sentinel://",
448-
ServerConfig::Cluster(_) => "redis+cluster://",
444+
match &self.server {
445+
ServerConfig::Standalone { host: _, port: _ } => f.write_str("redis://")?,
446+
ServerConfig::Sentinel(_) => f.write_str("redis+sentinel://")?,
447+
ServerConfig::Cluster(_) => f.write_str("redis+cluster://")?,
449448
}
450-
.to_owned();
451449

452450
if let Some(username) = &self.username {
453-
s.push_str(username);
451+
f.write_str(username)?;
454452
}
455453

456454
if let Some(password) = &self.password {
457-
s.push(':');
458-
s.push_str(password);
459-
s.push('@');
455+
f.write_char(':')?;
456+
f.write_str(password)?;
457+
f.write_char('@')?;
460458
}
461459

462460
match &self.server {
463461
ServerConfig::Standalone { host, port } => {
464-
s.push_str(host);
462+
f.write_str(host)?;
465463
if *port != DEFAULT_PORT {
466-
s.push(':');
467-
s.push_str(&port.to_string());
464+
f.write_char(':')?;
465+
f.write_str(&port.to_string())?;
468466
}
469467
}
470468
ServerConfig::Sentinel(SentinelConfig {
@@ -474,30 +472,30 @@ impl ToString for Config {
474472
password: _,
475473
username: _,
476474
}) => {
477-
s.push_str(
475+
f.write_str(
478476
&instances
479477
.iter()
480478
.map(|(host, port)| format!("{host}:{port}"))
481479
.collect::<Vec<String>>()
482480
.join(","),
483-
);
484-
s.push('/');
485-
s.push_str(service_name);
481+
)?;
482+
f.write_char('/')?;
483+
f.write_str(service_name)?;
486484
}
487485
ServerConfig::Cluster(ClusterConfig { nodes }) => {
488-
s.push_str(
486+
f.write_str(
489487
&nodes
490488
.iter()
491489
.map(|(host, port)| format!("{host}:{port}"))
492490
.collect::<Vec<String>>()
493491
.join(","),
494-
);
492+
)?;
495493
}
496494
}
497495

498496
if self.database > 0 {
499-
s.push('/');
500-
s.push_str(&self.database.to_string());
497+
f.write_char('/')?;
498+
f.write_str(&self.database.to_string())?;
501499
}
502500

503501
// query
@@ -508,82 +506,82 @@ impl ToString for Config {
508506
if connect_timeout != DEFAULT_CONNECT_TIMEOUT {
509507
if !query_separator {
510508
query_separator = true;
511-
s.push('?');
509+
f.write_char('?')?;
512510
} else {
513-
s.push('&');
511+
f.write_char('&')?;
514512
}
515-
s.push_str(&format!("connect_timeout={connect_timeout}"));
513+
f.write_fmt(format_args!("connect_timeout={connect_timeout}"))?;
516514
}
517515

518516
let command_timeout = self.command_timeout.as_millis() as u64;
519517
if command_timeout != DEFAULT_COMMAND_TIMEOUT {
520518
if !query_separator {
521519
query_separator = true;
522-
s.push('?');
520+
f.write_char('?')?;
523521
} else {
524-
s.push('&');
522+
f.write_char('&')?;
525523
}
526-
s.push_str(&format!("command_timeout={command_timeout}"));
524+
f.write_fmt(format_args!("command_timeout={command_timeout}"))?;
527525
}
528526

529527
if self.auto_resubscribe != DEFAULT_AUTO_RESUBSCRTBE {
530528
if !query_separator {
531529
query_separator = true;
532-
s.push('?');
530+
f.write_char('?')?;
533531
} else {
534-
s.push('&');
532+
f.write_char('&')?;
535533
}
536-
s.push_str(&format!("auto_resubscribe={}", self.auto_resubscribe));
534+
f.write_fmt(format_args!("auto_resubscribe={}", self.auto_resubscribe))?;
537535
}
538536

539537
if self.auto_remonitor != DEFAULT_AUTO_REMONITOR {
540538
if !query_separator {
541539
query_separator = true;
542-
s.push('?');
540+
f.write_char('?')?;
543541
} else {
544-
s.push('&');
542+
f.write_char('&')?;
545543
}
546-
s.push_str(&format!("auto_remonitor={}", self.auto_remonitor));
544+
f.write_fmt(format_args!("auto_remonitor={}", self.auto_remonitor))?;
547545
}
548546

549547
if !self.connection_name.is_empty() {
550548
if !query_separator {
551549
query_separator = true;
552-
s.push('?');
550+
f.write_char('?')?;
553551
} else {
554-
s.push('&');
552+
f.write_char('&')?;
555553
}
556-
s.push_str(&format!("connection_name={}", self.connection_name));
554+
f.write_fmt(format_args!("connection_name={}", self.connection_name))?;
557555
}
558556

559557
if let Some(keep_alive) = self.keep_alive {
560558
if !query_separator {
561559
query_separator = true;
562-
s.push('?');
560+
f.write_char('?')?;
563561
} else {
564-
s.push('&');
562+
f.write_char('&')?;
565563
}
566-
s.push_str(&format!("keep_alive={}", keep_alive.as_millis()));
564+
f.write_fmt(format_args!("keep_alive={}", keep_alive.as_millis()))?;
567565
}
568566

569567
if self.no_delay != DEFAULT_NO_DELAY {
570568
if !query_separator {
571569
query_separator = true;
572-
s.push('?');
570+
f.write_char('?')?;
573571
} else {
574-
s.push('&');
572+
f.write_char('&')?;
575573
}
576-
s.push_str(&format!("no_delay={}", self.no_delay));
574+
f.write_fmt(format_args!("no_delay={}", self.no_delay))?;
577575
}
578576

579577
if self.retry_on_error != DEFAULT_RETRY_ON_ERROR {
580578
if !query_separator {
581579
query_separator = true;
582-
s.push('?');
580+
f.write_char('?')?;
583581
} else {
584-
s.push('&');
582+
f.write_char('&')?;
585583
}
586-
s.push_str(&format!("retry_on_error={}", self.retry_on_error));
584+
f.write_fmt(format_args!("retry_on_error={}", self.retry_on_error))?;
587585
}
588586

589587
if let ServerConfig::Sentinel(SentinelConfig {
@@ -598,34 +596,34 @@ impl ToString for Config {
598596
if wait_between_failures != DEFAULT_WAIT_BETWEEN_FAILURES {
599597
if !query_separator {
600598
query_separator = true;
601-
s.push('?');
599+
f.write_char('?')?;
602600
} else {
603-
s.push('&');
601+
f.write_char('&')?;
604602
}
605-
s.push_str(&format!("wait_between_failures={wait_between_failures}"));
603+
f.write_fmt(format_args!("wait_between_failures={wait_between_failures}"))?;
606604
}
607605
if let Some(username) = username {
608606
if !query_separator {
609607
query_separator = true;
610-
s.push('?');
608+
f.write_char('?')?;
611609
} else {
612-
s.push('&');
610+
f.write_char('&')?;
613611
}
614-
s.push_str("sentinel_username=");
615-
s.push_str(username);
612+
f.write_str("sentinel_username=")?;
613+
f.write_str(username)?;
616614
}
617615
if let Some(password) = password {
618616
if !query_separator {
619-
s.push('?');
617+
f.write_char('?')?;
620618
} else {
621-
s.push('&');
619+
f.write_char('&')?;
622620
}
623-
s.push_str("sentinel_password=");
624-
s.push_str(password);
621+
f.write_str("sentinel_password=")?;
622+
f.write_str(password)?;
625623
}
626624
}
627625

628-
s
626+
Ok(())
629627
}
630628
}
631629

@@ -880,7 +878,7 @@ pub enum ReconnectionConfig {
880878
jitter: u32,
881879
},
882880
/// Backoff reconnection attempts exponentially, multiplying the last delay by `multiplicative_factor` each time.
883-
///
881+
///
884882
/// see <https://en.wikipedia.org/wiki/Exponential_backoff>
885883
Exponential {
886884
/// Maximum number of attemps, set `0` to retry forever.

src/network/cluster_connection.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ impl ClusterConnection {
12121212
for legacy_shard in legacy_shards {
12131213
let master_id = &legacy_shard.nodes[0].id;
12141214
if master_id != &last_master_id {
1215-
last_master_id = master_id.clone();
1215+
last_master_id.clone_from(master_id);
12161216
shards.push(ClusterShardResult {
12171217
slots: vec![legacy_shard.slot],
12181218
nodes: legacy_shard

src/network/sentinel_connection.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ impl SentinelConnection {
7070
let mut unreachable_sentinel = true;
7171

7272
let mut sentinel_node_config = config.clone();
73-
sentinel_node_config.username = sentinel_config.username.clone();
74-
sentinel_node_config.password = sentinel_config.password.clone();
73+
sentinel_node_config.username.clone_from(&sentinel_config.username);
74+
sentinel_node_config.password.clone_from(&sentinel_config.password);
7575

7676
loop {
7777
for sentinel_instance in &sentinel_config.instances {

src/network/standalone_connection.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl StandaloneConnection {
132132
if kill_connection {
133133
let client_id = self.client_id().await?;
134134
let mut config = self.config.clone();
135-
config.connection_name = "killer".to_owned();
135+
"killer".clone_into(&mut config.connection_name);
136136
let mut connection =
137137
StandaloneConnection::connect(&self.host, self.port, &config).await?;
138138
connection

src/resp/resp_buf.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,17 @@ impl Deref for RespBuf {
9999
impl fmt::Display for RespBuf {
100100
#[inline]
101101
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102-
let str = match self.to::<Value>() {
102+
match self.to::<Value>() {
103103
Ok(value) => {
104-
let mut str = format!("{value:?}");
104+
let str = format!("{value:?}");
105105
if str.len() > 1000 {
106-
str = str[..1000].to_owned();
106+
f.write_str(&str[..1000])
107+
} else {
108+
f.write_str(&str)
107109
}
108-
str
109110
}
110-
Err(e) => format!("RESP buffer error: {e:?}"),
111-
};
112-
// let str = if self.0.len() > 1000 {
113-
// format!(
114-
// "{}...",
115-
// String::from_utf8_lossy(&self.0[..1000]).replace("\r\n", "\\r\\n")
116-
// )
117-
// } else {
118-
// String::from_utf8_lossy(&self.0).replace("\r\n", "\\r\\n")
119-
// };
120-
121-
f.write_str(&str)
111+
Err(e) => f.write_fmt(format_args!("RESP buffer error: {e:?}")),
112+
}
122113
}
123114
}
124115

0 commit comments

Comments
 (0)