Skip to content

Commit

Permalink
Add regression test for rust-lang#12612
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Apr 8, 2024
1 parent 2202493 commit 677615d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/ui/map_clone.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,54 @@ fn main() {
let _z = y.map(RcWeak::clone);
}
}

// Ensures that it doesn't suggest `cloned` on `Arc` types.
fn issue_12612() {
use std::sync::Arc;

struct S2(Arc<String>);
struct S1(Option<S2>);

impl S1 {
fn v2(&self) -> Option<Arc<String>> {
self.v1().map(|v1| Arc::clone(&v1.0))
}

fn v3(&self) -> Option<Arc<String>> {
self.v1().map(|v1| {
let x = &v1.0;
Arc::clone(x)
})
}

fn v1(&self) -> Option<&S2> {
match &self.0 {
None => None,
Some(v) => Some(v),
}
}
}

struct D2(String);
struct D1(Option<D2>);

impl D1 {
fn v2(&self) -> Option<String> {
self.v1().map(|v1| String::clone(&v1.0))
}

fn v3(&self) -> Option<String> {
self.v1().map(|v1| {
let x = &v1.0;
String::clone(x)
})
}

fn v1(&self) -> Option<&D2> {
match &self.0 {
None => None,
Some(v) => Some(v),
}
}
}
}
51 changes: 51 additions & 0 deletions tests/ui/map_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,54 @@ fn main() {
let _z = y.map(RcWeak::clone);
}
}

// Ensures that it doesn't suggest `cloned` on `Arc` types.
fn issue_12612() {
use std::sync::Arc;

struct S2(Arc<String>);
struct S1(Option<S2>);

impl S1 {
fn v2(&self) -> Option<Arc<String>> {
self.v1().map(|v1| Arc::clone(&v1.0))
}

fn v3(&self) -> Option<Arc<String>> {
self.v1().map(|v1| {
let x = &v1.0;
Arc::clone(x)
})
}

fn v1(&self) -> Option<&S2> {
match &self.0 {
None => None,
Some(v) => Some(v),
}
}
}

struct D2(String);
struct D1(Option<D2>);

impl D1 {
fn v2(&self) -> Option<String> {
self.v1().map(|v1| String::clone(&v1.0))
}

fn v3(&self) -> Option<String> {
self.v1().map(|v1| {
let x = &v1.0;
String::clone(x)
})
}

fn v1(&self) -> Option<&D2> {
match &self.0 {
None => None,
Some(v) => Some(v),
}
}
}
}

0 comments on commit 677615d

Please sign in to comment.