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

new lint: unnecessary_indexing #14058

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

J-ZhengLi
Copy link
Member

@J-ZhengLi J-ZhengLi commented Jan 22, 2025

closes: #11858

This is a continuation of #12464 (shout out to @Jacherr for their hard work)

Since they don't have time, I thought I could push this forward by adding another commit on top of it to fix some of the addressed issues.

r? Alexendoo


changelog: add new lint [unnecessary_indexing]

Jacherr and others added 2 commits January 22, 2025 08:31
do not lint after mutable reference is taken

check path to local on conditional receiver, check mutable borrow after ref, do not lint on mutable auto borrow

fix autoborrow/mutability checks

remove unneded `extra_locals`

inline if ststements; check locality earlier

remove unnecessary impl on IndexCheckResult

check for borrows in if block and change inner `Some` based on this
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 22, 2025
/// Use instead:
/// ```no_run
/// let a: &[i32] = &[1];
/// if let Some(b) = a.first() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that you can generate even simplier expressions:

  • If a is a slice ref, use: if let [b, ..] = a
  • If a is an array, use: if let [b, ..] = &a
  • If a derefs to a slice, use: if let [b, ..] = &a[..]

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 can add it as an alternate suggestion, which one is more preferable though?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

suggest using if let Some(x) = arr.first() over if !arr.is_empty() then arr[0]
5 participants