make [manual_map] ignore types that contain dyn (#12712)
fixes: #12659 [`manual_map`] and [`manual_filter`] shares the same check logic, but this issue doesn't seems like it could affect `manual_filter` (?) --- changelog: make [`manual_map`] ignore types that contain `dyn`
This commit is contained in:
commit
d7fd1c8e3c
8 changed files with 344 additions and 34 deletions
|
|
@ -113,7 +113,16 @@ fn main() {
|
|||
}
|
||||
|
||||
// #6811
|
||||
Some(0).map(|x| vec![x]);
|
||||
match Some(0) {
|
||||
Some(x) => Some(vec![x]),
|
||||
None => None,
|
||||
};
|
||||
|
||||
// Don't lint, coercion
|
||||
let x: Option<Vec<&[u8]>> = match Some(()) {
|
||||
Some(_) => Some(vec![b"1234"]),
|
||||
None => None,
|
||||
};
|
||||
|
||||
option_env!("").map(String::from);
|
||||
|
||||
|
|
|
|||
|
|
@ -170,6 +170,12 @@ fn main() {
|
|||
None => None,
|
||||
};
|
||||
|
||||
// Don't lint, coercion
|
||||
let x: Option<Vec<&[u8]>> = match Some(()) {
|
||||
Some(_) => Some(vec![b"1234"]),
|
||||
None => None,
|
||||
};
|
||||
|
||||
match option_env!("") {
|
||||
Some(x) => Some(String::from(x)),
|
||||
None => None,
|
||||
|
|
|
|||
|
|
@ -156,16 +156,7 @@ LL | | };
|
|||
| |_____^ help: try: `Some((String::new(), "test")).as_ref().map(|(x, y)| (y, x))`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> tests/ui/manual_map_option.rs:168:5
|
||||
|
|
||||
LL | / match Some(0) {
|
||||
LL | | Some(x) => Some(vec![x]),
|
||||
LL | | None => None,
|
||||
LL | | };
|
||||
| |_____^ help: try: `Some(0).map(|x| vec![x])`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> tests/ui/manual_map_option.rs:173:5
|
||||
--> tests/ui/manual_map_option.rs:179:5
|
||||
|
|
||||
LL | / match option_env!("") {
|
||||
LL | | Some(x) => Some(String::from(x)),
|
||||
|
|
@ -174,7 +165,7 @@ LL | | };
|
|||
| |_____^ help: try: `option_env!("").map(String::from)`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> tests/ui/manual_map_option.rs:193:12
|
||||
--> tests/ui/manual_map_option.rs:199:12
|
||||
|
|
||||
LL | } else if let Some(x) = Some(0) {
|
||||
| ____________^
|
||||
|
|
@ -185,7 +176,7 @@ LL | | };
|
|||
| |_____^ help: try: `{ Some(0).map(|x| x + 1) }`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> tests/ui/manual_map_option.rs:201:12
|
||||
--> tests/ui/manual_map_option.rs:207:12
|
||||
|
|
||||
LL | } else if let Some(x) = Some(0) {
|
||||
| ____________^
|
||||
|
|
@ -195,5 +186,5 @@ LL | | None
|
|||
LL | | };
|
||||
| |_____^ help: try: `{ Some(0).map(|x| x + 1) }`
|
||||
|
||||
error: aborting due to 21 previous errors
|
||||
error: aborting due to 20 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -40,9 +40,14 @@ fn main() {
|
|||
None => None,
|
||||
};
|
||||
|
||||
// Lint. `s` is captured by reference, so no lifetime issues.
|
||||
let s = Some(String::new());
|
||||
// Lint. `s` is captured by reference, so no lifetime issues.
|
||||
let _ = s.as_ref().map(|x| { if let Some(ref s) = s { (x.clone(), s) } else { panic!() } });
|
||||
// Don't lint this, type of `s` is coercioned from `&String` to `&str`
|
||||
let x: Option<(String, &str)> = match &s {
|
||||
Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
|
||||
None => None,
|
||||
};
|
||||
|
||||
// Issue #7820
|
||||
unsafe fn f(x: u32) -> u32 {
|
||||
|
|
@ -54,3 +59,89 @@ fn main() {
|
|||
let _ = Some(0).map(|x| unsafe { f(x) });
|
||||
let _ = Some(0).map(|x| unsafe { f(x) });
|
||||
}
|
||||
|
||||
// issue #12659
|
||||
mod with_type_coercion {
|
||||
trait DummyTrait {}
|
||||
|
||||
fn foo<T: DummyTrait, F: Fn() -> Result<T, ()>>(f: F) {
|
||||
// Don't lint
|
||||
let _: Option<Result<Box<dyn DummyTrait>, ()>> = match Some(0) {
|
||||
Some(_) => Some(match f() {
|
||||
Ok(res) => Ok(Box::new(res)),
|
||||
_ => Err(()),
|
||||
}),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let _: Option<Box<&[u8]>> = match Some(()) {
|
||||
Some(_) => Some(Box::new(b"1234")),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let x = String::new();
|
||||
let _: Option<Box<&str>> = match Some(()) {
|
||||
Some(_) => Some(Box::new(&x)),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let _: Option<&str> = match Some(()) {
|
||||
Some(_) => Some(&x),
|
||||
None => None,
|
||||
};
|
||||
|
||||
//~v ERROR: manual implementation of `Option::map`
|
||||
let _ = Some(0).map(|_| match f() {
|
||||
Ok(res) => Ok(Box::new(res)),
|
||||
_ => Err(()),
|
||||
});
|
||||
}
|
||||
|
||||
#[allow(clippy::redundant_allocation)]
|
||||
fn bar() {
|
||||
fn f(_: Option<Box<&[u8]>>) {}
|
||||
fn g(b: &[u8]) -> Box<&[u8]> {
|
||||
Box::new(b)
|
||||
}
|
||||
|
||||
let x: &[u8; 4] = b"1234";
|
||||
f(match Some(()) {
|
||||
Some(_) => Some(Box::new(x)),
|
||||
None => None,
|
||||
});
|
||||
|
||||
//~v ERROR: manual implementation of `Option::map`
|
||||
let _: Option<Box<&[u8]>> = Some(0).map(|_| g(x));
|
||||
}
|
||||
|
||||
fn with_fn_ret(s: &Option<String>) -> Option<(String, &str)> {
|
||||
// Don't lint, `map` doesn't work as the return type is adjusted.
|
||||
match s {
|
||||
Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn with_fn_ret_2(s: &Option<String>) -> Option<(String, &str)> {
|
||||
if true {
|
||||
// Don't lint, `map` doesn't work as the return type is adjusted.
|
||||
return match s {
|
||||
Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_late_init)]
|
||||
fn with_fn_ret_3<'a>(s: &'a Option<String>) -> Option<(String, &'a str)> {
|
||||
let x: Option<(String, &'a str)>;
|
||||
x = {
|
||||
match s {
|
||||
Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
|
||||
None => None,
|
||||
}
|
||||
};
|
||||
x
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,12 +43,17 @@ fn main() {
|
|||
None => None,
|
||||
};
|
||||
|
||||
// Lint. `s` is captured by reference, so no lifetime issues.
|
||||
let s = Some(String::new());
|
||||
// Lint. `s` is captured by reference, so no lifetime issues.
|
||||
let _ = match &s {
|
||||
Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
|
||||
None => None,
|
||||
};
|
||||
// Don't lint this, type of `s` is coercioned from `&String` to `&str`
|
||||
let x: Option<(String, &str)> = match &s {
|
||||
Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
|
||||
None => None,
|
||||
};
|
||||
|
||||
// Issue #7820
|
||||
unsafe fn f(x: u32) -> u32 {
|
||||
|
|
@ -69,3 +74,95 @@ fn main() {
|
|||
None => None,
|
||||
};
|
||||
}
|
||||
|
||||
// issue #12659
|
||||
mod with_type_coercion {
|
||||
trait DummyTrait {}
|
||||
|
||||
fn foo<T: DummyTrait, F: Fn() -> Result<T, ()>>(f: F) {
|
||||
// Don't lint
|
||||
let _: Option<Result<Box<dyn DummyTrait>, ()>> = match Some(0) {
|
||||
Some(_) => Some(match f() {
|
||||
Ok(res) => Ok(Box::new(res)),
|
||||
_ => Err(()),
|
||||
}),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let _: Option<Box<&[u8]>> = match Some(()) {
|
||||
Some(_) => Some(Box::new(b"1234")),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let x = String::new();
|
||||
let _: Option<Box<&str>> = match Some(()) {
|
||||
Some(_) => Some(Box::new(&x)),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let _: Option<&str> = match Some(()) {
|
||||
Some(_) => Some(&x),
|
||||
None => None,
|
||||
};
|
||||
|
||||
//~v ERROR: manual implementation of `Option::map`
|
||||
let _ = match Some(0) {
|
||||
Some(_) => Some(match f() {
|
||||
Ok(res) => Ok(Box::new(res)),
|
||||
_ => Err(()),
|
||||
}),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(clippy::redundant_allocation)]
|
||||
fn bar() {
|
||||
fn f(_: Option<Box<&[u8]>>) {}
|
||||
fn g(b: &[u8]) -> Box<&[u8]> {
|
||||
Box::new(b)
|
||||
}
|
||||
|
||||
let x: &[u8; 4] = b"1234";
|
||||
f(match Some(()) {
|
||||
Some(_) => Some(Box::new(x)),
|
||||
None => None,
|
||||
});
|
||||
|
||||
//~v ERROR: manual implementation of `Option::map`
|
||||
let _: Option<Box<&[u8]>> = match Some(0) {
|
||||
Some(_) => Some(g(x)),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
|
||||
fn with_fn_ret(s: &Option<String>) -> Option<(String, &str)> {
|
||||
// Don't lint, `map` doesn't work as the return type is adjusted.
|
||||
match s {
|
||||
Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn with_fn_ret_2(s: &Option<String>) -> Option<(String, &str)> {
|
||||
if true {
|
||||
// Don't lint, `map` doesn't work as the return type is adjusted.
|
||||
return match s {
|
||||
Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_late_init)]
|
||||
fn with_fn_ret_3<'a>(s: &'a Option<String>) -> Option<(String, &'a str)> {
|
||||
let x: Option<(String, &'a str)>;
|
||||
x = {
|
||||
match s {
|
||||
Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
|
||||
None => None,
|
||||
}
|
||||
};
|
||||
x
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ LL | | };
|
|||
| |_____^ help: try: `s.as_ref().map(|x| { if let Some(ref s) = s { (x.clone(), s) } else { panic!() } })`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> tests/ui/manual_map_option_2.rs:58:17
|
||||
--> tests/ui/manual_map_option_2.rs:63:17
|
||||
|
|
||||
LL | let _ = match Some(0) {
|
||||
| _________________^
|
||||
|
|
@ -42,7 +42,7 @@ LL | | };
|
|||
| |_________^ help: try: `Some(0).map(|x| f(x))`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> tests/ui/manual_map_option_2.rs:63:13
|
||||
--> tests/ui/manual_map_option_2.rs:68:13
|
||||
|
|
||||
LL | let _ = match Some(0) {
|
||||
| _____________^
|
||||
|
|
@ -52,7 +52,7 @@ LL | | };
|
|||
| |_____^ help: try: `Some(0).map(|x| unsafe { f(x) })`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> tests/ui/manual_map_option_2.rs:67:13
|
||||
--> tests/ui/manual_map_option_2.rs:72:13
|
||||
|
|
||||
LL | let _ = match Some(0) {
|
||||
| _____________^
|
||||
|
|
@ -61,5 +61,36 @@ LL | | None => None,
|
|||
LL | | };
|
||||
| |_____^ help: try: `Some(0).map(|x| unsafe { f(x) })`
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
error: manual implementation of `Option::map`
|
||||
--> tests/ui/manual_map_option_2.rs:109:17
|
||||
|
|
||||
LL | let _ = match Some(0) {
|
||||
| _________________^
|
||||
LL | | Some(_) => Some(match f() {
|
||||
LL | | Ok(res) => Ok(Box::new(res)),
|
||||
LL | | _ => Err(()),
|
||||
LL | | }),
|
||||
LL | | None => None,
|
||||
LL | | };
|
||||
| |_________^
|
||||
|
|
||||
help: try
|
||||
|
|
||||
LL ~ let _ = Some(0).map(|_| match f() {
|
||||
LL + Ok(res) => Ok(Box::new(res)),
|
||||
LL + _ => Err(()),
|
||||
LL ~ });
|
||||
|
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> tests/ui/manual_map_option_2.rs:132:37
|
||||
|
|
||||
LL | let _: Option<Box<&[u8]>> = match Some(0) {
|
||||
| _____________________________________^
|
||||
LL | | Some(_) => Some(g(x)),
|
||||
LL | | None => None,
|
||||
LL | | };
|
||||
| |_________^ help: try: `Some(0).map(|_| g(x))`
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue