Skip to content

Commit 8d1f0fc

Browse files
Lint mem_replace_with_default within internal macros
1 parent 5a57bac commit 8d1f0fc

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 340 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 341 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/mem_replace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{
2-
in_macro, match_def_path, match_qpath, paths, snippet_with_applicability, span_help_and_lint, span_lint_and_sugg,
2+
match_def_path, match_qpath, paths, snippet_with_applicability, span_help_and_lint, span_lint_and_sugg,
33
};
44
use if_chain::if_chain;
55
use rustc::declare_lint_pass;
@@ -166,7 +166,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_, '_>, src: &Expr, expr_span: Sp
166166
fn check_replace_with_default(cx: &LateContext<'_, '_>, src: &Expr, dest: &Expr, expr_span: Span) {
167167
if let ExprKind::Call(ref repl_func, _) = src.kind {
168168
if_chain! {
169-
if !in_macro(expr_span) && !in_external_macro(cx.tcx.sess, expr_span);
169+
if !in_external_macro(cx.tcx.sess, expr_span);
170170
if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
171171
if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
172172
if match_def_path(cx, repl_def_id, &paths::DEFAULT_TRAIT_METHOD);

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 340] = [
9+
pub const ALL_LINTS: [Lint; 341] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",

tests/ui/mem_replace.fixed

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// except according to those terms.
99

1010
// run-rustfix
11+
// rustfix-only-machine-applicable
1112
// aux-build:macro_rules.rs
1213
#![allow(unused_imports)]
1314
#![warn(
@@ -24,7 +25,7 @@ use std::mem;
2425

2526
macro_rules! take {
2627
($s:expr) => {
27-
std::mem::replace($s, Default::default())
28+
std::mem::take(s)
2829
};
2930
}
3031

@@ -41,9 +42,9 @@ fn replace_with_default() {
4142
let s = &mut String::from("foo");
4243
let _ = std::mem::take(s);
4344
let _ = std::mem::take(s);
44-
45-
// dont lint within macros
4645
take!(s);
46+
47+
// dont lint within external macros
4748
take_external!(s);
4849
}
4950

tests/ui/mem_replace.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// except according to those terms.
99

1010
// run-rustfix
11+
// rustfix-only-machine-applicable
1112
// aux-build:macro_rules.rs
1213
#![allow(unused_imports)]
1314
#![warn(
@@ -41,9 +42,9 @@ fn replace_with_default() {
4142
let s = &mut String::from("foo");
4243
let _ = std::mem::replace(s, String::default());
4344
let _ = std::mem::replace(s, Default::default());
44-
45-
// dont lint within macros
4645
take!(s);
46+
47+
// dont lint within external macros
4748
take_external!(s);
4849
}
4950

tests/ui/mem_replace.stderr

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
11
error: replacing an `Option` with `None`
2-
--> $DIR/mem_replace.rs:33:13
2+
--> $DIR/mem_replace.rs:34:13
33
|
44
LL | let _ = mem::replace(&mut an_option, None);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()`
66
|
77
= note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings`
88

99
error: replacing an `Option` with `None`
10-
--> $DIR/mem_replace.rs:35:13
10+
--> $DIR/mem_replace.rs:36:13
1111
|
1212
LL | let _ = mem::replace(an_option, None);
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()`
1414

1515
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
16-
--> $DIR/mem_replace.rs:40:13
16+
--> $DIR/mem_replace.rs:41:13
1717
|
1818
LL | let _ = std::mem::replace(&mut s, String::default());
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)`
2020
|
2121
= note: `-D clippy::mem-replace-with-default` implied by `-D warnings`
2222

2323
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
24-
--> $DIR/mem_replace.rs:42:13
24+
--> $DIR/mem_replace.rs:43:13
2525
|
2626
LL | let _ = std::mem::replace(s, String::default());
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)`
2828

2929
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
30-
--> $DIR/mem_replace.rs:43:13
30+
--> $DIR/mem_replace.rs:44:13
3131
|
3232
LL | let _ = std::mem::replace(s, Default::default());
3333
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)`
3434

35-
error: aborting due to 5 previous errors
35+
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
36+
--> $DIR/mem_replace.rs:28:9
37+
|
38+
LL | std::mem::replace($s, Default::default())
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)`
40+
...
41+
LL | take!(s);
42+
| --------- in this macro invocation
43+
44+
error: aborting due to 6 previous errors
3645

0 commit comments

Comments
 (0)