如何重新表达此代码以避免 clippy::manual_let_else。

huangapple go评论60阅读模式
英文:

how to rephrase this code to get past clippy::manual_let_else

问题

以下是您要翻译的内容:

升级了 msrv 后,我得到了这个错误(在一个我不拥有的项目中,只是进行 msrv 升级,所以移除 'pedantic' 是不可以的)

error: this could be rewritten as `let...else`
   --> asyncgit/src/sync/config.rs:99:2
    |
99  | /     let entry = match entry_res {
100 | |         Ok(ent) => ent,
101 | |         Err(_) => return Ok(None),
102 | |     };
    | |______^ help: consider writing: `let Ok(ent) = entry_res else { return Ok(None) };`

在这段代码上

let entry_res = cfg.get_entry(key);

let entry = match entry_res {
	Ok(ent) => ent,
	Err(_) => return Ok(None),
 };

get_entry 返回一个 Result<T,...>

建议的修复不是有效的代码 - 除非我漏掉了一些显而易见的东西

编辑。当我说不是有效的代码时,我的意思是它不能编译,我确信语法是正确的,这是我的错,因为我不能理解它试图做什么。但这绝对是 clippy 的一个 bug。

编辑2:显然是一个已知的 bug https://github.com/rust-lang/rust-clippy/issues/10171

英文:

after some upgrade of msrv I get this (In a project I dont own, just doing the msrv upgrade, so removing the 'pedantic' is not OK)

error: this could be rewritten as `let...else`
   --&gt; asyncgit/src/sync/config.rs:99:2
    |
99  | /     let entry = match entry_res {
100 | |         Ok(ent) =&gt; ent,
101 | |         Err(_) =&gt; return Ok(None),
102 | |     };
    | |______^ help: consider writing: `let Ok(ent) = entry_res else { return Ok(None) };`

on this code

let entry_res = cfg.get_entry(key);

let entry = match entry_res {
	Ok(ent) =&gt; ent,
	Err(_) =&gt; return Ok(None),
 };

get_entry returns a Result&lt;T,...&gt;

The suggested fix is not valid code - unless I missed something obvious

Edit. When I say not valid code, I mean it won't compile, I am sure the syntax is correct and that it's my fault for not being able to follow what it's trying to do. But it's for sure a clippy bug

EDIT2 : apparently a known bug https://github.com/rust-lang/rust-clippy/issues/10171

答案1

得分: 1

The suggested fix is not valid code - unless I missed something obvious

建议的修复不是有效的代码 - 除非我漏掉了什么明显的东西

The suggested fix seems to have a pretty simple error (which you should report): it should be
建议的修复似乎有一个相当简单的错误(你应该报告它):应该是

let Ok(entry) = entry_res else { return Ok(None) };

Otherwise it creates the wrong top-level binding (ent rather than entry).
否则,它会创建错误的顶级绑定(ent 而不是 entry)。

Aside from that, the suggested code has been valid code since 1.65.0, released November 2022 (and longer than that on nightly).
除此之外,建议的代码从1.65.0版本开始是有效的代码,于2022年11月发布(在夜间版上更长时间有效)。

Though it is possible clippy does not properly take MSRV in account, I've no idea.
尽管可能 clippy 没有正确考虑到 MSRV,但我不清楚。

英文:

> The suggested fix is not valid code - unless I missed something obvious

The suggested fix seems to have a pretty simple error (which you should report): it should be

let Ok(entry) = entry_res else { return Ok(None) };

Otherwise it creates the wrong top-level binding (ent rather than entry).

Aside from that, the suggested code has been valid code since 1.65.0, released November 2022 (and longer than that on nightly).

Though it is possible clippy does not properly take MSRV in account, I've no idea.

huangapple
  • 本文由 发表于 2023年5月11日 12:44:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定