Skip to content

Commit

Permalink
do not lint after mutable reference is taken
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacherr committed Mar 14, 2024
1 parent ff31e49 commit 42cab74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
16 changes: 7 additions & 9 deletions clippy_lints/src/unnecessary_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clippy_utils::eq_expr_value;
use clippy_utils::source::snippet;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::visitors::for_each_expr;
use rustc_ast::LitKind;
use rustc_ast::{BorrowKind, LitKind, Mutability};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, Local, Node, UnOp};
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -47,8 +47,7 @@ impl LateLintPass<'_> for UnnecessaryIndexing {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) {
if let Some(if_expr) = clippy_utils::higher::If::hir(expr)
// check for negation
&& let ExprKind::Unary(op, unary_inner) = if_expr.cond.kind
&& UnOp::Not == op
&& let ExprKind::Unary(UnOp::Not, unary_inner) = if_expr.cond.kind
// check for call of is_empty
&& let ExprKind::MethodCall(method, conditional_receiver, _, _) = unary_inner.kind
&& method.ident.as_str() == "is_empty"
Expand Down Expand Up @@ -82,6 +81,10 @@ impl LateLintPass<'_> for UnnecessaryIndexing {
} else {
extra_exprs.push(x);
};
} else if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, val) = x.kind
&& eq_expr_value(cx, conditional_receiver, val)
{
return ControlFlow::Break(());
};

ControlFlow::Continue::<()>(())
Expand All @@ -105,12 +108,7 @@ impl LateLintPass<'_> for UnnecessaryIndexing {
),
Applicability::Unspecified,
);
x.span_suggestion(
first_local.span,
"remove this line",
"",
Applicability::MachineApplicable,
);
x.span_suggestion(first_local.span, "remove this line", "", Applicability::Unspecified);
if !extra_locals.is_empty() {
let extra_local_suggestions = extra_locals
.iter()
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/unnecessary_indexing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@no-rustfix
#![allow(unused)]
#![allow(dropping_copy_types)]
#![allow(dropping_references)]
#![warn(clippy::unnecessary_indexing)]

fn c(x: i32) -> i32 {
Expand Down Expand Up @@ -96,4 +97,11 @@ fn main() {
if a.is_empty() {
let b = a[0];
}

// dont lint if we have mutable reference
let mut a: &[i32] = &[1];
if !a.is_empty() {
drop(&mut a);
let b = a[0];
}
}
18 changes: 9 additions & 9 deletions tests/ui/unnecessary_indexing.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: condition can be simplified with if..let syntax
--> tests/ui/unnecessary_indexing.rs:22:5
--> tests/ui/unnecessary_indexing.rs:23:5
|
LL | / if !a.is_empty() {
LL | | let b = c(a[0]);
Expand All @@ -15,7 +15,7 @@ LL ~ let b = c(element);
|

error: condition can be simplified with if..let syntax
--> tests/ui/unnecessary_indexing.rs:28:5
--> tests/ui/unnecessary_indexing.rs:29:5
|
LL | / if !a.is_empty() {
LL | | let b = Struct::a(a[0]);
Expand All @@ -29,7 +29,7 @@ LL ~ let b = Struct::a(element);
|

error: condition can be simplified with if..let syntax
--> tests/ui/unnecessary_indexing.rs:34:5
--> tests/ui/unnecessary_indexing.rs:35:5
|
LL | / if !a.is_empty() {
LL | | let b = c(a[0]);
Expand All @@ -43,7 +43,7 @@ LL ~ let b = c(element);
|

error: condition can be simplified with if..let syntax
--> tests/ui/unnecessary_indexing.rs:40:5
--> tests/ui/unnecessary_indexing.rs:41:5
|
LL | / if !a.is_empty() {
LL | | let b = Struct::a(a[0]);
Expand All @@ -57,7 +57,7 @@ LL ~ let b = Struct::a(element);
|

error: condition can be simplified with if..let syntax
--> tests/ui/unnecessary_indexing.rs:46:5
--> tests/ui/unnecessary_indexing.rs:47:5
|
LL | / if !a.is_empty() {
LL | | let b = a[0];
Expand All @@ -75,7 +75,7 @@ LL +
|

error: condition can be simplified with if..let syntax
--> tests/ui/unnecessary_indexing.rs:52:5
--> tests/ui/unnecessary_indexing.rs:53:5
|
LL | / if !a.is_empty() {
LL | | let b = a[0];
Expand All @@ -93,7 +93,7 @@ LL +
|

error: condition can be simplified with if..let syntax
--> tests/ui/unnecessary_indexing.rs:58:5
--> tests/ui/unnecessary_indexing.rs:59:5
|
LL | / if !a.is_empty() {
LL | | dbg!(a);
Expand All @@ -112,7 +112,7 @@ LL +
|

error: condition can be simplified with if..let syntax
--> tests/ui/unnecessary_indexing.rs:65:5
--> tests/ui/unnecessary_indexing.rs:66:5
|
LL | / if !a.is_empty() {
LL | | dbg!(a);
Expand Down Expand Up @@ -141,7 +141,7 @@ LL | drop(b);
| ~

error: condition can be simplified with if..let syntax
--> tests/ui/unnecessary_indexing.rs:74:5
--> tests/ui/unnecessary_indexing.rs:75:5
|
LL | / if !a.is_empty() {
LL | | dbg!(a);
Expand Down

0 comments on commit 42cab74

Please sign in to comment.