Skip to content

Commit bc551b9

Browse files
committed
Do not run the lint on macro-generated code
1 parent f7356f2 commit bc551b9

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

clippy_lints/src/assigning_clones.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::macros::HirNode;
23
use clippy_utils::sugg::Sugg;
34
use clippy_utils::{is_trait_method, path_to_local};
45
use rustc_errors::Applicability;
56
use rustc_hir::{self as hir, Expr, ExprKind, Node};
67
use rustc_lint::{LateContext, LateLintPass};
7-
use rustc_middle::ty;
8-
use rustc_middle::ty::{Instance, Mutability};
8+
use rustc_middle::ty::{self, Instance, Mutability};
99
use rustc_session::declare_lint_pass;
1010
use rustc_span::def_id::DefId;
1111
use rustc_span::symbol::sym;
12+
use rustc_span::ExpnKind;
1213

1314
declare_clippy_lint! {
1415
/// ### What it does
@@ -52,6 +53,13 @@ declare_lint_pass!(AssigningClones => [ASSIGNING_CLONES]);
5253

5354
impl<'tcx> LateLintPass<'tcx> for AssigningClones {
5455
fn check_expr(&mut self, cx: &LateContext<'tcx>, assign_expr: &'tcx hir::Expr<'_>) {
56+
// Do not fire the lint in macros
57+
let expn_data = assign_expr.span().ctxt().outer_expn_data();
58+
match expn_data.kind {
59+
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) | ExpnKind::Macro(..) => return,
60+
ExpnKind::Root => {},
61+
}
62+
5563
let ExprKind::Assign(lhs, rhs, _span) = assign_expr.kind else {
5664
return;
5765
};

tests/ui/assigning_clones.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ fn ignore_generic_clone<T: Clone>(a: &mut T, b: &T) {
129129
*a = b.clone();
130130
}
131131

132+
macro_rules! clone_inside {
133+
($a:expr, $b: expr) => {
134+
$a = $b.clone();
135+
};
136+
}
137+
138+
fn clone_inside_macro() {
139+
let mut a = String::new();
140+
let b = String::new();
141+
clone_inside!(a, b);
142+
}
143+
132144
// ToOwned
133145
fn owned_method_mut_ref(mut_string: &mut String, ref_str: &str) {
134146
ref_str.clone_into(mut_string);

tests/ui/assigning_clones.rs

+12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ fn ignore_generic_clone<T: Clone>(a: &mut T, b: &T) {
129129
*a = b.clone();
130130
}
131131

132+
macro_rules! clone_inside {
133+
($a:expr, $b: expr) => {
134+
$a = $b.clone();
135+
};
136+
}
137+
138+
fn clone_inside_macro() {
139+
let mut a = String::new();
140+
let b = String::new();
141+
clone_inside!(a, b);
142+
}
143+
132144
// ToOwned
133145
fn owned_method_mut_ref(mut_string: &mut String, ref_str: &str) {
134146
*mut_string = ref_str.to_owned();

tests/ui/assigning_clones.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -68,37 +68,37 @@ LL | a = b.clone();
6868
| ^^^^^^^^^^^^^ help: use `clone_from()`: `a.clone_from(&b)`
6969

7070
error: assigning the result of `ToOwned::to_owned()` may be inefficient
71-
--> $DIR/assigning_clones.rs:134:5
71+
--> $DIR/assigning_clones.rs:146:5
7272
|
7373
LL | *mut_string = ref_str.to_owned();
7474
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(mut_string)`
7575

7676
error: assigning the result of `ToOwned::to_owned()` may be inefficient
77-
--> $DIR/assigning_clones.rs:138:5
77+
--> $DIR/assigning_clones.rs:150:5
7878
|
7979
LL | mut_string = ref_str.to_owned();
8080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut mut_string)`
8181

8282
error: assigning the result of `ToOwned::to_owned()` may be inefficient
83-
--> $DIR/assigning_clones.rs:159:5
83+
--> $DIR/assigning_clones.rs:171:5
8484
|
8585
LL | **mut_box_string = ref_str.to_owned();
8686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))`
8787

8888
error: assigning the result of `ToOwned::to_owned()` may be inefficient
89-
--> $DIR/assigning_clones.rs:163:5
89+
--> $DIR/assigning_clones.rs:175:5
9090
|
9191
LL | **mut_box_string = ref_str.to_owned();
9292
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))`
9393

9494
error: assigning the result of `ToOwned::to_owned()` may be inefficient
95-
--> $DIR/assigning_clones.rs:167:5
95+
--> $DIR/assigning_clones.rs:179:5
9696
|
9797
LL | *mut_thing = ToOwned::to_owned(ref_str);
9898
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, mut_thing)`
9999

100100
error: assigning the result of `ToOwned::to_owned()` may be inefficient
101-
--> $DIR/assigning_clones.rs:171:5
101+
--> $DIR/assigning_clones.rs:183:5
102102
|
103103
LL | mut_thing = ToOwned::to_owned(ref_str);
104104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, &mut mut_thing)`

0 commit comments

Comments
 (0)