Skip to content

Commit

Permalink
update ui tests and some minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Aug 31, 2023
1 parent b54bac9 commit 790922c
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 96 deletions.
24 changes: 11 additions & 13 deletions clippy_lints/src/missing_asserts_for_indexing.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::mem;
use std::ops::ControlFlow;

use clippy_utils::eq_expr_value;
use clippy_utils::comparisons::{normalize_comparison, Rel};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet;
use clippy_utils::{
comparisons::{normalize_comparison, Rel},
diagnostics::span_lint_and_then,
hash_expr, higher,
visitors::for_each_expr,
};
use clippy_utils::visitors::for_each_expr;
use clippy_utils::{eq_expr_value, hash_expr, higher};
use rustc_ast::{LitKind, RangeLimits};
use rustc_data_structures::unhash::UnhashMap;
use rustc_errors::{Applicability, Diagnostic};
Expand Down Expand Up @@ -220,13 +217,13 @@ fn upper_index_expr(expr: &Expr<'_>) -> Option<usize> {

/// Checks if the expression is an index into a slice and adds it to `indexes`
fn check_index<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut UnhashMap<u64, Vec<IndexEntry<'hir>>>) {
if let ExprKind::Index(slice, index_lit) = expr.kind
if let ExprKind::Index(slice, index_lit, _) = expr.kind
&& cx.typeck_results().expr_ty_adjusted(slice).peel_refs().is_slice()
&& let Some(index) = upper_index_expr(index_lit)
{
let hash = hash_expr(cx, slice);

let indexes = map.entry(hash).or_insert_with(Vec::new);
let indexes = map.entry(hash).or_default();
let entry = indexes.iter_mut().find(|entry| eq_expr_value(cx, entry.slice(), slice));

if let Some(entry) = entry {
Expand Down Expand Up @@ -261,7 +258,7 @@ fn check_index<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut Unh
fn check_assert<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut UnhashMap<u64, Vec<IndexEntry<'hir>>>) {
if let Some((comparison, asserted_len, slice)) = assert_len_expr(cx, expr) {
let hash = hash_expr(cx, slice);
let indexes = map.entry(hash).or_insert_with(Vec::new);
let indexes = map.entry(hash).or_default();

let entry = indexes.iter_mut().find(|entry| eq_expr_value(cx, entry.slice(), slice));

Expand Down Expand Up @@ -301,9 +298,10 @@ fn report_indexes(cx: &LateContext<'_>, map: &UnhashMap<u64, Vec<IndexEntry<'_>>
let Some(full_span) = entry
.index_spans()
.and_then(|spans| spans.first().zip(spans.last()))
.map(|(low, &high)| low.to(high)) else {
continue;
};
.map(|(low, &high)| low.to(high))
else {
continue;
};

match entry {
IndexEntry::AssertWithIndex {
Expand Down
20 changes: 9 additions & 11 deletions tests/ui/missing_asserts_for_indexing.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@run-rustfix

#![allow(unused)]
#![warn(clippy::missing_asserts_for_indexing)]

Expand Down Expand Up @@ -29,26 +27,26 @@ fn sum_with_assert_ge_other_way(v: &[u8]) -> u8 {

fn sum_with_assert_lt(v: &[u8]) -> u8 {
assert!(v.len() > 4);
//~^ ERROR incorrect length
v[0] + v[1] + v[2] + v[3] + v[4]
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

fn sum_with_assert_le(v: &[u8]) -> u8 {
assert!(v.len() > 4);
//~^ ERROR incorrect length
v[0] + v[1] + v[2] + v[3] + v[4]
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

fn sum_with_incorrect_assert_len(v: &[u8]) -> u8 {
assert!(v.len() > 4);
//~^ ERROR incorrect length
v[0] + v[1] + v[2] + v[3] + v[4]
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

fn sum_with_incorrect_assert_len2(v: &[u8]) -> u8 {
assert!(v.len() > 4);
//~^ ERROR incorrect length
v[0] + v[1] + v[2] + v[3] + v[4]
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

// ok, don't lint for single array access
Expand All @@ -65,8 +63,8 @@ fn subslice_ok(v: &[u8]) {

fn subslice_bad(v: &[u8]) {
assert!(v.len() > 3);
//~^ ERROR incorrect length
let _ = v[0];
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
let _ = v[1..4];
}

Expand All @@ -79,8 +77,8 @@ fn subslice_inclusive_ok(v: &[u8]) {

fn subslice_inclusive_bad(v: &[u8]) {
assert!(v.len() > 4);
//~^ ERROR incorrect length
let _ = v[0];
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
let _ = v[1..=4];
}

Expand All @@ -93,17 +91,17 @@ fn index_different_slices_ok(v1: &[u8], v2: &[u8]) {

fn index_different_slices_wrong_len(v1: &[u8], v2: &[u8]) {
assert!(v1.len() > 12);
//~^ ERROR incorrect length
assert!(v2.len() > 15);
//~^ ERROR incorrect length
let _ = v1[0] + v1[12];
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
let _ = v2[5] + v2[15];
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}
fn index_different_slices_one_wrong_len(v1: &[u8], v2: &[u8]) {
assert!(v1.len() > 12);
//~^ ERROR incorrect length
assert!(v2.len() > 15);
let _ = v1[0] + v1[12];
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
let _ = v2[5] + v2[15];
}

Expand Down
20 changes: 9 additions & 11 deletions tests/ui/missing_asserts_for_indexing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@run-rustfix

#![allow(unused)]
#![warn(clippy::missing_asserts_for_indexing)]

Expand Down Expand Up @@ -29,26 +27,26 @@ fn sum_with_assert_ge_other_way(v: &[u8]) -> u8 {

fn sum_with_assert_lt(v: &[u8]) -> u8 {
assert!(v.len() < 5);
//~^ ERROR incorrect length
v[0] + v[1] + v[2] + v[3] + v[4]
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

fn sum_with_assert_le(v: &[u8]) -> u8 {
assert!(v.len() <= 5);
//~^ ERROR incorrect length
v[0] + v[1] + v[2] + v[3] + v[4]
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

fn sum_with_incorrect_assert_len(v: &[u8]) -> u8 {
assert!(v.len() > 3);
//~^ ERROR incorrect length
v[0] + v[1] + v[2] + v[3] + v[4]
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

fn sum_with_incorrect_assert_len2(v: &[u8]) -> u8 {
assert!(v.len() >= 4);
//~^ ERROR incorrect length
v[0] + v[1] + v[2] + v[3] + v[4]
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}

// ok, don't lint for single array access
Expand All @@ -65,8 +63,8 @@ fn subslice_ok(v: &[u8]) {

fn subslice_bad(v: &[u8]) {
assert!(v.len() >= 3);
//~^ ERROR incorrect length
let _ = v[0];
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
let _ = v[1..4];
}

Expand All @@ -79,8 +77,8 @@ fn subslice_inclusive_ok(v: &[u8]) {

fn subslice_inclusive_bad(v: &[u8]) {
assert!(v.len() >= 4);
//~^ ERROR incorrect length
let _ = v[0];
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
let _ = v[1..=4];
}

Expand All @@ -93,17 +91,17 @@ fn index_different_slices_ok(v1: &[u8], v2: &[u8]) {

fn index_different_slices_wrong_len(v1: &[u8], v2: &[u8]) {
assert!(v1.len() >= 12);
//~^ ERROR incorrect length
assert!(v2.len() >= 15);
//~^ ERROR incorrect length
let _ = v1[0] + v1[12];
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
let _ = v2[5] + v2[15];
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
}
fn index_different_slices_one_wrong_len(v1: &[u8], v2: &[u8]) {
assert!(v1.len() >= 12);
//~^ ERROR incorrect length
assert!(v2.len() > 15);
let _ = v1[0] + v1[12];
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
let _ = v2[5] + v2[15];
}

Expand Down
Loading

0 comments on commit 790922c

Please sign in to comment.