Skip to content
Closed
Changes from 1 commit
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
32 changes: 23 additions & 9 deletions fehler-macros/src/throws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ impl Fold for Throws {

fn fold_expr_return(&mut self, i: syn::ExprReturn) -> syn::ExprReturn {
let ok = match &i.expr {
Some(expr) => ok(expr),
None => ok_unit(),
Some(expr) => ok(expr, false),
None => ok_unit(false),
};
syn::ExprReturn { expr: Some(Box::new(ok)), ..i }
}
Expand All @@ -117,13 +117,13 @@ fn modify_tail(is_unit_fn: bool, stmts: &mut Vec<syn::Stmt>) {
let new = syn::parse2(quote::quote!(#e;)).unwrap();
stmts.pop();
stmts.push(new);
stmts.push(syn::Stmt::Expr(ok_unit()));
stmts.push(syn::Stmt::Expr(ok_unit(true)));
}
Some(syn::Stmt::Expr(e)) => {
*e = ok(e);
*e = ok(e, true);
}
_ if is_unit_fn => {
stmts.push(syn::Stmt::Expr(ok_unit()));
stmts.push(syn::Stmt::Expr(ok_unit(true)));
}
_ => { }
}
Expand All @@ -141,10 +141,24 @@ fn is_unit_fn(i: &syn::ReturnType) -> bool {
}
}

fn ok(expr: &syn::Expr) -> syn::Expr {
syn::parse2(quote::quote!(<_ as ::fehler::__internal::_Succeed>::from_ok(#expr))).unwrap()
fn ok(expr: &syn::Expr, is_tail: bool) -> syn::Expr {
if is_tail {
syn::parse_quote! {
#[allow(unreachable_code)]
<_ as ::fehler::__internal::_Succeed>::from_ok(#expr)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's not great that this ends up suppressing legitimate unreachable_code lints inside the user's tail expr (which might be a big complicated match or something). Is there a different way?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Adding here is certainly wrong (I was confused with the case of unit return).
It seems to be desirable to add the attribute only on an implicit unit, and inherit the original attributes when an explicit tail expr is provided.

}
} else {
syn::parse_quote!(<_ as ::fehler::__internal::_Succeed>::from_ok(#expr))
}
}

fn ok_unit() -> syn::Expr {
syn::parse2(quote::quote!(<_ as ::fehler::__internal::_Succeed>::from_ok(()))).unwrap()
fn ok_unit(is_tail: bool) -> syn::Expr {
if is_tail {
syn::parse_quote! {
#[allow(unreachable_code)]
<_ as ::fehler::__internal::_Succeed>::from_ok(())
}
} else {
syn::parse_quote!(<_ as ::fehler::__internal::_Succeed>::from_ok(()))
}
}