Skip to content

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]

@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
Member

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

They're in order of preference. a is the simplest, &a is needed if a is [T] or [T; _], and &a[..] is needed for everything else.

@rustbot

This comment has been minimized.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 31, 2025
@Jarcho
Copy link
Contributor

Jarcho commented Sep 17, 2025

Ping @Alexendoo from triage. This is still waiting on a review.

Copy link
Member

@Alexendoo Alexendoo left a comment

Choose a reason for hiding this comment

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

I think also some of the points in #12464 still apply such as #12464 (comment)

View changes since this review

Comment on lines 66 to 68
let receiver = snippet(cx, receiver_span, "..");
let mut suggestions: Vec<(Span, String)> = vec![];
let mut message = "consider using `if..let` syntax instead of indexing".to_string();
Copy link
Member

Choose a reason for hiding this comment

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

These can be moved into the lint callback

Comment on lines 121 to 122
// span of the receiver for the index operation, only Some in the event the indexing is via a direct primitive
index_receiver_span: Option<Span>,
Copy link
Member

Choose a reason for hiding this comment

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

The Option here can be removed since process_indexing already returns an Option<IndexCheckResult>

@Jarcho
Copy link
Contributor

Jarcho commented Oct 1, 2025

Ping @J-ZhengLi from triage. Do you plan to return to working on this?

@J-ZhengLi
Copy link
Member Author

Ping @J-ZhengLi from triage. Do you plan to return to working on this?

yes, sorry I'm in a family vacation😃, I'll get back in a few days

Jacherr and others added 2 commits October 16, 2025 20:19
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
Copy link
Collaborator

rustbot commented Oct 16, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Copy link

Lintcheck changes for 4ddddab

Lint Added Removed Changed
clippy::unnecessary_indexing 1 0 0

This comment will be updated if you push new changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

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]

6 participants