Skip to content

Commit 233b5f2

Browse files
committed
Auto merge of rust-lang#13273 - alex-semenyuk:assigning_clones_disable_for_test, r=blyxyas
Disable assigning_clones for tests Close: rust-lang#12752 As mentioned at rust-lang#12752 when a test struct is initialized with some default string, this inverts the order of assignee/assignment and makes a bit harder to read/write code changelog: [`assigning_clones.rs`]: disable assigning_clones for tests
2 parents 68b222e + 953c7ed commit 233b5f2

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

clippy_lints/src/assigning_clones.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_config::Conf;
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::mir::{enclosing_mir, PossibleBorrowerMap};
55
use clippy_utils::sugg::Sugg;
6-
use clippy_utils::{is_diag_trait_item, last_path_segment, local_is_initialized, path_to_local};
6+
use clippy_utils::{is_diag_trait_item, is_in_test, last_path_segment, local_is_initialized, path_to_local};
77
use rustc_errors::Applicability;
88
use rustc_hir::{self as hir, Expr, ExprKind};
99
use rustc_lint::{LateContext, LateLintPass};
@@ -118,6 +118,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
118118
}
119119
)
120120
&& !clone_source_borrows_from_dest(cx, lhs, rhs.span)
121+
&& !is_in_test(cx.tcx, e.hir_id)
121122
{
122123
span_lint_and_then(
123124
cx,

tests/ui/assigning_clones.fixed

+20
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,23 @@ impl<T: Clone> Clone for DerefWrapperWithClone<T> {
396396
*self = Self(source.0.clone());
397397
}
398398
}
399+
400+
#[cfg(test)]
401+
mod test {
402+
#[derive(Default)]
403+
struct Data {
404+
field: String,
405+
}
406+
407+
fn test_data() -> Data {
408+
Data {
409+
field: "default_value".to_string(),
410+
}
411+
}
412+
413+
#[test]
414+
fn test() {
415+
let mut data = test_data();
416+
data.field = "override_value".to_owned();
417+
}
418+
}

tests/ui/assigning_clones.rs

+20
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,23 @@ impl<T: Clone> Clone for DerefWrapperWithClone<T> {
396396
*self = Self(source.0.clone());
397397
}
398398
}
399+
400+
#[cfg(test)]
401+
mod test {
402+
#[derive(Default)]
403+
struct Data {
404+
field: String,
405+
}
406+
407+
fn test_data() -> Data {
408+
Data {
409+
field: "default_value".to_string(),
410+
}
411+
}
412+
413+
#[test]
414+
fn test() {
415+
let mut data = test_data();
416+
data.field = "override_value".to_owned();
417+
}
418+
}

0 commit comments

Comments
 (0)