Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clippy_lints/src/matches/manual_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ where
return None;
}

let mut app = Applicability::MachineApplicable;

// `map` won't perform any adjustments.
if expr_requires_coercion(cx, expr) {
return None;
app = Applicability::MaybeIncorrect;
}

// Determine which binding mode to use.
Expand Down Expand Up @@ -111,8 +113,6 @@ where
None => return None,
}

let mut app = Applicability::MachineApplicable;

// Remove address-of expressions from the scrutinee. Either `as_ref` will be called, or
// it's being passed by value.
let scrutinee = peel_hir_expr_refs(scrutinee).0;
Expand Down
19 changes: 1 addition & 18 deletions tests/ui/manual_map_option.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,7 @@ fn main() {
}

// #6811
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,
};
Some(0).map(|x| vec![x]);

option_env!("").map(String::from);

Expand Down Expand Up @@ -154,12 +145,4 @@ fn main() {
None => None,
};
}

// #7077
let s = &String::new();
#[allow(clippy::needless_match)]
let _: Option<&str> = match Some(s) {
Some(s) => Some(s),
None => None,
};
}
15 changes: 1 addition & 14 deletions tests/ui/manual_map_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,11 @@ fn main() {

// #6811
match Some(0) {
//~^ manual_map
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,
};

match option_env!("") {
//~^ manual_map
Some(x) => Some(String::from(x)),
Expand Down Expand Up @@ -237,12 +232,4 @@ fn main() {
None => None,
};
}

// #7077
let s = &String::new();
#[allow(clippy::needless_match)]
let _: Option<&str> = match Some(s) {
Some(s) => Some(s),
None => None,
};
}
18 changes: 14 additions & 4 deletions tests/ui/manual_map_option.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,17 @@ 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:196:5
--> tests/ui/manual_map_option.rs:185:5
|
LL | / match Some(0) {
LL | |
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:191:5
|
LL | / match option_env!("") {
LL | |
Expand All @@ -183,7 +193,7 @@ LL | | };
| |_____^ help: try: `option_env!("").map(String::from)`

error: manual implementation of `Option::map`
--> tests/ui/manual_map_option.rs:217:12
--> tests/ui/manual_map_option.rs:212:12
|
LL | } else if let Some(x) = Some(0) {
| ____________^
Expand All @@ -195,7 +205,7 @@ LL | | };
| |_____^ help: try: `{ Some(0).map(|x| x + 1) }`

error: manual implementation of `Option::map`
--> tests/ui/manual_map_option.rs:226:12
--> tests/ui/manual_map_option.rs:221:12
|
LL | } else if let Some(x) = Some(0) {
| ____________^
Expand All @@ -206,5 +216,5 @@ LL | | None
LL | | };
| |_____^ help: try: `{ Some(0).map(|x| x + 1) }`

error: aborting due to 20 previous errors
error: aborting due to 21 previous errors

67 changes: 0 additions & 67 deletions tests/ui/manual_map_option_2.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ fn main() {
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 {
Expand All @@ -65,31 +60,6 @@ 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,
};

let _ = Some(0).map(|_| match f() {
Ok(res) => Ok(Box::new(res)),
_ => Err(()),
Expand All @@ -98,48 +68,11 @@ mod with_type_coercion {

#[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,
});

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(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(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(s) = s { (x.clone(), s) } else { panic!() } }),
None => None,
}
};
x
}
}
67 changes: 0 additions & 67 deletions tests/ui/manual_map_option_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ fn main() {
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 {
Expand Down Expand Up @@ -85,31 +80,6 @@ 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,
};

let _ = match Some(0) {
//~^ manual_map
Some(_) => Some(match f() {
Expand All @@ -122,52 +92,15 @@ mod with_type_coercion {

#[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,
});

let _: Option<Box<&[u8]>> = match Some(0) {
//~^ manual_map
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(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(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(s) = s { (x.clone(), s) } else { panic!() } }),
None => None,
}
};
x
}
}
10 changes: 5 additions & 5 deletions tests/ui/manual_map_option_2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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:65:17
--> tests/ui/manual_map_option_2.rs:60:17
|
LL | let _ = match Some(0) {
| _________________^
Expand All @@ -44,7 +44,7 @@ LL | | };
| |_________^ help: try: `Some(0).map(|x| f(x))`

error: manual implementation of `Option::map`
--> tests/ui/manual_map_option_2.rs:71:13
--> tests/ui/manual_map_option_2.rs:66:13
|
LL | let _ = match Some(0) {
| _____________^
Expand All @@ -55,7 +55,7 @@ LL | | };
| |_____^ help: try: `Some(0).map(|x| unsafe { f(x) })`

error: manual implementation of `Option::map`
--> tests/ui/manual_map_option_2.rs:76:13
--> tests/ui/manual_map_option_2.rs:71:13
|
LL | let _ = match Some(0) {
| _____________^
Expand All @@ -66,7 +66,7 @@ LL | | };
| |_____^ help: try: `Some(0).map(|x| unsafe { f(x) })`

error: manual implementation of `Option::map`
--> tests/ui/manual_map_option_2.rs:113:17
--> tests/ui/manual_map_option_2.rs:83:17
|
LL | let _ = match Some(0) {
| _________________^
Expand All @@ -87,7 +87,7 @@ LL ~ });
|

error: manual implementation of `Option::map`
--> tests/ui/manual_map_option_2.rs:136:37
--> tests/ui/manual_map_option_2.rs:100:37
|
LL | let _: Option<Box<&[u8]>> = match Some(0) {
| _____________________________________^
Expand Down
Loading