summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
2a75241)
Also added test cases for internal and external macros.
- match_def_path, match_qpath, paths, snippet_with_applicability, span_help_and_lint, span_lint_and_sugg,
+ in_macro, match_def_path, match_qpath, paths, snippet_with_applicability, span_help_and_lint, span_lint_and_sugg,
};
use if_chain::if_chain;
use rustc::declare_lint_pass;
use rustc::hir::{BorrowKind, Expr, ExprKind, HirVec, Mutability, QPath};
};
use if_chain::if_chain;
use rustc::declare_lint_pass;
use rustc::hir::{BorrowKind, Expr, ExprKind, HirVec, Mutability, QPath};
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintPass};
use rustc_errors::Applicability;
use rustc_session::declare_tool_lint;
use rustc_errors::Applicability;
use rustc_session::declare_tool_lint;
}
fn check_replace_with_default(cx: &LateContext<'_, '_>, expr: &'_ Expr, args: &HirVec<Expr>) {
}
fn check_replace_with_default(cx: &LateContext<'_, '_>, expr: &'_ Expr, args: &HirVec<Expr>) {
- if let ExprKind::Call(ref repl_func, ref repl_args) = args[1].kind {
+ if let ExprKind::Call(ref repl_func, _) = args[1].kind {
- if repl_args.is_empty();
+ if !in_macro(expr.span) && !in_external_macro(cx.tcx.sess, expr.span);
if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
if match_def_path(cx, repl_def_id, &paths::DEFAULT_TRAIT_METHOD);
if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
if match_def_path(cx, repl_def_id, &paths::DEFAULT_TRAIT_METHOD);
+
+#[macro_export]
+macro_rules! take_external {
+ ($s:expr) => {
+ std::mem::replace($s, Default::default())
+ };
+}
// except according to those terms.
// run-rustfix
// except according to those terms.
// run-rustfix
+// aux-build:macro_rules.rs
#![allow(unused_imports)]
#![warn(
clippy::all,
#![allow(unused_imports)]
#![warn(
clippy::all,
clippy::mem_replace_with_default
)]
clippy::mem_replace_with_default
)]
+#[macro_use]
+extern crate macro_rules;
+
+macro_rules! take {
+ ($s:expr) => {
+ std::mem::replace($s, Default::default())
+ };
+}
+
fn replace_option_with_none() {
let mut an_option = Some(1);
let _ = an_option.take();
fn replace_option_with_none() {
let mut an_option = Some(1);
let _ = an_option.take();
let s = &mut String::from("foo");
let _ = std::mem::take(s);
let _ = std::mem::take(s);
let s = &mut String::from("foo");
let _ = std::mem::take(s);
let _ = std::mem::take(s);
+
+ // dont lint within macros
+ take!(s);
+ take_external!(s);
// except according to those terms.
// run-rustfix
// except according to those terms.
// run-rustfix
+// aux-build:macro_rules.rs
#![allow(unused_imports)]
#![warn(
clippy::all,
#![allow(unused_imports)]
#![warn(
clippy::all,
clippy::mem_replace_with_default
)]
clippy::mem_replace_with_default
)]
+#[macro_use]
+extern crate macro_rules;
+
+macro_rules! take {
+ ($s:expr) => {
+ std::mem::replace($s, Default::default())
+ };
+}
+
fn replace_option_with_none() {
let mut an_option = Some(1);
let _ = mem::replace(&mut an_option, None);
fn replace_option_with_none() {
let mut an_option = Some(1);
let _ = mem::replace(&mut an_option, None);
let s = &mut String::from("foo");
let _ = std::mem::replace(s, String::default());
let _ = std::mem::replace(s, Default::default());
let s = &mut String::from("foo");
let _ = std::mem::replace(s, String::default());
let _ = std::mem::replace(s, Default::default());
+
+ // dont lint within macros
+ take!(s);
+ take_external!(s);
error: replacing an `Option` with `None`
error: replacing an `Option` with `None`
- --> $DIR/mem_replace.rs:23:13
+ --> $DIR/mem_replace.rs:33:13
|
LL | let _ = mem::replace(&mut an_option, None);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()`
|
LL | let _ = mem::replace(&mut an_option, None);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()`
= note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings`
error: replacing an `Option` with `None`
= note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings`
error: replacing an `Option` with `None`
- --> $DIR/mem_replace.rs:25:13
+ --> $DIR/mem_replace.rs:35:13
|
LL | let _ = mem::replace(an_option, None);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()`
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
|
LL | let _ = mem::replace(an_option, None);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()`
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
- --> $DIR/mem_replace.rs:30:13
+ --> $DIR/mem_replace.rs:40:13
|
LL | let _ = std::mem::replace(&mut s, String::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)`
|
LL | let _ = std::mem::replace(&mut s, String::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)`
= note: `-D clippy::mem-replace-with-default` implied by `-D warnings`
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
= note: `-D clippy::mem-replace-with-default` implied by `-D warnings`
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
- --> $DIR/mem_replace.rs:32:13
+ --> $DIR/mem_replace.rs:42:13
|
LL | let _ = std::mem::replace(s, String::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)`
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
|
LL | let _ = std::mem::replace(s, String::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)`
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
- --> $DIR/mem_replace.rs:33:13
+ --> $DIR/mem_replace.rs:43:13
|
LL | let _ = std::mem::replace(s, Default::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)`
|
LL | let _ = std::mem::replace(s, Default::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)`