diff --git a/rust-code-analysis-cli/src/main.rs b/rust-code-analysis-cli/src/main.rs index fed4e2456..de6b8b1c6 100644 --- a/rust-code-analysis-cli/src/main.rs +++ b/rust-code-analysis-cli/src/main.rs @@ -97,8 +97,11 @@ fn act_on_file(language: Option, path: PathBuf, cfg: &Config) -> std::io:: action::(&language, source, &path, pr, cfg) } else if cfg.metrics { if let Some(output_format) = &cfg.output_format { - let space = get_function_spaces(&language, source, &path, pr).unwrap(); - output_format.dump_formats(&space, &path, &cfg.output, cfg.pretty) + if let Some(space) = get_function_spaces(&language, source, &path, pr) { + output_format.dump_formats(&space, &path, &cfg.output, cfg.pretty) + } else { + Ok(()) + } } else { let cfg = MetricsCfg { path }; action::(&language, source, &cfg.path.clone(), pr, cfg) diff --git a/src/spaces.rs b/src/spaces.rs index 8875c49c8..ece102ac0 100644 --- a/src/spaces.rs +++ b/src/spaces.rs @@ -180,23 +180,26 @@ fn compute_halstead_and_mi<'a, T: ParserTrait>(state: &mut State<'a>) { } fn finalize<'a, T: ParserTrait>(state_stack: &mut Vec>, diff_level: usize) { + if state_stack.is_empty() { + return; + } for _ in 0..diff_level { - if state_stack.len() <= 1 { + if state_stack.len() == 1 { let mut last_state = state_stack.last_mut().unwrap(); compute_halstead_and_mi::(&mut last_state); break; - } - - let mut state = state_stack.pop().unwrap(); - compute_halstead_and_mi::(&mut state); + } else { + let mut state = state_stack.pop().unwrap(); + compute_halstead_and_mi::(&mut state); - let mut last_state = state_stack.last_mut().unwrap(); - last_state.halstead_maps.merge(&state.halstead_maps); - compute_halstead_and_mi::(&mut last_state); + let mut last_state = state_stack.last_mut().unwrap(); + last_state.halstead_maps.merge(&state.halstead_maps); + compute_halstead_and_mi::(&mut last_state); - // Merge function spaces - last_state.space.metrics.merge(&state.space.metrics); - last_state.space.spaces.push(state.space); + // Merge function spaces + last_state.space.metrics.merge(&state.space.metrics); + last_state.space.spaces.push(state.space); + } } }