Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply some minor changes to unnecessary-list-index-lookup #8932

Merged
merged 1 commit into from
Dec 1, 2023

Conversation

charliermarsh
Copy link
Member

Summary

I was late in reviewing this but found a few things I wanted to tweak. No functional changes.

/// It is more succinct to use the variable for the value at the current index which is already in scope from the iterator.
/// When iterating over a list with `enumerate`, the current item is already
/// available alongside its index. Using the index to look up the item is
/// unnecessary.
Copy link
Member Author

Choose a reason for hiding this comment

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

I try to wrap these at 80 characters so that they render reasonably in the terminal.

checker.diagnostics.push(diagnostic);
}
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer to put the rule implementations up top, and helper functions at the bottom. That way, each file reads as (1) diagnostic definition, then (2) rule body.

}
_ => false,
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I made this an associated method.

false
}
_ => false,
});
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a repeat of is_assignment.

match call_path.as_slice() {
["", "enumerate"] => (),
["builtins", "enumerate"] => (),
_ => return None,
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I rewrote this as:

    // Check that the function is the `enumerate` builtin.
    if !semantic
        .resolve_call_path(func.as_ref())
        .is_some_and(|call_path| matches!(call_path.as_slice(), ["builtins" | "", "enumerate"]))
    {
        return None;
    }

Note, however, that you could also do let call_path = checker.semantic().resolve_call_path(func.as_ref())? here, which is more idiomatic than let-else with a return None.


/// PLR1736
pub(crate) fn unnecessary_list_index_lookup_comprehension(checker: &mut Checker, expr: &Expr) {
match expr {
Copy link
Member Author

Choose a reason for hiding this comment

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

For a single branch match, I tend to prefer let-else or if-let.

call_expr: &'a Expr,
tuple_expr: &'a Expr,
semantic: &SemanticModel,
) -> Option<(&'a str, &'a str, &'a str)> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Able to avoid the clone that was here before by using a lifetime.

@charliermarsh charliermarsh added the internal An internal refactor or improvement label Dec 1, 2023
@charliermarsh charliermarsh enabled auto-merge (squash) December 1, 2023 00:43
@diceroll123
Copy link
Contributor

I'll learn from all this, thanks! 😄

@charliermarsh
Copy link
Member Author

Of course! Sorry that it took it getting merged for me to actually prioritize reviewing, that's a bad habit 😬

@charliermarsh charliermarsh merged commit c1dc4a6 into main Dec 1, 2023
16 checks passed
@charliermarsh charliermarsh deleted the charlie/cleanup branch December 1, 2023 00:53
Copy link
Contributor

github-actions bot commented Dec 1, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants