Skip to content

New lint: use slice.first() instead of slice.get(0) #8851

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

Closed
equal-l2 opened this issue May 20, 2022 · 3 comments · Fixed by #8882
Closed

New lint: use slice.first() instead of slice.get(0) #8851

equal-l2 opened this issue May 20, 2022 · 3 comments · Fixed by #8882
Assignees
Labels
A-lint Area: New lints

Comments

@equal-l2
Copy link

equal-l2 commented May 20, 2022

What it does

Detects slice.get(0) and suggests replacement with slice.first().

Lint Name

slice_get_zero

Category

style

Advantage

No magic numbers.

Drawbacks

first() is longer than get(0) by 1 character.

Example

let v = vec![0, 1, 2];
println!("{}", vec.get(0));

Could be written as:

let v = vec![0, 1, 2];
println!("{}", vec.first());
@equal-l2 equal-l2 added the A-lint Area: New lints label May 20, 2022
@kyoto7250
Copy link
Contributor

@rustbot claim

@bors bors closed this as completed in 3cc50a4 May 25, 2022
nars1 pushed a commit to YottaDB/YDBRust that referenced this issue Dec 29, 2023
…i/mod.rs

Background
----------

* The `rust-latest` pipeline job [started failing in the pipeline](https://gitlab.com/YottaDB/Lang/YDBRust/-/jobs/5834233436) as follows

```plaintext
Compiling yottadb v2.0.0 (/builds/YottaDB/Lang/YDBRust)
1232error: accessing first element with `self.subscripts.get(0)`
1233 --> src/simple_api/mod.rs:230:30
1234 |
1235230 | if let Some(first) = self.subscripts.get(0) {
1236 | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `self.subscripts.first()`
1237 |
1238 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#get_first
1239 = note: `-D clippy::get-first` implied by `-D clippy::all`
1240 = help: to override `-D clippy::all` add `#[allow(clippy::get_first)]`
1241error: could not compile `yottadb` (lib) due to previous error
```

Issue
-----

Since May 2020, new Rust lint rule introduced: [_use slice.first() instead of slice.get(0) #8851_](rust-lang/rust-clippy#8851 which causes Clippy to indicate error on previously accepted code

Fix
---

Subscript access changed from `.get(0)` to `.first()`, as Clippy suggested
@smmoosavi
Copy link

this rule is annoying. In this code, we need the first 3 items in the list.

let path = list.get(0);
let next = list.get(1);
let other = list.get(2);

but clippy::get_first is forcing us to change the into:

let path = list.first();
let next = list.get(1);
let other = list.get(2);

which is less readable

@y21
Copy link
Member

y21 commented Jun 26, 2024

@smmoosavi You can also write that as let [path, next, other, ..] = &*list else { /* handle the failure case */ }; which is arguably better than both, or you can just #[allow] the lint for that let statement if you don't agree or it's not applicable.

In general, I don't think there is anything that can be done about aesthetic cases like yours. That argument can be made about pretty much almost every clippy::style lint that exists. E.g.:

let x = (collection.len() == 0).then_some(foo);
let y = (collection.len() == 1).then_some(bar);
let z = (collection.len() == 2).then_some(baz);

Clippy also suggests using is_empty on only the first one, which breaks symmetry and doesn't look as nice 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants