Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::get_first: accessing first element with `x.get(0)`
What it meansChecks for usage of x.get(0) instead of x.first() or x.front(). It is in the style group and warns by default.
What you see
warning: accessing first element with `x.get(0)` --> main.rs:4:21 | 4 | let first_element = x.get(0); | ^^^^^^^^ help: try: `x.first()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#get_first = note: `#[warn(clippy::get_first)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Using x.first() for Vecs and slices or x.front() for VecDeques is easier to read and has the same result.
Code that works but is not idiomatic Rust. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
let x = vec![2, 3, 5];
let first_element = x.get(0);
Write this instead
let x = vec![2, 3, 5];
let first_element = x.first();
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::get_first)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
get_first = "allow"One run
cargo clippy -- -A clippy::get-firstThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for usage of x.get(0) instead of
x.first() or x.front().
Why is this bad?
Using x.first() for Vecs and slices or x.front()
for VecDeques is easier to read and has the same result.
Example
let x = vec![2, 3, 5];
let first_element = x.get(0);
Use instead:
let x = vec![2, 3, 5];
let first_element = x.first();
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- needless_returnunneeded `return` statement
- new_without_defaultChecks for public types with a pub fn new() -> Self method and no implementation of Default
- len_zeroChecks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable
- for_kv_mapChecks for iterating a map (HashMap or BTreeMap) and ignoring either the keys or values
- from_over_intoan implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
- from_str_radix_10Checks for function invocations of the form primitive::from_str_radix(s, 10)
- if_same_then_elseChecks for if/else with the same body as the then part and the else part
- implicit_saturating_addmanual saturating add detected
- implicit_saturating_subChecks for implicit saturating subtraction
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::needless_returnunneeded `return` statementRead it next →Did this answer your question?