Skip to content

Commit

Permalink
Don't generate unit tuple in clone macro as default-return value
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez authored and bilelmoussaoui committed Jul 23, 2023
1 parent ed849fd commit 11a9402
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion glib-macros/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ let {0} = match {1}::clone::Upgrade::upgrade(&{0}) {{
)
}
(BorrowKind::Weak, Some(WrapperKind::DefaultReturn(ref r))) => {
let not_unit_ret = r.chars().any(|c| c != '(' && c != ')' && c != ' ');
format!(
"\
let {0} = match {1}::clone::Upgrade::upgrade(&{0}) {{
Expand All @@ -210,7 +211,7 @@ let {0} = match {1}::clone::Upgrade::upgrade(&{0}) {{
}};",
name,
crate_ident_new(),
r,
if not_unit_ret { r } else { "" },
)
}
(BorrowKind::Weak, None) => {
Expand Down
21 changes: 21 additions & 0 deletions glib-macros/tests/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,24 @@ fn clone_failures() {
t.compile_fail_inline_check_sub(&format!("test_{index}"), &output, err);
}
}

const NO_WARNING: &[&str] = &[
"clone!(@weak v => @default-return (), move || println!(\"{}\", v);)",
"clone!(@weak v => @default-return (()), move || println!(\"{}\", v);)",
"clone!(@weak v => @default-return ( () ), move || println!(\"{}\", v);)",
"clone!(@weak v => @default-return ( ), move || println!(\"{}\", v);)",
];

// Ensures that no warning are emitted if the default-return is a unit tuple.
#[test]
fn clone_unit_tuple_return() {
let t = trybuild2::TestCases::new();

for (index, expr) in NO_WARNING.iter().enumerate() {
let prefix = "fn main() { use glib::clone; let v = std::rc::Rc::new(1); ";
let suffix = "; }";
let output = format!("{prefix}{expr}{suffix}");

t.pass_inline(&format!("test_{index}"), &output);
}
}

0 comments on commit 11a9402

Please sign in to comment.