我运行cargo clippy
检查我的代码,并得到了clippy的反馈,他告诉我我可以通过某种方式折叠if let语句。
以下是具体的“警告”:
warning: this `if let` can be collapsed into the outer `if let`
--> src\main.rs:107:21
|
107 | / if let Move::Normal { piece, from, to } = turn {
108 | | if i8::abs(from.1 - to.1) == 2 && piece.getColor() != *color && to.0 == x {
109 | | let offsetX = x - to.0;
110 | |
... |
116 | | }
117 | | }
| |_____________________^
我原以为可以使用&&
将内部的if追加到外部if let中,但是我得到了一个警告( `let` expressions in this position are experimental
,我正在使用rust版本1.57.0
,而不是nightly)。
你有什么想法可以满足clippy的要求吗?
编辑:
外部的if let本身又再次嵌套在另一个if let中:
if let Some(turn) = board.getLastMove() {
而且看起来你确实可以像这样结合它们:
if let Some(Move::Normal { piece, from, to }) = board.getLastMove() {
我认为clippy lint应该包括上面的行,因为对我来说否则有点令人困惑。
编辑2:
事实证明我只是无法阅读,在上面列出的警告下面还有一些更多的信息,告诉我应该做什么。
= note: `#[warn(clippy::collapsible_match)]` on by default
help: the outer pattern can be modified to include the inner pattern
--> src\main.rs:126:29
|
126 | if let Some(turn) = board.getLastMove() {
| ^^^^ replace this binding
127 | if let Move::Normal { piece, from, to } = turn {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ with this pattern
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_match
if let
嵌套到更高层的if let
中,而不是将第108行的if
改为在第107行使用if let
。是否还有另一个层次的if let
? - Joe_Jingyuif let
语句合并为一个:if let Some(Move::Normal { piece, from, to }) = board.getLastMove()
。 - Joe_Jingyu