From fb824244d0ce350306e2141630bc7600f7df65cd Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 27 Dec 2024 09:27:31 -0800 Subject: [PATCH] Resolve manual_let_else clippy lints in experimental-enum-variants-from-header warning: this could be rewritten as `let...else` --> macro/src/load.rs:107:13 | 107 | / let name = match &decl.name { 108 | | Some(name) => name, 109 | | // Can ignore enums inside an anonymous namespace. 110 | | None => return, 111 | | }; | |______________^ help: consider writing: `let Some(name) = &decl.name else { return };` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_let_else = note: `-W clippy::manual-let-else` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::manual_let_else)]` warning: this could be rewritten as `let...else` --> macro/src/load.rs:116:13 | 116 | / let name = match &decl.name { 117 | | Some(name) => name, 118 | | None => return, 119 | | }; | |______________^ help: consider writing: `let Some(name) = &decl.name else { return };` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_let_else warning: this could be rewritten as `let...else` --> macro/src/load.rs:130:21 | 130 | / let fixed_underlying_type = match &decl.fixed_underlying_type { 131 | | Some(fixed_underlying_type) => fixed_underlying_type, 132 | | None => { 133 | | let span = &enm.variants_from_header_attr; ... | 143 | | }; | |______________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_let_else help: consider writing | 130 ~ let Some(fixed_underlying_type) = &decl.fixed_underlying_type else { 131 + let span = &enm.variants_from_header_attr; 132 + let name = &enm.name.cxx; 133 + let qual_name = CxxName(&enm.name); 134 + let msg = format!( 135 + "implicit implementation-defined repr for enum {} is not supported yet; consider changing its C++ definition to `enum {}: int {{...}}", 136 + qual_name, name, 137 + ); 138 + cx.error(span, msg); 139 + return; 140 + }; | warning: this could be rewritten as `let...else` --> macro/src/load.rs:172:17 | 172 | / let cxx_name = match ForeignName::parse(&decl.name, span) { 173 | | Ok(foreign_name) => foreign_name, 174 | | Err(_) => { 175 | | let span = &enm.variants_from_header_attr; ... | 179 | | }; | |__________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_let_else help: consider writing | 172 ~ let Ok(cxx_name) = ForeignName::parse(&decl.name, span) else { 173 + let span = &enm.variants_from_header_attr; 174 + let msg = format!("unsupported C++ variant name: {}", decl.name); 175 + return cx.error(span, msg); 176 + }; | --- macro/src/load.rs | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/macro/src/load.rs b/macro/src/load.rs index d3148c94e..31fbaf522 100644 --- a/macro/src/load.rs +++ b/macro/src/load.rs @@ -104,18 +104,16 @@ fn traverse<'a>( ) { match &node.kind { Clang::NamespaceDecl(decl) => { - let name = match &decl.name { - Some(name) => name, + let Some(name) = &decl.name else { // Can ignore enums inside an anonymous namespace. - None => return, + return; }; namespace.push(name); idx = None; } Clang::EnumDecl(decl) => { - let name = match &decl.name { - Some(name) => name, - None => return, + let Some(name) = &decl.name else { + return; }; idx = None; for (i, enm) in variants_from_header.iter_mut().enumerate() { @@ -127,19 +125,16 @@ fn traverse<'a>( cx.error(span, msg); return; } - let fixed_underlying_type = match &decl.fixed_underlying_type { - Some(fixed_underlying_type) => fixed_underlying_type, - None => { - let span = &enm.variants_from_header_attr; - let name = &enm.name.cxx; - let qual_name = CxxName(&enm.name); - let msg = format!( - "implicit implementation-defined repr for enum {} is not supported yet; consider changing its C++ definition to `enum {}: int {{...}}", - qual_name, name, - ); - cx.error(span, msg); - return; - } + let Some(fixed_underlying_type) = &decl.fixed_underlying_type else { + let span = &enm.variants_from_header_attr; + let name = &enm.name.cxx; + let qual_name = CxxName(&enm.name); + let msg = format!( + "implicit implementation-defined repr for enum {} is not supported yet; consider changing its C++ definition to `enum {}: int {{...}}", + qual_name, name, + ); + cx.error(span, msg); + return; }; let repr = translate_qual_type( cx, @@ -169,13 +164,10 @@ fn traverse<'a>( .get_ident() .unwrap() .span(); - let cxx_name = match ForeignName::parse(&decl.name, span) { - Ok(foreign_name) => foreign_name, - Err(_) => { - let span = &enm.variants_from_header_attr; - let msg = format!("unsupported C++ variant name: {}", decl.name); - return cx.error(span, msg); - } + let Ok(cxx_name) = ForeignName::parse(&decl.name, span) else { + let span = &enm.variants_from_header_attr; + let msg = format!("unsupported C++ variant name: {}", decl.name); + return cx.error(span, msg); }; let rust_name: Ident = match syn::parse_str(&decl.name) { Ok(ident) => ident,