-
Notifications
You must be signed in to change notification settings - Fork 1.9k
new lint: drain_collect
#10835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
new lint: drain_collect
#10835
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2748ab9
new lint: `drain_collect`
y21 d2a6ec2
take into account reborrowing when inserting `&mut` in sugg
y21 3f3657a
make clippy happy
y21 20ae597
add a description
y21 5821fbb
add test case for not whole length, move sugg into variable
y21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| use crate::methods::DRAIN_COLLECT; | ||
| use clippy_utils::diagnostics::span_lint_and_sugg; | ||
| use clippy_utils::is_range_full; | ||
| use clippy_utils::source::snippet; | ||
| use clippy_utils::ty::is_type_lang_item; | ||
| use rustc_errors::Applicability; | ||
| use rustc_hir::Expr; | ||
| use rustc_hir::ExprKind; | ||
| use rustc_hir::LangItem; | ||
| use rustc_hir::Path; | ||
| use rustc_hir::QPath; | ||
| use rustc_lint::LateContext; | ||
| use rustc_middle::query::Key; | ||
| use rustc_middle::ty; | ||
| use rustc_middle::ty::Ty; | ||
| use rustc_span::sym; | ||
| use rustc_span::Symbol; | ||
|
|
||
| /// Checks if both types match the given diagnostic item, e.g.: | ||
| /// | ||
| /// `vec![1,2].drain(..).collect::<Vec<_>>()` | ||
| /// ^^^^^^^^^ ^^^^^^ true | ||
| /// `vec![1,2].drain(..).collect::<HashSet<_>>()` | ||
| /// ^^^^^^^^^ ^^^^^^^^^^ false | ||
| fn types_match_diagnostic_item(cx: &LateContext<'_>, expr: Ty<'_>, recv: Ty<'_>, sym: Symbol) -> bool { | ||
| if let Some(expr_adt_did) = expr.ty_adt_id() | ||
| && let Some(recv_adt_did) = recv.ty_adt_id() | ||
| { | ||
| cx.tcx.is_diagnostic_item(sym, expr_adt_did) && cx.tcx.is_diagnostic_item(sym, recv_adt_did) | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| /// Checks `std::{vec::Vec, collections::VecDeque}`. | ||
| fn check_vec(cx: &LateContext<'_>, args: &[Expr<'_>], expr: Ty<'_>, recv: Ty<'_>, recv_path: &Path<'_>) -> bool { | ||
| (types_match_diagnostic_item(cx, expr, recv, sym::Vec) | ||
| || types_match_diagnostic_item(cx, expr, recv, sym::VecDeque)) | ||
| && matches!(args, [arg] if is_range_full(cx, arg, Some(recv_path))) | ||
| } | ||
|
|
||
| /// Checks `std::string::String` | ||
| fn check_string(cx: &LateContext<'_>, args: &[Expr<'_>], expr: Ty<'_>, recv: Ty<'_>, recv_path: &Path<'_>) -> bool { | ||
| is_type_lang_item(cx, expr, LangItem::String) | ||
| && is_type_lang_item(cx, recv, LangItem::String) | ||
| && matches!(args, [arg] if is_range_full(cx, arg, Some(recv_path))) | ||
| } | ||
|
|
||
| /// Checks `std::collections::{HashSet, HashMap, BinaryHeap}`. | ||
| fn check_collections(cx: &LateContext<'_>, expr: Ty<'_>, recv: Ty<'_>) -> Option<&'static str> { | ||
| types_match_diagnostic_item(cx, expr, recv, sym::HashSet) | ||
| .then_some("HashSet") | ||
| .or_else(|| types_match_diagnostic_item(cx, expr, recv, sym::HashMap).then_some("HashMap")) | ||
| .or_else(|| types_match_diagnostic_item(cx, expr, recv, sym::BinaryHeap).then_some("BinaryHeap")) | ||
| } | ||
|
|
||
| pub(super) fn check(cx: &LateContext<'_>, args: &[Expr<'_>], expr: &Expr<'_>, recv: &Expr<'_>) { | ||
| let expr_ty = cx.typeck_results().expr_ty(expr); | ||
| let recv_ty = cx.typeck_results().expr_ty(recv); | ||
| let recv_ty_no_refs = recv_ty.peel_refs(); | ||
|
|
||
| if let ExprKind::Path(QPath::Resolved(_, recv_path)) = recv.kind | ||
| && let Some(typename) = check_vec(cx, args, expr_ty, recv_ty_no_refs, recv_path) | ||
| .then_some("Vec") | ||
| .or_else(|| check_string(cx, args, expr_ty, recv_ty_no_refs, recv_path).then_some("String")) | ||
| .or_else(|| check_collections(cx, expr_ty, recv_ty_no_refs)) | ||
| { | ||
| let recv = snippet(cx, recv.span, "<expr>"); | ||
| let sugg = if let ty::Ref(..) = recv_ty.kind() { | ||
| format!("std::mem::take({recv})") | ||
| } else { | ||
| format!("std::mem::take(&mut {recv})") | ||
| }; | ||
|
|
||
| span_lint_and_sugg( | ||
| cx, | ||
| DRAIN_COLLECT, | ||
| expr.span, | ||
| &format!("you seem to be trying to move all elements into a new `{typename}`"), | ||
| "consider using `mem::take`", | ||
| sugg, | ||
| Applicability::MachineApplicable, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| //@run-rustfix | ||
|
|
||
| #![deny(clippy::drain_collect)] | ||
| #![allow(dead_code)] | ||
|
|
||
| use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque}; | ||
|
|
||
| fn binaryheap(b: &mut BinaryHeap<i32>) -> BinaryHeap<i32> { | ||
| std::mem::take(b) | ||
| } | ||
|
|
||
| fn binaryheap_dont_lint(b: &mut BinaryHeap<i32>) -> HashSet<i32> { | ||
| b.drain().collect() | ||
| } | ||
|
|
||
| fn hashmap(b: &mut HashMap<i32, i32>) -> HashMap<i32, i32> { | ||
| std::mem::take(b) | ||
| } | ||
|
|
||
| fn hashmap_dont_lint(b: &mut HashMap<i32, i32>) -> Vec<(i32, i32)> { | ||
| b.drain().collect() | ||
| } | ||
|
|
||
| fn hashset(b: &mut HashSet<i32>) -> HashSet<i32> { | ||
| std::mem::take(b) | ||
| } | ||
|
|
||
| fn hashset_dont_lint(b: &mut HashSet<i32>) -> Vec<i32> { | ||
| b.drain().collect() | ||
| } | ||
|
|
||
| fn vecdeque(b: &mut VecDeque<i32>) -> VecDeque<i32> { | ||
| std::mem::take(b) | ||
| } | ||
|
|
||
| fn vecdeque_dont_lint(b: &mut VecDeque<i32>) -> HashSet<i32> { | ||
| b.drain(..).collect() | ||
| } | ||
|
|
||
| fn vec(b: &mut Vec<i32>) -> Vec<i32> { | ||
| std::mem::take(b) | ||
| } | ||
|
|
||
| fn vec2(b: &mut Vec<i32>) -> Vec<i32> { | ||
| std::mem::take(b) | ||
| } | ||
|
|
||
| fn vec3(b: &mut Vec<i32>) -> Vec<i32> { | ||
| std::mem::take(b) | ||
| } | ||
|
|
||
| fn vec4(b: &mut Vec<i32>) -> Vec<i32> { | ||
| std::mem::take(b) | ||
| } | ||
|
|
||
| fn vec_no_reborrow() -> Vec<i32> { | ||
| let mut b = vec![1, 2, 3]; | ||
| std::mem::take(&mut b) | ||
| } | ||
|
|
||
| fn vec_dont_lint(b: &mut Vec<i32>) -> HashSet<i32> { | ||
| b.drain(..).collect() | ||
| } | ||
|
|
||
| fn string(b: &mut String) -> String { | ||
| std::mem::take(b) | ||
| } | ||
|
|
||
| fn string_dont_lint(b: &mut String) -> HashSet<char> { | ||
| b.drain(..).collect() | ||
| } | ||
|
|
||
| fn not_whole_length(v: &mut Vec<i32>) -> Vec<i32> { | ||
| v.drain(1..).collect() | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| //@run-rustfix | ||
|
|
||
| #![deny(clippy::drain_collect)] | ||
| #![allow(dead_code)] | ||
|
|
||
| use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque}; | ||
|
|
||
| fn binaryheap(b: &mut BinaryHeap<i32>) -> BinaryHeap<i32> { | ||
| b.drain().collect() | ||
| } | ||
|
|
||
| fn binaryheap_dont_lint(b: &mut BinaryHeap<i32>) -> HashSet<i32> { | ||
| b.drain().collect() | ||
| } | ||
|
|
||
| fn hashmap(b: &mut HashMap<i32, i32>) -> HashMap<i32, i32> { | ||
| b.drain().collect() | ||
| } | ||
|
|
||
| fn hashmap_dont_lint(b: &mut HashMap<i32, i32>) -> Vec<(i32, i32)> { | ||
| b.drain().collect() | ||
| } | ||
|
|
||
| fn hashset(b: &mut HashSet<i32>) -> HashSet<i32> { | ||
| b.drain().collect() | ||
| } | ||
|
|
||
| fn hashset_dont_lint(b: &mut HashSet<i32>) -> Vec<i32> { | ||
| b.drain().collect() | ||
| } | ||
|
|
||
| fn vecdeque(b: &mut VecDeque<i32>) -> VecDeque<i32> { | ||
| b.drain(..).collect() | ||
| } | ||
|
|
||
| fn vecdeque_dont_lint(b: &mut VecDeque<i32>) -> HashSet<i32> { | ||
| b.drain(..).collect() | ||
| } | ||
|
|
||
| fn vec(b: &mut Vec<i32>) -> Vec<i32> { | ||
| b.drain(..).collect() | ||
| } | ||
|
|
||
| fn vec2(b: &mut Vec<i32>) -> Vec<i32> { | ||
| b.drain(0..).collect() | ||
| } | ||
|
|
||
| fn vec3(b: &mut Vec<i32>) -> Vec<i32> { | ||
| b.drain(..b.len()).collect() | ||
| } | ||
|
|
||
| fn vec4(b: &mut Vec<i32>) -> Vec<i32> { | ||
| b.drain(0..b.len()).collect() | ||
| } | ||
|
|
||
| fn vec_no_reborrow() -> Vec<i32> { | ||
| let mut b = vec![1, 2, 3]; | ||
| b.drain(..).collect() | ||
| } | ||
|
|
||
| fn vec_dont_lint(b: &mut Vec<i32>) -> HashSet<i32> { | ||
| b.drain(..).collect() | ||
| } | ||
|
|
||
| fn string(b: &mut String) -> String { | ||
| b.drain(..).collect() | ||
| } | ||
|
|
||
| fn string_dont_lint(b: &mut String) -> HashSet<char> { | ||
| b.drain(..).collect() | ||
| } | ||
|
|
||
| fn not_whole_length(v: &mut Vec<i32>) -> Vec<i32> { | ||
| v.drain(1..).collect() | ||
| } | ||
|
|
||
| fn main() {} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| error: you seem to be trying to move all elements into a new `BinaryHeap` | ||
| --> $DIR/drain_collect.rs:9:5 | ||
| | | ||
| LL | b.drain().collect() | ||
| | ^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `std::mem::take(b)` | ||
| | | ||
| note: the lint level is defined here | ||
| --> $DIR/drain_collect.rs:3:9 | ||
| | | ||
| LL | #![deny(clippy::drain_collect)] | ||
| | ^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: you seem to be trying to move all elements into a new `HashMap` | ||
| --> $DIR/drain_collect.rs:17:5 | ||
| | | ||
| LL | b.drain().collect() | ||
| | ^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `std::mem::take(b)` | ||
|
|
||
| error: you seem to be trying to move all elements into a new `HashSet` | ||
| --> $DIR/drain_collect.rs:25:5 | ||
| | | ||
| LL | b.drain().collect() | ||
| | ^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `std::mem::take(b)` | ||
|
|
||
| error: you seem to be trying to move all elements into a new `Vec` | ||
| --> $DIR/drain_collect.rs:33:5 | ||
| | | ||
| LL | b.drain(..).collect() | ||
| | ^^^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `std::mem::take(b)` | ||
|
|
||
| error: you seem to be trying to move all elements into a new `Vec` | ||
| --> $DIR/drain_collect.rs:41:5 | ||
| | | ||
| LL | b.drain(..).collect() | ||
| | ^^^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `std::mem::take(b)` | ||
|
|
||
| error: you seem to be trying to move all elements into a new `Vec` | ||
| --> $DIR/drain_collect.rs:45:5 | ||
| | | ||
| LL | b.drain(0..).collect() | ||
| | ^^^^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `std::mem::take(b)` | ||
|
|
||
| error: you seem to be trying to move all elements into a new `Vec` | ||
| --> $DIR/drain_collect.rs:49:5 | ||
| | | ||
| LL | b.drain(..b.len()).collect() | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `std::mem::take(b)` | ||
|
|
||
| error: you seem to be trying to move all elements into a new `Vec` | ||
| --> $DIR/drain_collect.rs:53:5 | ||
| | | ||
| LL | b.drain(0..b.len()).collect() | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `std::mem::take(b)` | ||
|
|
||
| error: you seem to be trying to move all elements into a new `Vec` | ||
| --> $DIR/drain_collect.rs:58:5 | ||
| | | ||
| LL | b.drain(..).collect() | ||
| | ^^^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `std::mem::take(&mut b)` | ||
|
|
||
| error: you seem to be trying to move all elements into a new `String` | ||
| --> $DIR/drain_collect.rs:66:5 | ||
| | | ||
| LL | b.drain(..).collect() | ||
| | ^^^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `std::mem::take(b)` | ||
|
|
||
| error: aborting due to 10 previous errors | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.