Skip to content

Commit 627eaa7

Browse files
Lint within internal macros without a suggestion
1 parent ee7f1e3 commit 627eaa7

File tree

6 files changed

+57
-44
lines changed

6 files changed

+57
-44
lines changed

clippy_lints/src/mem_replace.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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+
in_macro, match_def_path, match_qpath, paths, snippet, snippet_with_applicability, span_help_and_lint,
3+
span_lint_and_sugg, span_lint_and_then,
34
};
45
use if_chain::if_chain;
56
use rustc::declare_lint_pass;
@@ -166,24 +167,28 @@ fn check_replace_with_uninit(cx: &LateContext<'_, '_>, src: &Expr, expr_span: Sp
166167
fn check_replace_with_default(cx: &LateContext<'_, '_>, src: &Expr, dest: &Expr, expr_span: Span) {
167168
if let ExprKind::Call(ref repl_func, _) = src.kind {
168169
if_chain! {
169-
if !in_macro(expr_span) && !in_external_macro(cx.tcx.sess, expr_span);
170+
if !in_external_macro(cx.tcx.sess, expr_span);
170171
if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
171172
if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
172173
if match_def_path(cx, repl_def_id, &paths::DEFAULT_TRAIT_METHOD);
173174
then {
174-
let mut applicability = Applicability::MachineApplicable;
175-
176-
span_lint_and_sugg(
175+
span_lint_and_then(
177176
cx,
178177
MEM_REPLACE_WITH_DEFAULT,
179178
expr_span,
180179
"replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`",
181-
"consider using",
182-
format!(
183-
"std::mem::take({})",
184-
snippet_with_applicability(cx, dest.span, "", &mut applicability)
185-
),
186-
applicability,
180+
|db| {
181+
if !in_macro(expr_span) {
182+
let suggestion = format!("std::mem::take({})", snippet(cx, dest.span, ""));
183+
184+
db.span_suggestion(
185+
expr_span,
186+
"consider using",
187+
suggestion,
188+
Applicability::MachineApplicable
189+
);
190+
}
191+
}
187192
);
188193
}
189194
}

tests/ui/mem_replace.fixed

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// except according to those terms.
99

1010
// run-rustfix
11-
// aux-build:macro_rules.rs
1211
#![allow(unused_imports)]
1312
#![warn(
1413
clippy::all,
@@ -17,17 +16,8 @@
1716
clippy::mem_replace_with_default
1817
)]
1918

20-
#[macro_use]
21-
extern crate macro_rules;
22-
2319
use std::mem;
2420

25-
macro_rules! take {
26-
($s:expr) => {
27-
std::mem::replace($s, Default::default())
28-
};
29-
}
30-
3121
fn replace_option_with_none() {
3222
let mut an_option = Some(1);
3323
let _ = an_option.take();
@@ -41,10 +31,6 @@ fn replace_with_default() {
4131
let s = &mut String::from("foo");
4232
let _ = std::mem::take(s);
4333
let _ = std::mem::take(s);
44-
45-
// dont lint within macros
46-
take!(s);
47-
take_external!(s);
4834
}
4935

5036
fn main() {

tests/ui/mem_replace.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// except according to those terms.
99

1010
// run-rustfix
11-
// aux-build:macro_rules.rs
1211
#![allow(unused_imports)]
1312
#![warn(
1413
clippy::all,
@@ -17,17 +16,8 @@
1716
clippy::mem_replace_with_default
1817
)]
1918

20-
#[macro_use]
21-
extern crate macro_rules;
22-
2319
use std::mem;
2420

25-
macro_rules! take {
26-
($s:expr) => {
27-
std::mem::replace($s, Default::default())
28-
};
29-
}
30-
3121
fn replace_option_with_none() {
3222
let mut an_option = Some(1);
3323
let _ = mem::replace(&mut an_option, None);
@@ -41,10 +31,6 @@ fn replace_with_default() {
4131
let s = &mut String::from("foo");
4232
let _ = std::mem::replace(s, String::default());
4333
let _ = std::mem::replace(s, Default::default());
44-
45-
// dont lint within macros
46-
take!(s);
47-
take_external!(s);
4834
}
4935

5036
fn main() {

tests/ui/mem_replace.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
error: replacing an `Option` with `None`
2-
--> $DIR/mem_replace.rs:33:13
2+
--> $DIR/mem_replace.rs:23: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:25: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:30: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:32: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:33:13
3131
|
3232
LL | let _ = std::mem::replace(s, Default::default());
3333
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)`

tests/ui/mem_replace_macro.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// aux-build:macro_rules.rs
2+
#![warn(clippy::mem_replace_with_default)]
3+
4+
#[macro_use]
5+
extern crate macro_rules;
6+
7+
use std::mem;
8+
9+
macro_rules! take {
10+
($s:expr) => {
11+
std::mem::replace($s, Default::default())
12+
};
13+
}
14+
15+
fn replace_with_default() {
16+
let s = &mut String::from("foo");
17+
take!(s);
18+
take_external!(s);
19+
}
20+
21+
fn main() {
22+
replace_with_default();
23+
}

tests/ui/mem_replace_macro.stderr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
2+
--> $DIR/mem_replace_macro.rs:11:9
3+
|
4+
LL | std::mem::replace($s, Default::default())
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
...
7+
LL | take!(s);
8+
| --------- in this macro invocation
9+
|
10+
= note: `-D clippy::mem-replace-with-default` implied by `-D warnings`
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)