Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions clippy_lints/src/mem_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,16 @@ fn check_replace_with_default(
expr.span,
"replacing a value of type `T` with `T::default()`",
|diag| {
if !expr.span.from_expansion() {
let mut applicability = Applicability::MachineApplicable;
let (dest_snip, _) = snippet_with_context(cx, dest.span, expr.span.ctxt(), "", &mut applicability);
let suggestion = format!("{top_crate}::mem::take({dest_snip})");
let mut applicability = Applicability::MachineApplicable;
let (dest_snip, _) = snippet_with_context(cx, dest.span, expr.span.ctxt(), "", &mut applicability);
let suggestion = format!("{top_crate}::mem::take({dest_snip})");

diag.span_suggestion(
expr.span,
format!("consider using `{top_crate}::mem::take` instead"),
suggestion,
applicability,
);
}
diag.span_suggestion(
expr.span,
format!("consider using `{top_crate}::mem::take` instead"),
suggestion,
applicability,
);
},
);
true
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/mem_replace_with_default.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//@aux-build:proc_macros.rs
#![warn(clippy::mem_replace_with_default)]

extern crate proc_macros;
use proc_macros::{external, inline_macros};

use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
use std::mem;

Expand Down Expand Up @@ -69,6 +73,13 @@ fn main() {
//~^ mem_replace_with_default
}

#[inline_macros]
fn macros(s: &mut String) {
let _ = inline!(std::mem::take($s));
//~^ mem_replace_with_default
let _ = external!(std::mem::replace($s, Default::default()));
}

