From 4264548283dbaabb854e903994e4055215a956b8 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 15 Jul 2024 21:16:17 +0700 Subject: [PATCH 1/2] Add regression test for issue 13077 --- tests/ui/needless_option_as_deref.fixed | 14 ++++++++++++++ tests/ui/needless_option_as_deref.rs | 14 ++++++++++++++ tests/ui/needless_option_as_deref.stderr | 8 +++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/ui/needless_option_as_deref.fixed b/tests/ui/needless_option_as_deref.fixed index 58f56eba0..7bde15558 100644 --- a/tests/ui/needless_option_as_deref.fixed +++ b/tests/ui/needless_option_as_deref.fixed @@ -52,3 +52,17 @@ struct S<'a> { fn from_field<'a>(s: &'a mut S<'a>) -> Option<&'a mut usize> { s.opt.as_deref_mut() } + +mod issue_non_copy_13077 { + pub fn something(mut maybe_side_effect: Option<&mut String>) { + for _ in 0..10 { + let _ = S { field: other(maybe_side_effect) }; + } + } + + fn other(_maybe_side_effect: Option<&mut String>) { + unimplemented!() + } + + pub struct S { pub field: () } +} diff --git a/tests/ui/needless_option_as_deref.rs b/tests/ui/needless_option_as_deref.rs index 842e025f6..ab06a38f7 100644 --- a/tests/ui/needless_option_as_deref.rs +++ b/tests/ui/needless_option_as_deref.rs @@ -52,3 +52,17 @@ struct S<'a> { fn from_field<'a>(s: &'a mut S<'a>) -> Option<&'a mut usize> { s.opt.as_deref_mut() } + +mod issue_non_copy_13077 { + pub fn something(mut maybe_side_effect: Option<&mut String>) { + for _ in 0..10 { + let _ = S { field: other(maybe_side_effect.as_deref_mut()) }; + } + } + + fn other(_maybe_side_effect: Option<&mut String>) { + unimplemented!() + } + + pub struct S { pub field: () } +} diff --git a/tests/ui/needless_option_as_deref.stderr b/tests/ui/needless_option_as_deref.stderr index a05d0aa92..3211062af 100644 --- a/tests/ui/needless_option_as_deref.stderr +++ b/tests/ui/needless_option_as_deref.stderr @@ -19,5 +19,11 @@ error: derefed type is same as origin LL | let _ = x.as_deref_mut(); | ^^^^^^^^^^^^^^^^ help: try: `x` -error: aborting due to 3 previous errors +error: derefed type is same as origin + --> tests/ui/needless_option_as_deref.rs:59:38 + | +LL | let _ = S { field: other(maybe_side_effect.as_deref_mut()) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `maybe_side_effect` + +error: aborting due to 4 previous errors From dcee2e8a0fb95887284b7779f2fd809f148722c5 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 15 Jul 2024 15:56:17 +0000 Subject: [PATCH 2/2] Dont stop find loop node at struct field node --- clippy_utils/src/lib.rs | 2 +- tests/ui/needless_option_as_deref.fixed | 8 ++++++-- tests/ui/needless_option_as_deref.rs | 8 ++++++-- tests/ui/needless_option_as_deref.stderr | 8 +------- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 8a6750c89..0156e223c 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -1407,7 +1407,7 @@ pub fn get_enclosing_loop_or_multi_call_closure<'tcx>( ExprKind::Closure { .. } | ExprKind::Loop(..) => return Some(e), _ => (), }, - Node::Stmt(_) | Node::Block(_) | Node::LetStmt(_) | Node::Arm(_) => (), + Node::Stmt(_) | Node::Block(_) | Node::LetStmt(_) | Node::Arm(_) | Node::ExprField(_) => (), _ => break, } } diff --git a/tests/ui/needless_option_as_deref.fixed b/tests/ui/needless_option_as_deref.fixed index 7bde15558..84eaf12fc 100644 --- a/tests/ui/needless_option_as_deref.fixed +++ b/tests/ui/needless_option_as_deref.fixed @@ -56,7 +56,9 @@ fn from_field<'a>(s: &'a mut S<'a>) -> Option<&'a mut usize> { mod issue_non_copy_13077 { pub fn something(mut maybe_side_effect: Option<&mut String>) { for _ in 0..10 { - let _ = S { field: other(maybe_side_effect) }; + let _ = S { + field: other(maybe_side_effect.as_deref_mut()), + }; } } @@ -64,5 +66,7 @@ mod issue_non_copy_13077 { unimplemented!() } - pub struct S { pub field: () } + pub struct S { + pub field: (), + } } diff --git a/tests/ui/needless_option_as_deref.rs b/tests/ui/needless_option_as_deref.rs index ab06a38f7..fff1e45d8 100644 --- a/tests/ui/needless_option_as_deref.rs +++ b/tests/ui/needless_option_as_deref.rs @@ -56,7 +56,9 @@ fn from_field<'a>(s: &'a mut S<'a>) -> Option<&'a mut usize> { mod issue_non_copy_13077 { pub fn something(mut maybe_side_effect: Option<&mut String>) { for _ in 0..10 { - let _ = S { field: other(maybe_side_effect.as_deref_mut()) }; + let _ = S { + field: other(maybe_side_effect.as_deref_mut()), + }; } } @@ -64,5 +66,7 @@ mod issue_non_copy_13077 { unimplemented!() } - pub struct S { pub field: () } + pub struct S { + pub field: (), + } } diff --git a/tests/ui/needless_option_as_deref.stderr b/tests/ui/needless_option_as_deref.stderr index 3211062af..a05d0aa92 100644 --- a/tests/ui/needless_option_as_deref.stderr +++ b/tests/ui/needless_option_as_deref.stderr @@ -19,11 +19,5 @@ error: derefed type is same as origin LL | let _ = x.as_deref_mut(); | ^^^^^^^^^^^^^^^^ help: try: `x` -error: derefed type is same as origin - --> tests/ui/needless_option_as_deref.rs:59:38 - | -LL | let _ = S { field: other(maybe_side_effect.as_deref_mut()) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `maybe_side_effect` - -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors