correct suggestion for drain_collect
in a no_std
environment
This commit is contained in:
parent
cb0a479d1f
commit
d99eae4325
4 changed files with 31 additions and 3 deletions
|
@ -1,8 +1,8 @@
|
||||||
use crate::methods::DRAIN_COLLECT;
|
use crate::methods::DRAIN_COLLECT;
|
||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::is_range_full;
|
|
||||||
use clippy_utils::source::snippet;
|
use clippy_utils::source::snippet;
|
||||||
use clippy_utils::ty::is_type_lang_item;
|
use clippy_utils::ty::is_type_lang_item;
|
||||||
|
use clippy_utils::{is_range_full, std_or_core};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{Expr, ExprKind, LangItem, Path, QPath};
|
use rustc_hir::{Expr, ExprKind, LangItem, Path, QPath};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
|
@ -58,12 +58,13 @@ pub(super) fn check(cx: &LateContext<'_>, args: &[Expr<'_>], expr: &Expr<'_>, re
|
||||||
.then_some("Vec")
|
.then_some("Vec")
|
||||||
.or_else(|| check_string(cx, args, expr_ty, recv_ty_no_refs, recv_path).then_some("String"))
|
.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))
|
.or_else(|| check_collections(cx, expr_ty, recv_ty_no_refs))
|
||||||
|
&& let Some(exec_context) = std_or_core(cx)
|
||||||
{
|
{
|
||||||
let recv = snippet(cx, recv.span, "<expr>");
|
let recv = snippet(cx, recv.span, "<expr>");
|
||||||
let sugg = if let ty::Ref(..) = recv_ty.kind() {
|
let sugg = if let ty::Ref(..) = recv_ty.kind() {
|
||||||
format!("std::mem::take({recv})")
|
format!("{exec_context}::mem::take({recv})")
|
||||||
} else {
|
} else {
|
||||||
format!("std::mem::take(&mut {recv})")
|
format!("{exec_context}::mem::take(&mut {recv})")
|
||||||
};
|
};
|
||||||
|
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
|
|
8
tests/ui/drain_collect_nostd.fixed
Normal file
8
tests/ui/drain_collect_nostd.fixed
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#![warn(clippy::drain_collect)]
|
||||||
|
#![no_std]
|
||||||
|
extern crate alloc;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
|
fn remove_all(v: &mut Vec<i32>) -> Vec<i32> {
|
||||||
|
core::mem::take(v)
|
||||||
|
}
|
8
tests/ui/drain_collect_nostd.rs
Normal file
8
tests/ui/drain_collect_nostd.rs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#![warn(clippy::drain_collect)]
|
||||||
|
#![no_std]
|
||||||
|
extern crate alloc;
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
|
fn remove_all(v: &mut Vec<i32>) -> Vec<i32> {
|
||||||
|
v.drain(..).collect()
|
||||||
|
}
|
11
tests/ui/drain_collect_nostd.stderr
Normal file
11
tests/ui/drain_collect_nostd.stderr
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
error: you seem to be trying to move all elements into a new `Vec`
|
||||||
|
--> tests/ui/drain_collect_nostd.rs:7:5
|
||||||
|
|
|
||||||
|
LL | v.drain(..).collect()
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `mem::take`: `core::mem::take(v)`
|
||||||
|
|
|
||||||
|
= note: `-D clippy::drain-collect` implied by `-D warnings`
|
||||||
|
= help: to override `-D warnings` add `#[allow(clippy::drain_collect)]`
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue