Skip to content

Commit c3d58cd

Browse files
committed
Fix if_then_some_else_none sugg missing closure intro
Fixes rust-lang#13407
1 parent 903293b commit c3d58cd

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

clippy_lints/src/if_then_some_else_none.rs

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
105105
snippet_with_context(cx, first_stmt.span.until(then_arg.span), ctxt, "..", &mut app);
106106
let closure = if method_name == "then" { "|| " } else { "" };
107107
format!("{closure} {{ {block_snippet}; {arg_snip} }}")
108+
} else if method_name == "then" {
109+
(std::borrow::Cow::Borrowed("|| ") + arg_snip).into_owned()
108110
} else {
109111
arg_snip.into_owned()
110112
};

tests/ui/if_then_some_else_none.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ fn issue11394(b: bool, v: Result<(), ()>) -> Result<(), ()> {
113113
Ok(())
114114
}
115115

116+
fn issue13407(s: &str) -> Option<bool> {
117+
(s == "1").then(|| true)
118+
}
119+
116120
const fn issue12103(x: u32) -> Option<u32> {
117121
// Should not issue an error in `const` context
118122
if x > 42 { Some(150) } else { None }

tests/ui/if_then_some_else_none.rs

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ fn issue11394(b: bool, v: Result<(), ()>) -> Result<(), ()> {
131131
Ok(())
132132
}
133133

134+
fn issue13407(s: &str) -> Option<bool> {
135+
if s == "1" { Some(true) } else { None }
136+
}
137+
134138
const fn issue12103(x: u32) -> Option<u32> {
135139
// Should not issue an error in `const` context
136140
if x > 42 { Some(150) } else { None }

tests/ui/if_then_some_else_none.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,11 @@ LL | | None
5252
LL | | };
5353
| |_____^ help: try: `foo().then(|| { println!("true!"); 150 })`
5454

55-
error: aborting due to 5 previous errors
55+
error: this could be simplified with `bool::then`
56+
--> tests/ui/if_then_some_else_none.rs:135:5
57+
|
58+
LL | if s == "1" { Some(true) } else { None }
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(s == "1").then(|| true)`
60+
61+
error: aborting due to 6 previous errors
5662

0 commit comments

Comments
 (0)