[get_first]: lint on non-primitive types

This commit is contained in:
y21 2023-10-04 18:07:54 +02:00
parent b00236d7f0
commit 31fd282732
4 changed files with 32 additions and 14 deletions

View file

@ -1,5 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_slice_of_primitives;
use clippy_utils::source::snippet_with_applicability;
use if_chain::if_chain;
use rustc_ast::LitKind;
@ -20,7 +19,6 @@ pub(super) fn check<'tcx>(
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
if let Some(impl_id) = cx.tcx.impl_of_method(method_id);
if cx.tcx.type_of(impl_id).instantiate_identity().is_slice();
if let Some(_) = is_slice_of_primitives(cx, recv);
if let hir::ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = arg.kind;
then {
let mut app = Applicability::MachineApplicable;

View file

@ -14,17 +14,20 @@ impl Bar {
fn main() {
let x = vec![2, 3, 5];
let _ = x.first(); // Use x.first()
let _ = x.first();
//~^ ERROR: accessing first element with `x.get(0)`
let _ = x.get(1);
let _ = x[0];
let y = [2, 3, 5];
let _ = y.first(); // Use y.first()
let _ = y.first();
//~^ ERROR: accessing first element with `y.get(0)`
let _ = y.get(1);
let _ = y[0];
let z = &[2, 3, 5];
let _ = z.first(); // Use z.first()
let _ = z.first();
//~^ ERROR: accessing first element with `z.get(0)`
let _ = z.get(1);
let _ = z[0];
@ -37,4 +40,8 @@ fn main() {
let bar = Bar { arr: [0, 1, 2] };
let _ = bar.get(0); // Do not lint, because Bar is struct.
let non_primitives = [vec![1, 2], vec![3, 4]];
let _ = non_primitives.first();
//~^ ERROR: accessing first element with `non_primitives.get(0)`
}

View file

@ -14,17 +14,20 @@ impl Bar {
fn main() {
let x = vec![2, 3, 5];
let _ = x.get(0); // Use x.first()
let _ = x.get(0);
//~^ ERROR: accessing first element with `x.get(0)`
let _ = x.get(1);
let _ = x[0];
let y = [2, 3, 5];
let _ = y.get(0); // Use y.first()
let _ = y.get(0);
//~^ ERROR: accessing first element with `y.get(0)`
let _ = y.get(1);
let _ = y[0];
let z = &[2, 3, 5];
let _ = z.get(0); // Use z.first()
let _ = z.get(0);
//~^ ERROR: accessing first element with `z.get(0)`
let _ = z.get(1);
let _ = z[0];
@ -37,4 +40,8 @@ fn main() {
let bar = Bar { arr: [0, 1, 2] };
let _ = bar.get(0); // Do not lint, because Bar is struct.
let non_primitives = [vec![1, 2], vec![3, 4]];
let _ = non_primitives.get(0);
//~^ ERROR: accessing first element with `non_primitives.get(0)`
}

View file

@ -1,23 +1,29 @@
error: accessing first element with `x.get(0)`
--> $DIR/get_first.rs:17:13
|
LL | let _ = x.get(0); // Use x.first()
LL | let _ = x.get(0);
| ^^^^^^^^ help: try: `x.first()`
|
= note: `-D clippy::get-first` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::get_first)]`
error: accessing first element with `y.get(0)`
--> $DIR/get_first.rs:22:13
--> $DIR/get_first.rs:23:13
|
LL | let _ = y.get(0); // Use y.first()
LL | let _ = y.get(0);
| ^^^^^^^^ help: try: `y.first()`
error: accessing first element with `z.get(0)`
--> $DIR/get_first.rs:27:13
--> $DIR/get_first.rs:29:13
|
LL | let _ = z.get(0); // Use z.first()
LL | let _ = z.get(0);
| ^^^^^^^^ help: try: `z.first()`
error: aborting due to 3 previous errors
error: accessing first element with `non_primitives.get(0)`
--> $DIR/get_first.rs:45:13
|
LL | let _ = non_primitives.get(0);
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `non_primitives.first()`
error: aborting due to 4 previous errors