Skip to content

Commit b54aaa3

Browse files
committed
remove unneded extra_locals
1 parent e8efc1a commit b54aaa3

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

clippy_lints/src/unnecessary_indexing.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_errors::Applicability;
1010
use rustc_hir::{Block, Expr, ExprKind, LetStmt, Node, UnOp};
1111
use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_middle::ty::adjustment::{Adjust, AutoBorrow, AutoBorrowMutability};
13-
use rustc_middle::ty::{self};
1413
use rustc_session::declare_lint_pass;
1514
use rustc_span::sym;
1615

@@ -53,20 +52,14 @@ impl LateLintPass<'_> for UnnecessaryIndexing {
5352
// check for call of is_empty
5453
&& let ExprKind::MethodCall(method, conditional_receiver, _, _) = unary_inner.kind
5554
&& method.ident.as_str() == "is_empty"
56-
&& let typeck_results = cx.typeck_results()
57-
&& let expr_ty = typeck_results.expr_ty(conditional_receiver)
55+
&& let expr_ty = cx.typeck_results().expr_ty(conditional_receiver)
5856
&& let peeled = expr_ty.peel_refs()
5957
&& (peeled.is_slice() || peeled.is_array() || is_type_diagnostic_item(cx, peeled, sym::Vec))
6058
&& let ExprKind::Block(block, _) = if_expr.then.kind
61-
{
6259
// do not lint if conditional receiver is mutable reference
63-
if let ty::Ref(_, _, Mutability::Mut) = expr_ty.kind() {
64-
return;
65-
}
66-
67-
let result = process_indexing(cx, block, conditional_receiver);
68-
69-
if let Some(r) = result
60+
&& expr_ty.ref_mutability() != Some(Mutability::Mut)
61+
{
62+
if let Some(r) = process_indexing(cx, block, conditional_receiver)
7063
&& let Some(receiver) = r.index_receiver
7164
{
7265
span_lint_and_then(
@@ -87,23 +80,26 @@ impl LateLintPass<'_> for UnnecessaryIndexing {
8780
Applicability::Unspecified,
8881
);
8982
diag.span_suggestion(first_local.span, "remove this line", "", Applicability::Unspecified);
90-
if !r.extra_locals.is_empty() {
83+
if !r.extra_exprs.is_empty() {
9184
let extra_local_suggestions = r
92-
.extra_locals
85+
.extra_exprs
9386
.iter()
94-
.map(|x| {
95-
(
96-
x.init.unwrap().span,
97-
snippet(cx, first_local.pat.span, "..").to_string(),
98-
)
87+
.filter_map(|x| {
88+
if let ExprKind::Let(l) = x.kind {
89+
Some((l.init.span, snippet(cx, first_local.pat.span, "..").to_string()))
90+
} else {
91+
None
92+
}
9993
})
10094
.collect::<Vec<_>>();
10195

102-
diag.multipart_suggestion(
103-
"initialize this variable to be the `Some` variant (may need dereferencing)",
104-
extra_local_suggestions,
105-
Applicability::Unspecified,
106-
);
96+
if !extra_local_suggestions.is_empty() {
97+
diag.multipart_suggestion(
98+
"initialize this variable to be the `Some` variant (may need dereferencing)",
99+
extra_local_suggestions,
100+
Applicability::Unspecified,
101+
);
102+
}
107103
}
108104
if !r.extra_exprs.is_empty() {
109105
let index_accesses = r
@@ -140,20 +136,17 @@ impl LateLintPass<'_> for UnnecessaryIndexing {
140136

141137
struct IndexCheckResult<'a> {
142138
// the receiver for the index operation
143-
pub index_receiver: Option<&'a Expr<'a>>,
139+
index_receiver: Option<&'a Expr<'a>>,
144140
// first local in the block - used as pattern for `Some(pat)`
145-
pub first_local: Option<&'a LetStmt<'a>>,
146-
// any other locals to be aware of, these are set to the value of `pat`
147-
pub extra_locals: Vec<&'a LetStmt<'a>>,
141+
first_local: Option<&'a LetStmt<'a>>,
148142
// any other index expressions to replace with `pat` (or "element" if no local exists)
149-
pub extra_exprs: Vec<&'a Expr<'a>>,
143+
extra_exprs: Vec<&'a Expr<'a>>,
150144
}
151145
impl<'a> IndexCheckResult<'a> {
152146
pub fn new() -> Self {
153147
IndexCheckResult {
154148
index_receiver: None,
155149
first_local: None,
156-
extra_locals: vec![],
157150
extra_exprs: vec![],
158151
}
159152
}
@@ -170,7 +163,6 @@ fn process_indexing<'a>(
170163

171164
let mut index_receiver: Option<&Expr<'_>> = None;
172165
let mut first_local: Option<&LetStmt<'_>> = None;
173-
let mut extra_locals: Vec<&LetStmt<'_>> = vec![];
174166
let mut extra_exprs: Vec<&Expr<'_>> = vec![];
175167

176168
// if res == Some(()), then mutation occurred
@@ -190,7 +182,7 @@ fn process_indexing<'a>(
190182
if first_local.is_none() {
191183
first_local = Some(local);
192184
} else {
193-
extra_locals.push(local);
185+
extra_exprs.push(x);
194186
};
195187
} else {
196188
extra_exprs.push(x);
@@ -214,7 +206,6 @@ fn process_indexing<'a>(
214206

215207
if res.is_none() {
216208
result.extra_exprs = extra_exprs;
217-
result.extra_locals = extra_locals;
218209
result.first_local = first_local;
219210
result.index_receiver = index_receiver;
220211
Some(result)

tests/ui/unnecessary_indexing.stderr

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,11 @@ help: remove this line
131131
LL - let b = a[0];
132132
LL +
133133
|
134-
help: initialize this variable to be the `Some` variant (may need dereferencing)
135-
|
136-
LL | let c = b;
137-
| ~
138134
help: set this index to be the `Some` variant (may need dereferencing)
139135
|
140-
LL | drop(b);
141-
| ~
136+
LL ~ let c = b;
137+
LL ~ drop(b);
138+
|
142139

143140
error: condition can be simplified with if..let syntax
144141
--> tests/ui/unnecessary_indexing.rs:75:5

0 commit comments

Comments
 (0)