Skip to content

Commit 42cab74

Browse files
committed
do not lint after mutable reference is taken
1 parent ff31e49 commit 42cab74

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

clippy_lints/src/unnecessary_indexing.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::eq_expr_value;
55
use clippy_utils::source::snippet;
66
use clippy_utils::ty::is_type_diagnostic_item;
77
use clippy_utils::visitors::for_each_expr;
8-
use rustc_ast::LitKind;
8+
use rustc_ast::{BorrowKind, LitKind, Mutability};
99
use rustc_errors::Applicability;
1010
use rustc_hir::{Expr, ExprKind, Local, Node, UnOp};
1111
use rustc_lint::{LateContext, LateLintPass};
@@ -47,8 +47,7 @@ impl LateLintPass<'_> for UnnecessaryIndexing {
4747
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) {
4848
if let Some(if_expr) = clippy_utils::higher::If::hir(expr)
4949
// check for negation
50-
&& let ExprKind::Unary(op, unary_inner) = if_expr.cond.kind
51-
&& UnOp::Not == op
50+
&& let ExprKind::Unary(UnOp::Not, unary_inner) = if_expr.cond.kind
5251
// check for call of is_empty
5352
&& let ExprKind::MethodCall(method, conditional_receiver, _, _) = unary_inner.kind
5453
&& method.ident.as_str() == "is_empty"
@@ -82,6 +81,10 @@ impl LateLintPass<'_> for UnnecessaryIndexing {
8281
} else {
8382
extra_exprs.push(x);
8483
};
84+
} else if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, val) = x.kind
85+
&& eq_expr_value(cx, conditional_receiver, val)
86+
{
87+
return ControlFlow::Break(());
8588
};
8689

8790
ControlFlow::Continue::<()>(())
@@ -105,12 +108,7 @@ impl LateLintPass<'_> for UnnecessaryIndexing {
105108
),
106109
Applicability::Unspecified,
107110
);
108-
x.span_suggestion(
109-
first_local.span,
110-
"remove this line",
111-
"",
112-
Applicability::MachineApplicable,
113-
);
111+
x.span_suggestion(first_local.span, "remove this line", "", Applicability::Unspecified);
114112
if !extra_locals.is_empty() {
115113
let extra_local_suggestions = extra_locals
116114
.iter()

tests/ui/unnecessary_indexing.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@no-rustfix
22
#![allow(unused)]
33
#![allow(dropping_copy_types)]
4+
#![allow(dropping_references)]
45
#![warn(clippy::unnecessary_indexing)]
56

67
fn c(x: i32) -> i32 {
@@ -96,4 +97,11 @@ fn main() {
9697
if a.is_empty() {
9798
let b = a[0];
9899
}
100+
101+
// dont lint if we have mutable reference
102+
let mut a: &[i32] = &[1];
103+
if !a.is_empty() {
104+
drop(&mut a);
105+
let b = a[0];
106+
}
99107
}

tests/ui/unnecessary_indexing.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: condition can be simplified with if..let syntax
2-
--> tests/ui/unnecessary_indexing.rs:22:5
2+
--> tests/ui/unnecessary_indexing.rs:23:5
33
|
44
LL | / if !a.is_empty() {
55
LL | | let b = c(a[0]);
@@ -15,7 +15,7 @@ LL ~ let b = c(element);
1515
|
1616

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

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

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

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

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

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

114114
error: condition can be simplified with if..let syntax
115-
--> tests/ui/unnecessary_indexing.rs:65:5
115+
--> tests/ui/unnecessary_indexing.rs:66:5
116116
|
117117
LL | / if !a.is_empty() {
118118
LL | | dbg!(a);
@@ -141,7 +141,7 @@ LL | drop(b);
141141
| ~
142142

143143
error: condition can be simplified with if..let syntax
144-
--> tests/ui/unnecessary_indexing.rs:74:5
144+
--> tests/ui/unnecessary_indexing.rs:75:5
145145
|
146146
LL | / if !a.is_empty() {
147147
LL | | dbg!(a);

0 commit comments

Comments
 (0)