// lint is disabled for primitives because in this case `take`
// has no clear benefit over `replace` and sometimes is harder to read
fn dont_lint_primitive() {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/mem_replace_with_default.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//@aux-build:proc_macros.rs
#![warn(clippy::mem_replace_with_default)]

extern crate proc_macros;
use proc_macros::{external, inline_macros};

use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
use std::mem;

Expand Down Expand Up @@ -69,6 +73,13 @@ fn main() {
//~^ mem_replace_with_default
}

#[inline_macros]
fn macros(s: &mut String) {
let _ = inline!(std::mem::replace($s, Default::default()));
//~^ mem_replace_with_default
let _ = external!(std::mem::replace($s, Default::default()));
}

// lint is disabled for primitives because in this case `take`
// has no clear benefit over `replace` and sometimes is harder to read
fn dont_lint_primitive() {
Expand Down
54 changes: 31 additions & 23 deletions tests/ui/mem_replace_with_default.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:8:13
--> tests/ui/mem_replace_with_default.rs:12:13
|
LL | let _ = std::mem::replace(&mut s, String::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)`
Expand All @@ -8,130 +8,138 @@ LL | let _ = std::mem::replace(&mut s, String::default());
= help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:10:13
--> tests/ui/mem_replace_with_default.rs:14:13
|
LL | let _ = std::mem::replace(&mut s, String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:14:13
--> tests/ui/mem_replace_with_default.rs:18:13
|
LL | let _ = std::mem::replace(s, String::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:16:13
--> tests/ui/mem_replace_with_default.rs:20:13
|
LL | let _ = std::mem::replace(s, String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:18:13
--> tests/ui/mem_replace_with_default.rs:22:13
|
LL | let _ = std::mem::replace(s, Default::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:22:13
--> tests/ui/mem_replace_with_default.rs:26:13
|
LL | let _ = std::mem::replace(&mut v, Vec::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:24:13
--> tests/ui/mem_replace_with_default.rs:28:13
|
LL | let _ = std::mem::replace(&mut v, Default::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:26:13
--> tests/ui/mem_replace_with_default.rs:30:13
|
LL | let _ = std::mem::replace(&mut v, Vec::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:28:13
--> tests/ui/mem_replace_with_default.rs:32:13
|
LL | let _ = std::mem::replace(&mut v, vec![]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:32:13
--> tests/ui/mem_replace_with_default.rs:36:13
|
LL | let _ = std::mem::replace(&mut hash_map, HashMap::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut hash_map)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:36:13
--> tests/ui/mem_replace_with_default.rs:40:13
|
LL | let _ = std::mem::replace(&mut btree_map, BTreeMap::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut btree_map)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:40:13
--> tests/ui/mem_replace_with_default.rs:44:13
|
LL | let _ = std::mem::replace(&mut vd, VecDeque::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut vd)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:44:13
--> tests/ui/mem_replace_with_default.rs:48:13
|
LL | let _ = std::mem::replace(&mut hash_set, HashSet::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut hash_set)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:48:13
--> tests/ui/mem_replace_with_default.rs:52:13
|
LL | let _ = std::mem::replace(&mut btree_set, BTreeSet::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut btree_set)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:52:13
--> tests/ui/mem_replace_with_default.rs:56:13
|
LL | let _ = std::mem::replace(&mut list, LinkedList::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut list)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:56:13
--> tests/ui/mem_replace_with_default.rs:60:13
|
LL | let _ = std::mem::replace(&mut binary_heap, BinaryHeap::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut binary_heap)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:60:13
--> tests/ui/mem_replace_with_default.rs:64:13
|
LL | let _ = std::mem::replace(&mut tuple, (vec![], BinaryHeap::new()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut tuple)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:64:13
--> tests/ui/mem_replace_with_default.rs:68:13
|
LL | let _ = std::mem::replace(&mut refstr, "");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut refstr)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:68:13
--> tests/ui/mem_replace_with_default.rs:72:13
|
LL | let _ = std::mem::replace(&mut slice, &[]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut slice)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:99:13
--> tests/ui/mem_replace_with_default.rs:78:21
|
LL | let _ = inline!(std::mem::replace($s, Default::default()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take($s)`
|
= note: this error originates in the macro `__inline_mac_fn_macros` (in Nightly builds, run with -Z macro-backtrace for more info)

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:110:13
|
LL | let _ = std::mem::replace(&mut s, String::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:112:13
--> tests/ui/mem_replace_with_default.rs:123:13
|
LL | let _ = std::mem::replace(&mut b.val, String::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut b.val)`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default.rs:118:20
--> tests/ui/mem_replace_with_default.rs:129:20
|
LL | let replaced = std::mem::replace(dbg!(&mut text), String::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(dbg!(&mut text))`

error: aborting due to 22 previous errors
error: aborting due to 23 previous errors

13 changes: 0 additions & 13 deletions tests/ui/mem_replace_with_default_macro.rs

This file was deleted.

12 changes: 0 additions & 12 deletions tests/ui/mem_replace_with_default_macro.stderr

This file was deleted.

11 changes: 11 additions & 0 deletions tests/ui/mem_replace_with_default_no_std.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//@aux-build:proc_macros.rs
#![warn(clippy::mem_replace_with_default)]
#![no_std]

extern crate proc_macros;
use proc_macros::{external, inline_macros};

use core::mem;

fn it_works() {
Expand All @@ -12,3 +16,10 @@ fn it_works() {
let _ = core::mem::take(&mut slice);
//~^ mem_replace_with_default
}

#[inline_macros]
fn macros(mut refstr: &str) {
let _ = inline!(core::mem::take(&mut $refstr));
//~^ mem_replace_with_default
let _ = external!(mem::replace(&mut $refstr, ""));
}
11 changes: 11 additions & 0 deletions tests/ui/mem_replace_with_default_no_std.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//@aux-build:proc_macros.rs
#![warn(clippy::mem_replace_with_default)]
#![no_std]

extern crate proc_macros;
use proc_macros::{external, inline_macros};

use core::mem;

fn it_works() {
Expand All @@ -12,3 +16,10 @@ fn it_works() {
let _ = mem::replace(&mut slice, &[]);
//~^ mem_replace_with_default
}

#[inline_macros]
fn macros(mut refstr: &str) {
let _ = inline!(mem::replace(&mut $refstr, ""));
//~^ mem_replace_with_default
let _ = external!(mem::replace(&mut $refstr, ""));
}
14 changes: 11 additions & 3 deletions tests/ui/mem_replace_with_default_no_std.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default_no_std.rs:8:13
--> tests/ui/mem_replace_with_default_no_std.rs:12:13
|
LL | let _ = mem::replace(&mut refstr, "");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut refstr)`
Expand All @@ -8,10 +8,18 @@ LL | let _ = mem::replace(&mut refstr, "");
= help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]`

error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default_no_std.rs:12:13
--> tests/ui/mem_replace_with_default_no_std.rs:16:13
|
LL | let _ = mem::replace(&mut slice, &[]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut slice)`

error: aborting due to 2 previous errors
error: replacing a value of type `T` with `T::default()`
--> tests/ui/mem_replace_with_default_no_std.rs:22:21
|
LL | let _ = inline!(mem::replace(&mut $refstr, ""));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut $refstr)`
|
= note: this error originates in the macro `__inline_mac_fn_macros` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors