rust-lang / rust-lang/rust-clippy

`get_first_mut` lint for element access using zero index

Open
#11,768 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Similar to the get_first lint but for mutable elements.
When retrieving the first element using get_mut(0) suggest using first_mut(), or front_mut() in case of VecDeque.

I'm not sure if this should be an extension of the existing get_first lint or should get implemented as a separate lint.
If this is considered a useful extension, I can gladly implement it.

Advantage
  • Usage of provided functions
  • The intend of the code is easier to understand
Drawbacks

No response

Example
let mut v: Vec<i32> = Vec::from([1, 2, 3]);
let _ = v.get_mut(0);

let mut d: VecDeque<i32> = VecDeque::from([1, 2, 3]);
let _ = d.get_mut(0);

Could be written as:

let mut v: Vec<i32> = Vec::from([1, 2, 3]);
let _ = v.first_mut();

let mut d: VecDeque<i32> = VecDeque::from([1, 2, 3]);
let _ = d.front_mut();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by inspecting the existing get_first lint and compare its behavior with the get_mut(0) examples for Vec and VecDeque. Add coverage for suggesting first_mut() and front_mut(), and confirm the lint output matches the requested transformations.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.