Closed lynzrand closed 3 weeks ago
From the provided git diff
output, here are three potential issues or improvements that can be suggested:
Redundant filter
and is_empty
Check:
filter(|it| !it.is_empty())
to remove empty lines before processing the lines. This can be simplified by using the lines()
method directly, which already skips empty lines..split('\n').filter(|it| !it.is_empty())
with .lines()
.lines()
method is more idiomatic and directly provides an iterator over non-empty lines, making the code cleaner and more efficient.Shared println!
in Both Branches:
println!("{content}")
is shared in both branches of the if-else
statement. This duplication can be avoided by moving the println!
outside the if-else
statement.println!("{content}")
outside the if-else
statement and wrap the moonutil::render::MooncDiagnostic::render(content, use_fancy);
call with an if !output_json
condition.Locking catcher
Mutex:
catcher.lock().unwrap().push(content.to_owned());
line locks the mutex every time a line is processed. This can be optimized by locking the mutex once and then processing all lines within the same locked scope.Combining these suggestions, the revised code could look something like this:
let catcher = Arc::clone(&logger);
let output_json = moonbuild_opt.output_json;
let render_and_catch = move |output: &str| {
let mut locked_catcher = catcher.lock().unwrap();
output.lines().for_each(|content| {
locked_catcher.push(content.to_owned());
if output_json {
println!("{content}");
} else {
moonutil::render::MooncDiagnostic::render(content, use_fancy);
}
});
};
This approach ensures the code is more idiomatic, avoids unnecessary duplication, and potentially improves performance by reducing mutex locking overhead.
Stripping them will interfere with styling if the compiler prints anything other than error messages
Superceeds #426