forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#6941 - ThibsG:suggMatchSingleBinding, r=llogiq
Fix bad suggestion for `match_single_binding` lint Fix a bad suggestion that needs curly braces when the target `match` is the body of an arm. Fixes rust-lang#6572 changelog: none
- Loading branch information
Showing
4 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::match_single_binding)] | ||
#![allow(unused_variables)] | ||
|
||
fn main() { | ||
// Lint (additional curly braces needed, see #6572) | ||
struct AppendIter<I> | ||
where | ||
I: Iterator, | ||
{ | ||
inner: Option<(I, <I as Iterator>::Item)>, | ||
} | ||
|
||
#[allow(dead_code)] | ||
fn size_hint<I: Iterator>(iter: &AppendIter<I>) -> (usize, Option<usize>) { | ||
match &iter.inner { | ||
Some((iter, _item)) => { | ||
let (min, max) = iter.size_hint(); | ||
(min.saturating_add(1), max.and_then(|max| max.checked_add(1))) | ||
}, | ||
None => (0, Some(0)), | ||
} | ||
} | ||
|
||
// Lint (no additional curly braces needed) | ||
let opt = Some((5, 2)); | ||
let get_tup = || -> (i32, i32) { (1, 2) }; | ||
match opt { | ||
#[rustfmt::skip] | ||
Some((first, _second)) => { | ||
let (a, b) = get_tup(); | ||
println!("a {:?} and b {:?}", a, b); | ||
}, | ||
None => println!("nothing"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::match_single_binding)] | ||
#![allow(unused_variables)] | ||
|
||
fn main() { | ||
// Lint (additional curly braces needed, see #6572) | ||
struct AppendIter<I> | ||
where | ||
I: Iterator, | ||
{ | ||
inner: Option<(I, <I as Iterator>::Item)>, | ||
} | ||
|
||
#[allow(dead_code)] | ||
fn size_hint<I: Iterator>(iter: &AppendIter<I>) -> (usize, Option<usize>) { | ||
match &iter.inner { | ||
Some((iter, _item)) => match iter.size_hint() { | ||
(min, max) => (min.saturating_add(1), max.and_then(|max| max.checked_add(1))), | ||
}, | ||
None => (0, Some(0)), | ||
} | ||
} | ||
|
||
// Lint (no additional curly braces needed) | ||
let opt = Some((5, 2)); | ||
let get_tup = || -> (i32, i32) { (1, 2) }; | ||
match opt { | ||
#[rustfmt::skip] | ||
Some((first, _second)) => { | ||
match get_tup() { | ||
(a, b) => println!("a {:?} and b {:?}", a, b), | ||
} | ||
}, | ||
None => println!("nothing"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
error: this match could be written as a `let` statement | ||
--> $DIR/match_single_binding2.rs:18:36 | ||
| | ||
LL | Some((iter, _item)) => match iter.size_hint() { | ||
| ____________________________________^ | ||
LL | | (min, max) => (min.saturating_add(1), max.and_then(|max| max.checked_add(1))), | ||
LL | | }, | ||
| |_____________^ | ||
| | ||
= note: `-D clippy::match-single-binding` implied by `-D warnings` | ||
help: consider using `let` statement | ||
| | ||
LL | Some((iter, _item)) => { | ||
LL | let (min, max) = iter.size_hint(); | ||
LL | (min.saturating_add(1), max.and_then(|max| max.checked_add(1))) | ||
LL | }, | ||
| | ||
|
||
error: this match could be written as a `let` statement | ||
--> $DIR/match_single_binding2.rs:31:13 | ||
| | ||
LL | / match get_tup() { | ||
LL | | (a, b) => println!("a {:?} and b {:?}", a, b), | ||
LL | | } | ||
| |_____________^ | ||
| | ||
help: consider using `let` statement | ||
| | ||
LL | let (a, b) = get_tup(); | ||
LL | println!("a {:?} and b {:?}", a, b); | ||
| | ||
|
||
error: aborting due to 2 previous errors | ||
|