Skip to content

Commit c246d5a

Browse files
Merge pull request #1731 from Yamato-Security/fix-clippy-error
fix: simplify optional value handling in multiple files
2 parents 3dedfee + 20cfc6f commit c246d5a

File tree

6 files changed

+41
-77
lines changed

6 files changed

+41
-77
lines changed

src/afterfact.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,12 @@ pub fn output_additional_afterfact(
655655
)
656656
.ok();
657657

658-
if afterfact_info.tl_starttime.is_some() {
658+
if let Some(tl_starttime) = afterfact_info.tl_starttime {
659659
output_and_data_stack_for_html(
660660
&format!(
661661
"First timestamp: {}",
662662
utils::format_time(
663-
&afterfact_info.tl_starttime.unwrap(),
663+
&tl_starttime,
664664
false,
665665
&stored_static
666666
.output_option
@@ -673,12 +673,12 @@ pub fn output_additional_afterfact(
673673
&stored_static.html_report_flag,
674674
);
675675
}
676-
if afterfact_info.tl_endtime.is_some() {
676+
if let Some(tl_endtime) = afterfact_info.tl_endtime {
677677
output_and_data_stack_for_html(
678678
&format!(
679679
"Last timestamp: {}",
680680
utils::format_time(
681-
&afterfact_info.tl_endtime.unwrap(),
681+
&tl_endtime,
682682
false,
683683
&stored_static
684684
.output_option
@@ -692,12 +692,12 @@ pub fn output_additional_afterfact(
692692
);
693693
println!();
694694
}
695-
if afterfact_info.detect_starttime.is_some() {
695+
if let Some(detect_starttime) = afterfact_info.detect_starttime {
696696
output_and_data_stack_for_html(
697697
&format!(
698698
"First detection: {}",
699699
utils::format_time(
700-
&afterfact_info.detect_starttime.unwrap(),
700+
&detect_starttime,
701701
false,
702702
&stored_static
703703
.output_option
@@ -710,12 +710,12 @@ pub fn output_additional_afterfact(
710710
&stored_static.html_report_flag,
711711
);
712712
}
713-
if afterfact_info.detect_endtime.is_some() {
713+
if let Some(detect_endtime) = afterfact_info.detect_endtime {
714714
output_and_data_stack_for_html(
715715
&format!(
716716
"Last detection: {}",
717717
utils::format_time(
718-
&afterfact_info.detect_endtime.unwrap(),
718+
&detect_endtime,
719719
false,
720720
&stored_static
721721
.output_option
@@ -891,11 +891,11 @@ pub fn output_additional_afterfact(
891891
stored_static.html_report_flag,
892892
);
893893
println!();
894-
if afterfact_info.tl_starttime.is_some() {
894+
if let Some(tl_starttime) = afterfact_info.tl_starttime {
895895
let ts = format!(
896896
"First timestamp: {}",
897897
format_time(
898-
&afterfact_info.tl_starttime.unwrap(),
898+
&tl_starttime,
899899
false,
900900
&stored_static
901901
.output_option
@@ -912,11 +912,11 @@ pub fn output_additional_afterfact(
912912
)
913913
.ok();
914914
}
915-
if afterfact_info.tl_endtime.is_some() {
915+
if let Some(tl_endtime) = afterfact_info.tl_endtime {
916916
let ts = format!(
917917
"Last timestamp: {}",
918918
format_time(
919-
&afterfact_info.tl_endtime.unwrap(),
919+
&tl_endtime,
920920
false,
921921
&stored_static
922922
.output_option
@@ -934,11 +934,11 @@ pub fn output_additional_afterfact(
934934
.ok();
935935
println!();
936936
}
937-
if afterfact_info.detect_starttime.is_some() {
937+
if let Some(detect_starttime) = afterfact_info.detect_starttime {
938938
let ts = format!(
939939
"First detection: {}",
940940
format_time(
941-
&afterfact_info.detect_starttime.unwrap(),
941+
&detect_starttime,
942942
false,
943943
&stored_static
944944
.output_option
@@ -955,11 +955,11 @@ pub fn output_additional_afterfact(
955955
)
956956
.ok();
957957
}
958-
if afterfact_info.detect_endtime.is_some() {
958+
if let Some(detect_endtime) = afterfact_info.detect_endtime {
959959
let ts = format!(
960960
"Last detection: {}",
961961
format_time(
962-
&afterfact_info.detect_endtime.unwrap(),
962+
&detect_endtime,
963963
false,
964964
&stored_static
965965
.output_option

src/detections/detection.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,18 +1158,12 @@ impl Detection {
11581158
write!(ret, "Count:{}", agg_result.data).ok();
11591159
let mut sorted_filed_values = agg_result.field_values.clone();
11601160
sorted_filed_values.sort();
1161-
if agg_condition._field_name.is_some() {
1162-
write!(
1163-
ret,
1164-
" ¦ {}:{}",
1165-
agg_condition._field_name.as_ref().unwrap(),
1166-
sorted_filed_values.join("/")
1167-
)
1168-
.ok();
1161+
if let Some(_field_name) = agg_condition._field_name.as_ref() {
1162+
write!(ret, " ¦ {}:{}", _field_name, sorted_filed_values.join("/")).ok();
11691163
}
11701164

1171-
if agg_condition._by_field_name.is_some() {
1172-
let field_name = agg_condition._by_field_name.as_ref().unwrap();
1165+
if let Some(_by_field_name) = agg_condition._by_field_name.as_ref() {
1166+
let field_name = _by_field_name;
11731167
if field_name.contains(',') {
11741168
write!(
11751169
ret,

src/detections/rule/count.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ pub fn create_count_key(
162162
eventkey_alias: &EventKeyAliasConfig,
163163
) -> String {
164164
let agg_condition = rule.get_agg_condition().unwrap();
165-
if agg_condition._by_field_name.is_some() {
166-
let by_field_key = agg_condition._by_field_name.as_ref().unwrap();
165+
if let Some(_by_field_name) = agg_condition._by_field_name.as_ref() {
166+
let by_field_key = _by_field_name;
167167
if by_field_key.contains(',') {
168168
let mut res = String::default();
169169
for key in by_field_key.split(',') {

src/main.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,9 @@ Any hostnames added to the critical_systems.txt file will have all alerts above
384384
.as_mut()
385385
.unwrap()
386386
.set_checkpoint(analysis_start_time);
387-
let target_extensions = if stored_static.output_option.is_some() {
387+
let target_extensions = if let Some(output_option) = stored_static.output_option.as_ref() {
388388
configs::get_target_extensions(
389-
stored_static
390-
.output_option
391-
.as_ref()
392-
.unwrap()
393-
.detect_common_options
394-
.evtx_file_ext
395-
.as_ref(),
389+
output_option.detect_common_options.evtx_file_ext.as_ref(),
396390
stored_static.json_input_flag,
397391
)
398392
} else {
@@ -1156,12 +1150,8 @@ Any hostnames added to the critical_systems.txt file will have all alerts above
11561150
stored_static: &mut StoredStatic,
11571151
) {
11581152
if stored_static.output_option.is_none() {
1159-
} else if stored_static
1160-
.output_option
1161-
.as_ref()
1162-
.unwrap()
1163-
.input_args
1164-
.live_analysis
1153+
} else if let Some(output_option) = stored_static.output_option.as_ref()
1154+
&& output_option.input_args.live_analysis
11651155
{
11661156
let live_analysis_list =
11671157
self.collect_liveanalysis_files(target_extensions, stored_static);
@@ -1173,12 +1163,8 @@ Any hostnames added to the critical_systems.txt file will have all alerts above
11731163
time_filter,
11741164
stored_static.borrow_mut(),
11751165
);
1176-
} else if let Some(directories) = &stored_static
1177-
.output_option
1178-
.as_ref()
1179-
.unwrap()
1180-
.input_args
1181-
.directory
1166+
} else if let Some(output_option) = &stored_static.output_option.as_ref()
1167+
&& let Some(directories) = &output_option.input_args.directory
11821168
{
11831169
let mut evtx_files = Vec::new();
11841170
for directory in directories {
@@ -1195,12 +1181,8 @@ Any hostnames added to the critical_systems.txt file will have all alerts above
11951181
self.analysis_files(evtx_files, time_filter, stored_static.borrow_mut());
11961182
} else {
11971183
// directory, live_analysis以外はfilepathの指定の場合
1198-
if let Some(filepath) = &stored_static
1199-
.output_option
1200-
.as_ref()
1201-
.unwrap()
1202-
.input_args
1203-
.filepath
1184+
if let Some(input_args) = &stored_static.output_option.as_ref()
1185+
&& let Some(filepath) = &input_args.input_args.filepath
12041186
{
12051187
let mut replaced_filepath = filepath.display().to_string();
12061188
if replaced_filepath.starts_with('"') {

src/timeline/timelines.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ impl Timeline {
120120
sammsges.push(format!("Evtx File Path: {}", self.stats.filepath));
121121
}
122122
sammsges.push(total_event_record);
123-
if self.stats.start_time.is_some() {
123+
if let Some(start_time) = self.stats.start_time {
124124
sammsges.push(format!(
125125
"First Timestamp: {}",
126126
utils::format_time(
127-
&self.stats.start_time.unwrap(),
127+
&start_time,
128128
false,
129129
&stored_static
130130
.output_option
@@ -134,11 +134,11 @@ impl Timeline {
134134
)
135135
));
136136
}
137-
if self.stats.end_time.is_some() {
137+
if let Some(end_time) = self.stats.end_time {
138138
sammsges.push(format!(
139139
"Last Timestamp: {}\n",
140140
utils::format_time(
141-
&self.stats.end_time.unwrap(),
141+
&end_time,
142142
false,
143143
&stored_static
144144
.output_option
@@ -260,11 +260,11 @@ impl Timeline {
260260
}
261261
sammsges.push(total_event_record);
262262

263-
if self.stats.start_time.is_some() {
263+
if let Some(start_time) = self.stats.start_time {
264264
sammsges.push(format!(
265265
"First Timestamp: {}",
266266
utils::format_time(
267-
&self.stats.start_time.unwrap(),
267+
&start_time,
268268
false,
269269
&stored_static
270270
.output_option
@@ -274,11 +274,11 @@ impl Timeline {
274274
)
275275
));
276276
}
277-
if self.stats.end_time.is_some() {
277+
if let Some(end_time) = self.stats.end_time {
278278
sammsges.push(format!(
279279
"Last Timestamp: {}\n",
280280
utils::format_time(
281-
&self.stats.end_time.unwrap(),
281+
&end_time,
282282
false,
283283
&stored_static
284284
.output_option

src/yaml.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -530,25 +530,13 @@ impl ParseYaml {
530530
}
531531

532532
// exclude-tag optionで指定されたtagを持つルールは除外する
533-
if stored_static.output_option.is_some()
534-
&& stored_static
535-
.output_option
536-
.as_ref()
537-
.unwrap()
538-
.exclude_tag
539-
.is_some()
533+
if let Some(opt) = stored_static.output_option.as_ref()
534+
&& let Some(exclude_tag) = opt.exclude_tag.as_ref()
540535
{
541-
let exclude_target_tags = stored_static
542-
.output_option
543-
.as_ref()
544-
.unwrap()
545-
.exclude_tag
546-
.as_ref()
547-
.unwrap();
548536
let rule_tags_vec = yaml_doc["tags"].as_vec();
549537
if let Some(rule_tags) = rule_tags_vec {
550538
let is_match = rule_tags.iter().any(|tag| {
551-
exclude_target_tags.contains(&tag.as_str().unwrap_or_default().to_string())
539+
exclude_tag.contains(&tag.as_str().unwrap_or_default().to_string())
552540
});
553541
if is_match {
554542
up_rule_load_cnt("excluded");

0 commit comments

Comments
 (0)