Skip to content

Conversation

o2sh
Copy link
Owner

@o2sh o2sh commented May 4, 2024

Fix for #1334

@o2sh o2sh added the fix label May 4, 2024
@o2sh o2sh requested a review from Byron May 4, 2024 22:29
Copy link
Collaborator

@Byron Byron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

fn get_churn_channel(
repo: &gix::Repository,
mailmap: &gix::mailmap::Snapshot,
bot_regex_pattern: &Option<MyRegex>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing &Option<T> isn't idiomatic, but Option<&T> is. That requires .as_ref() on the call-site. This is likely the work of an IDE and refactoring tools.

It's a minor detail though and one doesn't have to care, it also seems to be done in other places that aren't touched here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there perhaps a blanket AsRef implementation that can be used? Something like this?

bot_regex_pattern: impl AsRef<Option<MyRegex>>

(also perhaps MyRegex should implement AsRef<Regex>)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that adding generics to method signatures would do more than allowing to avoid manual .as_ref(), which I'd be more than happy to do for clear compiler errors.
But in the end, it's probably Ok the way it is as it's most natural for IDEs. It's surprising clippy is happy with it though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH looking at this more it is kind of odd IMO to have functions that don't do anything significant unless that option is Some.
Basically instead of this

fn foo(ref: &Option<T1>) -> T2 { if bar(ref) { do_thing() } else { do_other_thing() } } fn bar(ref: &Option<T1>) -> bool { let thing = do_calculation(); baz(thing, ref) } fn baz(thing: Thing, ref: &Option<T1>) -> bool { ref.as_ref().map_or(false, |t| t.process(thing)) }

I think it should be more like this:

fn foo(ref: &Option<T1>) -> T2 { if ref.as_ref().unwrap_or(false, bar) { do_thing() } else { do_other_thing() } } fn bar(ref: &T1) -> bool { let thing = do_calculation(); ref.process(thing) }

So, like you said, move the .as_ref call higher up in the "layers" of functions, but while we're at it these function calls could probably use some other cleanup. I think this non-idiomatic usage is partially caused by an overly complex flow of functions getting called.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Byron @spenserblack 1b682e9 Did I do it right?
There seems to be a little more cloning involved.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, as a follow up #1338

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a second follow up #1340


fn get_no_bots_regex(no_bots: &Option<Option<MyRegex>>) -> Result<Option<MyRegex>> {
let reg = if let Some(r) = no_bots.clone() {
fn get_no_bots_regex(no_bots: Option<&Option<MyRegex>>) -> Result<Option<MyRegex>> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that this is the same concern as #1335 (comment), and you'd want to make this Option<Option<&T>>.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no more Option<Option...

cf. #1340

let mut number_of_diffs_computed = 0;
while let Ok(commit_id) = rx.recv() {
let commit = repo.find_object(commit_id)?.into_commit();
if bot_regex_pattern.is_some()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra check shouldn't be needed if is_bot_commit() would exit early. I agree that all the hassle might go away if is_bot_commit() would take the regex without an Option right away.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, moved the check inside is_bot_commit

@o2sh o2sh merged commit 1775cc1 into main May 7, 2024
@o2sh o2sh deleted the fix/churn-with-no-bots branch May 7, 2024 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3 participants