prevent useless_asref from suggesting .clone() on types without the Clone trait

This commit is contained in:
lapla-cogito 2025-02-08 22:10:12 +09:00
parent a8b17827c6
commit 91548d0fe3
No known key found for this signature in database
GPG key ID: B39C71D9F127FF9F
4 changed files with 82 additions and 19 deletions

View file

@ -8,6 +8,7 @@
)]
use std::fmt::Debug;
use std::ops::Deref;
use std::rc::{Rc, Weak as RcWeak};
use std::sync::{Arc, Weak as ArcWeak};
@ -218,6 +219,35 @@ fn issue_14088() {
let _: Option<&str> = s.as_ref().map(|x| x.as_ref());
}
pub struct Wrap<T> {
inner: T,
}
impl<T> Deref for Wrap<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
struct NonCloneableError;
pub struct Issue12357 {
current: Option<Wrap<Arc<u32>>>,
}
impl Issue12357 {
fn f(&self) -> Option<Arc<u32>> {
self.current.as_ref().map(|p| Arc::clone(p))
}
fn g(&self) {
let result: Result<String, NonCloneableError> = Ok("Hello".to_string());
let cloned = result.as_ref().map(|s| s.clone());
}
}
fn main() {
not_ok();
ok();