Skip to content

Commit

Permalink
Avoid renaming for TupleStruct with multiple arguments
Browse files Browse the repository at this point in the history
update spec

fix: move specs in fire
  • Loading branch information
koka831 committed Feb 1, 2023
1 parent 20338fd commit 07c8c50
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/manual_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ fn emit_manual_let_else(
let (sn_pat, _) = snippet_with_context(cx, pat.span, span.ctxt(), "", &mut app);
format!("({sn_pat})")
},
PatKind::TupleStruct(ref w, ..) => {
// Replace the variable name iff `TupleStruct` has one argument like `Variant(v)`.
PatKind::TupleStruct(ref w, args, ..) if args.len() == 1 => {
let sn_wrapper = cx.sess().source_map().span_to_snippet(w.span()).unwrap_or_default();
let (sn_inner, _) = snippet_with_context(cx, local.span, span.ctxt(), "", &mut app);
format!("{sn_wrapper}({sn_inner})")
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/manual_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
)]
#![warn(clippy::manual_let_else)]

enum Variant {
A(usize, usize),
B(usize),
C,
}

fn g() -> Option<()> {
None
}
Expand Down Expand Up @@ -135,6 +141,15 @@ fn fire() {
};
}
create_binding_if_some!(w, g());

fn e() -> Variant {
Variant::A(0, 0)
}

// Should not be renamed
let v = if let Variant::A(a, 0) = e() { a } else { return };
// Should be renamed
let v = if let Variant::B(b) = e() { b } else { return };
}

fn not_fire() {
Expand Down
50 changes: 31 additions & 19 deletions tests/ui/manual_let_else.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:18:5
--> $DIR/manual_let_else.rs:24:5
|
LL | let v = if let Some(v_some) = g() { v_some } else { return };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v) = g() else { return };`
|
= note: `-D clippy::manual-let-else` implied by `-D warnings`

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:19:5
--> $DIR/manual_let_else.rs:25:5
|
LL | / let v = if let Some(v_some) = g() {
LL | | v_some
Expand All @@ -24,7 +24,7 @@ LL + };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:25:5
--> $DIR/manual_let_else.rs:31:5
|
LL | / let v = if let Some(v) = g() {
LL | | // Blocks around the identity should have no impact
Expand All @@ -45,25 +45,25 @@ LL + };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:38:9
--> $DIR/manual_let_else.rs:44:9
|
LL | let v = if let Some(v_some) = g() { v_some } else { continue };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v) = g() else { continue };`

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:39:9
--> $DIR/manual_let_else.rs:45:9
|
LL | let v = if let Some(v_some) = g() { v_some } else { break };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v) = g() else { break };`

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:43:5
--> $DIR/manual_let_else.rs:49:5
|
LL | let v = if let Some(v_some) = g() { v_some } else { panic!() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v) = g() else { panic!() };`

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:46:5
--> $DIR/manual_let_else.rs:52:5
|
LL | / let v = if let Some(v_some) = g() {
LL | | v_some
Expand All @@ -80,7 +80,7 @@ LL + };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:53:5
--> $DIR/manual_let_else.rs:59:5
|
LL | / let v = if let Some(v_some) = g() {
LL | | v_some
Expand All @@ -97,7 +97,7 @@ LL + };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:60:5
--> $DIR/manual_let_else.rs:66:5
|
LL | / let v = if let Some(v_some) = g() {
LL | | v_some
Expand All @@ -116,7 +116,7 @@ LL + };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:70:5
--> $DIR/manual_let_else.rs:76:5
|
LL | / let v = if let Some(v_some) = g() {
LL | | v_some
Expand All @@ -138,13 +138,13 @@ LL + };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:80:5
--> $DIR/manual_let_else.rs:86:5
|
LL | let v = if let Some(v_some) = g() { v_some } else { if panic!() {} };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v) = g() else { if panic!() {} };`

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:83:5
--> $DIR/manual_let_else.rs:89:5
|
LL | / let v = if let Some(v_some) = g() {
LL | | v_some
Expand All @@ -165,7 +165,7 @@ LL + };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:92:5
--> $DIR/manual_let_else.rs:98:5
|
LL | / let v = if let Some(v_some) = g() {
LL | | v_some
Expand All @@ -186,7 +186,7 @@ LL + } };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:101:5
--> $DIR/manual_let_else.rs:107:5
|
LL | / let v = if let Some(v_some) = g() {
LL | | v_some
Expand Down Expand Up @@ -215,7 +215,7 @@ LL + };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:118:5
--> $DIR/manual_let_else.rs:124:5
|
LL | / let (v, w) = if let Some(v_some) = g().map(|v| (v, 42)) {
LL | | v_some
Expand All @@ -232,7 +232,7 @@ LL + };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:125:5
--> $DIR/manual_let_else.rs:131:5
|
LL | / let v = if let (Some(v_some), w_some) = (g(), 0) {
LL | | (w_some, v_some)
Expand All @@ -249,7 +249,7 @@ LL + };
|

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:134:13
--> $DIR/manual_let_else.rs:140:13
|
LL | let $n = if let Some(v) = $e { v } else { return };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some($n) = g() else { return };`
Expand All @@ -260,13 +260,25 @@ LL | create_binding_if_some!(w, g());
= note: this error originates in the macro `create_binding_if_some` (in Nightly builds, run with -Z macro-backtrace for more info)

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:247:5
--> $DIR/manual_let_else.rs:150:5
|
LL | let v = if let Variant::A(a, 0) = e() { a } else { return };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Variant::A(a, 0) = e() else { return };`

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:152:5
|
LL | let v = if let Variant::B(b) = e() { b } else { return };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Variant::B(v) = e() else { return };`

error: this could be rewritten as `let...else`
--> $DIR/manual_let_else.rs:262:5
|
LL | / let _ = match ff {
LL | | Some(value) => value,
LL | | _ => macro_call!(),
LL | | };
| |______^ help: consider writing: `let Some(_) = ff else { macro_call!() };`

error: aborting due to 18 previous errors
error: aborting due to 20 previous errors

0 comments on commit 07c8c50

Please sign in to comment.