Skip to content

Fix if_then_some_else_none FP when require type coercion #15267

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion clippy_lints/src/if_then_some_else_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_with_context;
use clippy_utils::sugg::Sugg;
use clippy_utils::{
contains_return, higher, is_else_clause, is_in_const_context, is_res_lang_ctor, path_res, peel_blocks,
contains_return, expr_requires_coercion, higher, is_else_clause, is_in_const_context, is_res_lang_ctor, path_res,
peel_blocks,
};
use rustc_errors::Applicability;
use rustc_hir::LangItem::{OptionNone, OptionSome};
Expand Down Expand Up @@ -79,6 +80,7 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
&& !expr.span.in_external_macro(cx.sess().source_map())
&& self.msrv.meets(cx, msrvs::BOOL_THEN)
&& !contains_return(then_block.stmts)
&& !expr_requires_coercion(cx, then_expr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, what do you think about, instead of completely discarding the block if it requires type coercion, we check if we're in a trait item function (we cannot change the signatures of those functions) and if we're in just a normal function, we emit a non-MachineApplicable suggestion with a note about the user will have to modify the return type?

{
let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(cx, msrvs::BOOL_THEN_SOME) {
"then_some"
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/if_then_some_else_none.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,19 @@ const fn issue12103(x: u32) -> Option<u32> {
// Should not issue an error in `const` context
if x > 42 { Some(150) } else { None }
}

mod issue15257 {
#[derive(Default)]
pub struct Foo {}
pub trait Bar {}
impl Bar for Foo {}

#[allow(clippy::manual_is_multiple_of)]
fn maybe_get_bar(i: u32) -> Option<Box<dyn Bar>> {
if i % 2 == 0 {
Some(Box::new(Foo::default()))
} else {
None
}
}
}
16 changes: 16 additions & 0 deletions tests/ui/if_then_some_else_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,19 @@ const fn issue12103(x: u32) -> Option<u32> {
// Should not issue an error in `const` context
if x > 42 { Some(150) } else { None }
}

mod issue15257 {
#[derive(Default)]
pub struct Foo {}
pub trait Bar {}
impl Bar for Foo {}

#[allow(clippy::manual_is_multiple_of)]
fn maybe_get_bar(i: u32) -> Option<Box<dyn Bar>> {
if i % 2 == 0 {
Some(Box::new(Foo::default()))
} else {
None
}
}
}
Loading