Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix if_then_some_else_none sugg missing closure intro #13409

Merged
merged 1 commit into from
Sep 22, 2024
Merged
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
2 changes: 2 additions & 0 deletions clippy_lints/src/if_then_some_else_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
snippet_with_context(cx, first_stmt.span.until(then_arg.span), ctxt, "..", &mut app);
let closure = if method_name == "then" { "|| " } else { "" };
format!("{closure} {{ {block_snippet}; {arg_snip} }}")
} else if method_name == "then" {
(std::borrow::Cow::Borrowed("|| ") + arg_snip).into_owned()
} else {
arg_snip.into_owned()
};
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/if_then_some_else_none.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ fn issue11394(b: bool, v: Result<(), ()>) -> Result<(), ()> {
Ok(())
}

fn issue13407(s: &str) -> Option<bool> {
(s == "1").then(|| true)
}

const fn issue12103(x: u32) -> Option<u32> {
// Should not issue an error in `const` context
if x > 42 { Some(150) } else { None }
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/if_then_some_else_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ fn issue11394(b: bool, v: Result<(), ()>) -> Result<(), ()> {
Ok(())
}

fn issue13407(s: &str) -> Option<bool> {
if s == "1" { Some(true) } else { None }
}

const fn issue12103(x: u32) -> Option<u32> {
// Should not issue an error in `const` context
if x > 42 { Some(150) } else { None }
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/if_then_some_else_none.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,11 @@ LL | | None
LL | | };
| |_____^ help: try: `foo().then(|| { println!("true!"); 150 })`

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

error: aborting due to 6 previous errors