]> git.lizzy.rs Git - rust.git/commitdiff
Prevent `mem_replace_with_default` lint within macros
authorKrishna Veera Reddy <veerareddy@email.arizona.edu>
Sun, 8 Dec 2019 19:46:21 +0000 (11:46 -0800)
committerKrishna Veera Reddy <veerareddy@email.arizona.edu>
Tue, 31 Dec 2019 17:22:34 +0000 (09:22 -0800)
Also added test cases for internal and external macros.

clippy_lints/src/mem_replace.rs
tests/ui/auxiliary/macro_rules.rs
tests/ui/mem_replace.fixed
tests/ui/mem_replace.rs
tests/ui/mem_replace.stderr

index 5ad41a53b89baa8d94b224ed772e4dd74cbba070..ab0bdb4d02c4786a57aebe2ea9fabef31c51bea1 100644 (file)
@@ -1,10 +1,10 @@
 use crate::utils::{
-    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 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;
 
@@ -163,9 +163,9 @@ fn check_replace_with_uninit(cx: &LateContext<'_, '_>, expr: &'_ Expr, args: &Hi
 }
 
 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_chain! {
-            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);
index 504d6733abfe0f5f885f06893938e1b2f5360559..eafc68bd6bcf45d7377a20555f07635f7aee7ecb 100644 (file)
@@ -39,3 +39,10 @@ macro_rules! string_add {
         let z = y + "...";
     };
 }
+
+#[macro_export]
+macro_rules! take_external {
+    ($s:expr) => {
+        std::mem::replace($s, Default::default())
+    };
+}
index 58657b934fbfeb08e74e5d80569bff16afe4a9cc..8606e98335dfe49e172b9c3cdf2481ac6c65188f 100644 (file)
@@ -8,6 +8,7 @@
 // except according to those terms.
 
 // run-rustfix
+// aux-build:macro_rules.rs
 #![allow(unused_imports)]
 #![warn(
     clippy::all,
     clippy::mem_replace_with_default
 )]
 
+#[macro_use]
+extern crate macro_rules;
+
 use std::mem;
 
+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();
@@ -31,6 +41,10 @@ fn replace_with_default() {
     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);
 }
 
 fn main() {
index eac0ce586a088836dff42cec25bd9b7d736b0607..c116107a923c687e2f44f89ce82829aaf2ee1aad 100644 (file)
@@ -8,6 +8,7 @@
 // except according to those terms.
 
 // run-rustfix
+// aux-build:macro_rules.rs
 #![allow(unused_imports)]
 #![warn(
     clippy::all,
     clippy::mem_replace_with_default
 )]
 
+#[macro_use]
+extern crate macro_rules;
+
 use std::mem;
 
+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);
@@ -31,6 +41,10 @@ fn replace_with_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);
 }
 
 fn main() {
index d5dc66873b2fe4d00ee9d329983bd5028f13cc94..9c925cefb04cb9d4b45b099369d3acb89bb377a7 100644 (file)
@@ -1,5 +1,5 @@
 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()`
@@ -7,13 +7,13 @@ LL |     let _ = mem::replace(&mut an_option, 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`
-  --> $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)`
@@ -21,13 +21,13 @@ LL |     let _ = std::mem::replace(&mut s, String::default());
    = 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`
-  --> $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)`