Do not run the lint on macro-generated code

This commit is contained in:
Jakub Beránek 2024-01-23 15:30:50 +01:00
parent f7356f2a8f
commit bc551b9a70
No known key found for this signature in database
GPG key ID: 909CD0D26483516B
4 changed files with 40 additions and 8 deletions

View file

@ -1,14 +1,15 @@
use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::HirNode;
use clippy_utils::sugg::Sugg; use clippy_utils::sugg::Sugg;
use clippy_utils::{is_trait_method, path_to_local}; use clippy_utils::{is_trait_method, path_to_local};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{self as hir, Expr, ExprKind, Node}; use rustc_hir::{self as hir, Expr, ExprKind, Node};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty; use rustc_middle::ty::{self, Instance, Mutability};
use rustc_middle::ty::{Instance, Mutability};
use rustc_session::declare_lint_pass; use rustc_session::declare_lint_pass;
use rustc_span::def_id::DefId; use rustc_span::def_id::DefId;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use rustc_span::ExpnKind;
declare_clippy_lint! { declare_clippy_lint! {
/// ### What it does /// ### What it does
@ -52,6 +53,13 @@ declare_lint_pass!(AssigningClones => [ASSIGNING_CLONES]);
impl<'tcx> LateLintPass<'tcx> for AssigningClones { impl<'tcx> LateLintPass<'tcx> for AssigningClones {
fn check_expr(&mut self, cx: &LateContext<'tcx>, assign_expr: &'tcx hir::Expr<'_>) { 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 { let ExprKind::Assign(lhs, rhs, _span) = assign_expr.kind else {
return; return;
}; };

View file

@ -129,6 +129,18 @@ fn ignore_generic_clone<T: Clone>(a: &mut T, b: &T) {
*a = b.clone(); *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 // ToOwned
fn owned_method_mut_ref(mut_string: &mut String, ref_str: &str) { fn owned_method_mut_ref(mut_string: &mut String, ref_str: &str) {
ref_str.clone_into(mut_string); ref_str.clone_into(mut_string);

View file

@ -129,6 +129,18 @@ fn ignore_generic_clone<T: Clone>(a: &mut T, b: &T) {
*a = b.clone(); *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 // ToOwned
fn owned_method_mut_ref(mut_string: &mut String, ref_str: &str) { fn owned_method_mut_ref(mut_string: &mut String, ref_str: &str) {
*mut_string = ref_str.to_owned(); *mut_string = ref_str.to_owned();

View file

@ -68,37 +68,37 @@ LL | a = b.clone();
| ^^^^^^^^^^^^^ help: use `clone_from()`: `a.clone_from(&b)` | ^^^^^^^^^^^^^ help: use `clone_from()`: `a.clone_from(&b)`
error: assigning the result of `ToOwned::to_owned()` may be inefficient 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(); LL | *mut_string = ref_str.to_owned();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(mut_string)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(mut_string)`
error: assigning the result of `ToOwned::to_owned()` may be inefficient 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(); LL | mut_string = ref_str.to_owned();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut mut_string)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut mut_string)`
error: assigning the result of `ToOwned::to_owned()` may be inefficient 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(); LL | **mut_box_string = ref_str.to_owned();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))`
error: assigning the result of `ToOwned::to_owned()` may be inefficient 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(); LL | **mut_box_string = ref_str.to_owned();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))`
error: assigning the result of `ToOwned::to_owned()` may be inefficient 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); LL | *mut_thing = ToOwned::to_owned(ref_str);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, mut_thing)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, mut_thing)`
error: assigning the result of `ToOwned::to_owned()` may be inefficient 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); LL | mut_thing = ToOwned::to_owned(ref_str);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, &mut mut_thing)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, &mut mut_thing)`