fix [manual_map] not catching type adjustment
This commit is contained in:
parent
75e3a2e905
commit
a2f9861df8
4 changed files with 302 additions and 17 deletions
|
|
@ -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