Skip to content

Commit 8382a45

Browse files
committed
rust: fix the new clippy warnings.
Signed-off-by: Frédéric Gardes <frederic.gardes@orange.com>
1 parent 33cf85b commit 8382a45

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

rust/examples/collector/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,10 @@ mod tests {
821821

822822
#[test]
823823
fn receiver_configuration_no_custom_settings() {
824-
let mut configuration = Configuration::default();
825-
configuration.custom_settings = None;
824+
let configuration = Configuration {
825+
custom_settings: None,
826+
..Default::default()
827+
};
826828
let receiver_config = ReceiverConfiguration::try_from(&configuration).unwrap();
827829
assert_eq!(receiver_config.topic_list, vec!["#"]); // Should use default
828830
assert_eq!(receiver_config.route_level, None,);

rust/examples/json_counter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async fn main() {
7474
}
7575
total += 1;
7676

77-
if total % 1000 == 0 {
77+
if total.is_multiple_of(1000) {
7878
println!("Received {total} messages including {json} as JSON");
7979
}
8080
}

rust/src/client/configuration.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ impl Configuration {
6767
section: Option<&'static str>,
6868
key: &'static str,
6969
) -> Result<T, ConfigurationError> {
70-
if self.custom_settings.is_some() {
71-
match get_optional(section, key, self.custom_settings.as_ref().unwrap()) {
70+
if let Some(custom_settings) = &self.custom_settings {
71+
match get_optional(section, key, custom_settings) {
7272
Ok(result) => match result {
7373
Some(value) => Ok(value),
7474
_ => Err(FieldNotFound(key)),
@@ -81,14 +81,8 @@ impl Configuration {
8181
}
8282

8383
pub fn set<T: Into<String>>(&mut self, section: Option<&str>, key: &str, value: T) {
84-
if self.custom_settings.is_none() {
85-
self.custom_settings = Some(Ini::default())
86-
}
87-
self.custom_settings
88-
.as_mut()
89-
.unwrap()
90-
.with_section(section)
91-
.set(key, value);
84+
let custom_settings = self.custom_settings.get_or_insert_with(Ini::default);
85+
custom_settings.with_section(section).set(key, value);
9286
}
9387

9488
/// Get a list of values from configuration, separated by commas
@@ -97,8 +91,8 @@ impl Configuration {
9791
section: Option<&'static str>,
9892
key: &'static str,
9993
) -> Result<Vec<T>, ConfigurationError> {
100-
if self.custom_settings.is_some() {
101-
match get_optional_list(section, key, self.custom_settings.as_ref().unwrap()) {
94+
if let Some(custom_settings) = &self.custom_settings {
95+
match get_optional_list(section, key, custom_settings) {
10296
Ok(result) => match result {
10397
Some(values) => Ok(values),
10498
None => Ok(Vec::new()), // Return empty vec if not found
@@ -611,8 +605,10 @@ use_tls = false
611605

612606
#[test]
613607
fn get_list_no_custom_settings_error() {
614-
let mut configuration = Configuration::default();
615-
configuration.custom_settings = None;
608+
let configuration = Configuration {
609+
custom_settings: None,
610+
..Default::default()
611+
};
616612
let result = configuration.get_list::<String>(Some("test"), "list_field");
617613
assert!(matches!(result, Err(NoCustomSettings)));
618614
}
@@ -648,7 +644,7 @@ use_tls = false
648644
// Tests for get_optional_list functionality
649645
#[test]
650646
fn get_optional_list_ok() {
651-
let mut properties = ini::Properties::new();
647+
let mut properties = Properties::new();
652648
properties.insert("test_list", "a,b,c".to_string());
653649
let result = get_optional_list_from_properties::<String>("test_list", &properties).unwrap();
654650
assert_eq!(
@@ -659,14 +655,14 @@ use_tls = false
659655

660656
#[test]
661657
fn get_optional_list_missing_field() {
662-
let properties = ini::Properties::new();
658+
let properties = Properties::new();
663659
let result = get_optional_list_from_properties::<String>("missing", &properties).unwrap();
664660
assert!(result.is_none());
665661
}
666662

667663
#[test]
668664
fn get_optional_list_type_error() {
669-
let mut properties = ini::Properties::new();
665+
let mut properties = Properties::new();
670666
properties.insert("test_list", "not_a_number,123".to_string());
671667
let result = get_optional_list_from_properties::<u32>("test_list", &properties);
672668
assert!(result.is_err());

0 commit comments

Comments
 (0)