about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJakub Beránek <berykubik@gmail.com>2024-01-23 15:30:50 +0100
committerJakub Beránek <berykubik@gmail.com>2024-03-01 16:36:05 +0100
commitbc551b9a70c93a7cd2e4c7762cff025a6c851901 (patch)
tree36fe9bfabb3969482c4c523d368c11411cfab63e
parentf7356f2a8f1c47f8358c62dceacc2386d7ae7d8e (diff)
downloadrust-bc551b9a70c93a7cd2e4c7762cff025a6c851901.tar.gz
rust-bc551b9a70c93a7cd2e4c7762cff025a6c851901.zip
Do not run the lint on macro-generated code
-rw-r--r--clippy_lints/src/assigning_clones.rs12
-rw-r--r--tests/ui/assigning_clones.fixed12
-rw-r--r--tests/ui/assigning_clones.rs12
-rw-r--r--tests/ui/assigning_clones.stderr12
4 files changed, 40 insertions, 8 deletions
diff --git a/clippy_lints/src/assigning_clones.rs b/clippy_lints/src/assigning_clones.rs
index 40c72a299c1..1a38f456dc0 100644
--- a/clippy_lints/src/assigning_clones.rs
+++ b/clippy_lints/src/assigning_clones.rs
@@ -1,14 +1,15 @@
 use clippy_utils::diagnostics::span_lint_and_then;
+use clippy_utils::macros::HirNode;
 use clippy_utils::sugg::Sugg;
 use clippy_utils::{is_trait_method, path_to_local};
 use rustc_errors::Applicability;
 use rustc_hir::{self as hir, Expr, ExprKind, Node};
 use rustc_lint::{LateContext, LateLintPass};
-use rustc_middle::ty;
-use rustc_middle::ty::{Instance, Mutability};
+use rustc_middle::ty::{self, Instance, Mutability};
 use rustc_session::declare_lint_pass;
 use rustc_span::def_id::DefId;
 use rustc_span::symbol::sym;
+use rustc_span::ExpnKind;
 
 declare_clippy_lint! {
     /// ### What it does
@@ -52,6 +53,13 @@ declare_lint_pass!(AssigningClones => [ASSIGNING_CLONES]);
 
 impl<'tcx> LateLintPass<'tcx> for AssigningClones {
     fn check_expr(&mut self, cx: &LateContext<'tcx>, assign_expr: &'tcx hir::Expr<'_>) {
+        // Do not fire the lint in macros
+        let expn_data = assign_expr.span().ctxt().outer_expn_data();
+        match expn_data.kind {
+            ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) | ExpnKind::Macro(..) => return,
+            ExpnKind::Root => {},
+        }
+
         let ExprKind::Assign(lhs, rhs, _span) = assign_expr.kind else {
             return;
         };
diff --git a/tests/ui/assigning_clones.fixed b/tests/ui/assigning_clones.fixed
index 0b42b52ee48..b417734c59d 100644
--- a/tests/ui/assigning_clones.fixed
+++ b/tests/ui/assigning_clones.fixed
@@ -129,6 +129,18 @@ fn ignore_generic_clone<T: Clone>(a: &mut T, b: &T) {
     *a = b.clone();
 }
 
+macro_rules! clone_inside {
+    ($a:expr, $b: expr) => {
+        $a = $b.clone();
+    };
+}
+
+fn clone_inside_macro() {
+    let mut a = String::new();
+    let b = String::new();
+    clone_inside!(a, b);
+}
+
 // ToOwned
 fn owned_method_mut_ref(mut_string: &mut String, ref_str: &str) {
     ref_str.clone_into(mut_string);
diff --git a/tests/ui/assigning_clones.rs b/tests/ui/assigning_clones.rs
index f1ee202c532..d5c67fd3afb 100644
--- a/tests/ui/assigning_clones.rs
+++ b/tests/ui/assigning_clones.rs
@@ -129,6 +129,18 @@ fn ignore_generic_clone<T: Clone>(a: &mut T, b: &T) {
     *a = b.clone();
 }
 
+macro_rules! clone_inside {
+    ($a:expr, $b: expr) => {
+        $a = $b.clone();
+    };
+}
+
+fn clone_inside_macro() {
+    let mut a = String::new();
+    let b = String::new();
+    clone_inside!(a, b);
+}
+
 // ToOwned
 fn owned_method_mut_ref(mut_string: &mut String, ref_str: &str) {
     *mut_string = ref_str.to_owned();
diff --git a/tests/ui/assigning_clones.stderr b/tests/ui/assigning_clones.stderr
index ad2e8857d00..93ec789cacf 100644
--- a/tests/ui/assigning_clones.stderr
+++ b/tests/ui/assigning_clones.stderr
@@ -68,37 +68,37 @@ LL |         a = b.clone();
    |         ^^^^^^^^^^^^^ help: use `clone_from()`: `a.clone_from(&b)`
 
 error: assigning the result of `ToOwned::to_owned()` may be inefficient
-  --> $DIR/assigning_clones.rs:134:5
+  --> $DIR/assigning_clones.rs:146:5
    |
 LL |     *mut_string = ref_str.to_owned();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(mut_string)`
 
 error: assigning the result of `ToOwned::to_owned()` may be inefficient
-  --> $DIR/assigning_clones.rs:138:5
+  --> $DIR/assigning_clones.rs:150:5
    |
 LL |     mut_string = ref_str.to_owned();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut mut_string)`
 
 error: assigning the result of `ToOwned::to_owned()` may be inefficient
-  --> $DIR/assigning_clones.rs:159:5
+  --> $DIR/assigning_clones.rs:171:5
    |
 LL |     **mut_box_string = ref_str.to_owned();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))`
 
 error: assigning the result of `ToOwned::to_owned()` may be inefficient
-  --> $DIR/assigning_clones.rs:163:5
+  --> $DIR/assigning_clones.rs:175:5
    |
 LL |     **mut_box_string = ref_str.to_owned();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))`
 
 error: assigning the result of `ToOwned::to_owned()` may be inefficient
-  --> $DIR/assigning_clones.rs:167:5
+  --> $DIR/assigning_clones.rs:179:5
    |
 LL |     *mut_thing = ToOwned::to_owned(ref_str);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, mut_thing)`
 
 error: assigning the result of `ToOwned::to_owned()` may be inefficient
-  --> $DIR/assigning_clones.rs:171:5
+  --> $DIR/assigning_clones.rs:183:5
    |
 LL |     mut_thing = ToOwned::to_owned(ref_str);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, &mut mut_thing)`