-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Comments
@rustbot claim |
…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
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 let path = list.first();
let next = list.get(1);
let other = list.get(2); which is less readable |
@smmoosavi You can also write that as 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 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 🤷♂️ |
Uh oh!
There was an error while loading. Please reload this page.
What it does
Detects
slice.get(0)
and suggests replacement withslice.first()
.Lint Name
slice_get_zero
Category
style
Advantage
No magic numbers.
Drawbacks
first()
is longer thanget(0)
by 1 character.Example
Could be written as:
The text was updated successfully, but these errors were encountered: