Auto merge of #12452 - CBSpeir:dedup-manual-retain, r=blyxyas

[`manual_retain`]: Fix duplicate diagnostics

Relates to: #12379

The first lint guard executed in `LateLintPass::check_expr` was testing if the parent was of type `ExprKind::Assign`.  This meant the lint emitted on both sides of the assignment operator when `check_expr` is called on either `Expr`.  The guard in the fix only lints once when the `Expr` is of kind `Assign`.

changelog:  Fix duplicate lint diagnostic emission from [`manual_retain`]
This commit is contained in:
bors 2024-03-11 22:21:27 +00:00
commit e22ca03627
4 changed files with 43 additions and 48 deletions

View File

@ -2,7 +2,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
use clippy_utils::{get_parent_expr, match_def_path, paths, SpanlessEq};
use clippy_utils::{match_def_path, paths, SpanlessEq};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
@ -69,17 +69,16 @@ pub fn new(msrv: Msrv) -> Self {
impl<'tcx> LateLintPass<'tcx> for ManualRetain {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
if let Some(parent_expr) = get_parent_expr(cx, expr)
&& let Assign(left_expr, collect_expr, _) = &parent_expr.kind
if let Assign(left_expr, collect_expr, _) = &expr.kind
&& let hir::ExprKind::MethodCall(seg, ..) = &collect_expr.kind
&& seg.args.is_none()
&& let hir::ExprKind::MethodCall(_, target_expr, [], _) = &collect_expr.kind
&& let Some(collect_def_id) = cx.typeck_results().type_dependent_def_id(collect_expr.hir_id)
&& cx.tcx.is_diagnostic_item(sym::iterator_collect_fn, collect_def_id)
{
check_into_iter(cx, left_expr, target_expr, parent_expr.span, &self.msrv);
check_iter(cx, left_expr, target_expr, parent_expr.span, &self.msrv);
check_to_owned(cx, left_expr, target_expr, parent_expr.span, &self.msrv);
check_into_iter(cx, left_expr, target_expr, expr.span, &self.msrv);
check_iter(cx, left_expr, target_expr, expr.span, &self.msrv);
check_to_owned(cx, left_expr, target_expr, expr.span, &self.msrv);
}
}

View File

@ -1,5 +1,3 @@
//@compile-flags: -Zdeduplicate-diagnostics=yes
#![warn(clippy::manual_retain)]
#![allow(unused, clippy::redundant_clone)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

View File

@ -1,5 +1,3 @@
//@compile-flags: -Zdeduplicate-diagnostics=yes
#![warn(clippy::manual_retain)]
#![allow(unused, clippy::redundant_clone)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

View File

@ -1,5 +1,5 @@
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:27:5
--> tests/ui/manual_retain.rs:25:5
|
LL | binary_heap = binary_heap.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `binary_heap.retain(|x| x % 2 == 0)`
@ -8,43 +8,43 @@ LL | binary_heap = binary_heap.into_iter().filter(|x| x % 2 == 0).collect();
= help: to override `-D warnings` add `#[allow(clippy::manual_retain)]`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:28:5
--> tests/ui/manual_retain.rs:26:5
|
LL | binary_heap = binary_heap.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `binary_heap.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:29:5
--> tests/ui/manual_retain.rs:27:5
|
LL | binary_heap = binary_heap.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `binary_heap.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:33:5
--> tests/ui/manual_retain.rs:31:5
|
LL | tuples = tuples.iter().filter(|(ref x, ref y)| *x == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `tuples.retain(|(ref x, ref y)| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:34:5
--> tests/ui/manual_retain.rs:32:5
|
LL | tuples = tuples.iter().filter(|(x, y)| *x == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `tuples.retain(|(x, y)| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:64:5
--> tests/ui/manual_retain.rs:62:5
|
LL | btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|k, _| k % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:65:5
--> tests/ui/manual_retain.rs:63:5
|
LL | btree_map = btree_map.into_iter().filter(|(_, v)| v % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|_, &mut v| v % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:66:5
--> tests/ui/manual_retain.rs:64:5
|
LL | / btree_map = btree_map
LL | | .into_iter()
@ -53,49 +53,49 @@ LL | | .collect();
| |__________________^ help: consider calling `.retain()` instead: `btree_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0))`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:91:5
--> tests/ui/manual_retain.rs:89:5
|
LL | btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:92:5
--> tests/ui/manual_retain.rs:90:5
|
LL | btree_set = btree_set.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:93:5
--> tests/ui/manual_retain.rs:91:5
|
LL | btree_set = btree_set.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:97:5
--> tests/ui/manual_retain.rs:95:5
|
LL | tuples = tuples.iter().filter(|(ref x, ref y)| *x == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `tuples.retain(|(ref x, ref y)| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:98:5
--> tests/ui/manual_retain.rs:96:5
|
LL | tuples = tuples.iter().filter(|(x, y)| *x == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `tuples.retain(|(x, y)| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:128:5
--> tests/ui/manual_retain.rs:126:5
|
LL | hash_map = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_map.retain(|k, _| k % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:129:5
--> tests/ui/manual_retain.rs:127:5
|
LL | hash_map = hash_map.into_iter().filter(|(_, v)| v % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_map.retain(|_, &mut v| v % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:130:5
--> tests/ui/manual_retain.rs:128:5
|
LL | / hash_map = hash_map
LL | | .into_iter()
@ -104,133 +104,133 @@ LL | | .collect();
| |__________________^ help: consider calling `.retain()` instead: `hash_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0))`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:154:5
--> tests/ui/manual_retain.rs:152:5
|
LL | hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:155:5
--> tests/ui/manual_retain.rs:153:5
|
LL | hash_set = hash_set.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:156:5
--> tests/ui/manual_retain.rs:154:5
|
LL | hash_set = hash_set.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:160:5
--> tests/ui/manual_retain.rs:158:5
|
LL | tuples = tuples.iter().filter(|(ref x, ref y)| *x == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `tuples.retain(|(ref x, ref y)| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:161:5
--> tests/ui/manual_retain.rs:159:5
|
LL | tuples = tuples.iter().filter(|(x, y)| *x == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `tuples.retain(|(x, y)| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:190:5
--> tests/ui/manual_retain.rs:188:5
|
LL | s = s.chars().filter(|&c| c != 'o').to_owned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `s.retain(|c| c != 'o')`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:202:5
--> tests/ui/manual_retain.rs:200:5
|
LL | vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:203:5
--> tests/ui/manual_retain.rs:201:5
|
LL | vec = vec.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:204:5
--> tests/ui/manual_retain.rs:202:5
|
LL | vec = vec.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:208:5
--> tests/ui/manual_retain.rs:206:5
|
LL | tuples = tuples.iter().filter(|(ref x, ref y)| *x == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `tuples.retain(|(ref x, ref y)| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:209:5
--> tests/ui/manual_retain.rs:207:5
|
LL | tuples = tuples.iter().filter(|(x, y)| *x == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `tuples.retain(|(x, y)| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:231:5
--> tests/ui/manual_retain.rs:229:5
|
LL | vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:232:5
--> tests/ui/manual_retain.rs:230:5
|
LL | vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:233:5
--> tests/ui/manual_retain.rs:231:5
|
LL | vec_deque = vec_deque.into_iter().filter(|x| x % 2 == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:290:5
--> tests/ui/manual_retain.rs:288:5
|
LL | vec = vec.into_iter().filter(|(x, y)| *x == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|(x, y)| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:294:5
--> tests/ui/manual_retain.rs:292:5
|
LL | tuples = tuples.into_iter().filter(|(_, n)| *n > 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `tuples.retain(|(_, n)| *n > 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:311:5
--> tests/ui/manual_retain.rs:309:5
|
LL | vec = vec.iter().filter(|&&x| x == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|&x| x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:312:5
--> tests/ui/manual_retain.rs:310:5
|
LL | vec = vec.iter().filter(|&&x| x == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|&x| x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:313:5
--> tests/ui/manual_retain.rs:311:5
|
LL | vec = vec.into_iter().filter(|&x| x == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|&x| x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:316:5
--> tests/ui/manual_retain.rs:314:5
|
LL | vec = vec.iter().filter(|&x| *x == 0).copied().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:317:5
--> tests/ui/manual_retain.rs:315:5
|
LL | vec = vec.iter().filter(|&x| *x == 0).cloned().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| *x == 0)`
error: this expression can be written more simply using `.retain()`
--> tests/ui/manual_retain.rs:318:5
--> tests/ui/manual_retain.rs:316:5
|
LL | vec = vec.into_iter().filter(|x| *x == 0).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| *x == 0)`