Skip to content

Commit

Permalink
Auto merge of #13273 - alex-semenyuk:assigning_clones_disable_for_tes…
Browse files Browse the repository at this point in the history
…t, r=blyxyas

Disable assigning_clones for tests

Close: #12752
As mentioned at #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
  • Loading branch information
bors committed Aug 18, 2024
2 parents 68b222e + 953c7ed commit 233b5f2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clippy_lints/src/assigning_clones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::mir::{enclosing_mir, PossibleBorrowerMap};
use clippy_utils::sugg::Sugg;
use clippy_utils::{is_diag_trait_item, last_path_segment, local_is_initialized, path_to_local};
use clippy_utils::{is_diag_trait_item, is_in_test, last_path_segment, local_is_initialized, path_to_local};
use rustc_errors::Applicability;
use rustc_hir::{self as hir, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -118,6 +118,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
}
)
&& !clone_source_borrows_from_dest(cx, lhs, rhs.span)
&& !is_in_test(cx.tcx, e.hir_id)
{
span_lint_and_then(
cx,
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/assigning_clones.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,23 @@ impl<T: Clone> Clone for DerefWrapperWithClone<T> {
*self = Self(source.0.clone());
}
}

#[cfg(test)]
mod test {
#[derive(Default)]
struct Data {
field: String,
}

fn test_data() -> Data {
Data {
field: "default_value".to_string(),
}
}

#[test]
fn test() {
let mut data = test_data();
data.field = "override_value".to_owned();
}
}
20 changes: 20 additions & 0 deletions tests/ui/assigning_clones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,23 @@ impl<T: Clone> Clone for DerefWrapperWithClone<T> {
*self = Self(source.0.clone());
}
}

#[cfg(test)]
mod test {
#[derive(Default)]
struct Data {
field: String,
}

fn test_data() -> Data {
Data {
field: "default_value".to_string(),
}
}

#[test]
fn test() {
let mut data = test_data();
data.field = "override_value".to_owned();
}
}

0 comments on commit 233b5f2

Please sign in to comment.