简体   繁体   English

Cargo Clippy 抛出错误“问号运算符在这里没用” - 建议可以实施吗?

[英]Cargo Clippy throws error 'Question mark operator is useless here' - are suggestions okay to implement?

I've taken over a deploy process and part of this runs Cargo Clippy which was working fine up until late last week, when I started getting this error:我已经接管了一个部署过程,其中一部分运行 Cargo Clippy,它一直运行良好,直到上周晚些时候我开始收到此错误:

error: Question mark operator is useless here错误:问号运算符在这里没用

I've read the suggested link https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark and understand that the?我已阅读建议的链接https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark并理解? is no longer required, and have had a look at the suggested changes it offers.不再需要,并查看了它提供的建议更改。 I just want to know whether it is okay for me to make the changes as suggested, as some seem to remove some code so I'm not sure whether the result will be the same.我只是想知道我是否可以按照建议进行更改,因为有些似乎删除了一些代码,所以我不确定结果是否相同。

Some of the suggested changes seem to be simple and okay: From this一些建议的更改似乎很简单,还可以:从此

 Ok(dsl::orgs
     .filter(dsl::salesforce_id.eq(salesforce_id))
     .get_result(conn)?)

To this:对此:

dsl::orgs
     .filter(dsl::salesforce_id.eq(salesforce_id))
     .get_result(conn)

So I'm guessing that the above type of change is safe to accept?所以我猜测上述类型的更改可以安全接受吗?

Then I have these然后我有这些

  1. Here the 'optional' has disappeared in the suggested fix.这里的“可选”在建议的修复中消失了。 From:从:

     Ok(dsl::orgs.filter( dsl::customer_id.eq(new_org.customer_id).and(dsl::name.eq(&new_org.name)), ).get_result(conn).optional()?)

    To

    dsl::orgs.filter( dsl::customer_id.eq(new_org.customer_id).and(dsl::name.eq(&new_org.name)), )
  2. And this one, where the inner join has disappeared in the suggested fix:而这个,内部连接在建议的修复中消失了:

     Ok(orgs::dsl::orgs.filter(orgs::dsl::customer_id.eq(customer_id)).filter(orgs::dsl::kind.eq(org_kind)).filter( orgs::dsl::kind.eq(OrgKind::Production).or(orgs::dsl::name.eq(&org_name)), ).inner_join(schema::customers::dsl::customers).get_result(conn)?)

    to this:对此:

     orgs::dsl::orgs.filter(orgs::dsl::customer_id.eq(customer_id)).filter(orgs::dsl::kind.eq(org_kind)).filter( orgs::dsl::kind.eq(OrgKind::Production)

Are these 2 suggested fixes okay to implement?这 2 个建议的修复可以实施吗? Could someone please provide some help?有人可以提供一些帮助吗?

I've taken over a deploy process and part of this runs Cargo Clippy which was working fine up until late last week,我已经接管了一个部署过程,其中一部分运行 Cargo Clippy,它一直运行良好,直到上周晚些时候,

The lint appeared last week because Rust 1.51 and a new accompanying Clippy version was released last Thursday, and it added needless_question_mark . lint 上周出现是因为 Rust 1.51 和新的随附 Clippy 版本于上周四发布,并添加了needless_question_mark

when I started getting this error:当我开始收到此错误时:

Your CI should probably not consider all Clippy lints to be errors .您的 CI 可能不应该将所有 Clippy lints 都视为错误 Clippy is designed to give warnings about many things that are simply code style improvements, or possible problems; Clippy 旨在对许多简单的代码样式改进或可能的问题发出警告; counting them as errors will lead to unnecessary breakage in the future.将它们视为错误将导致将来不必要的损坏。

So I'm guessing that the above type of change is safe to accept?所以我猜测上述类型的更改可以安全接受吗?

Ok(some_expression?) is almost the same as some_expression . Ok(some_expression?)some_expression几乎相同。 The only difference is that the version with ?唯一的区别是版本与? may implicitly convert the error type of the expression (the E in Result<T, E> ) to the error type expected by the function.可以将表达式的错误类型( Result<T, E> E的 E)隐式转换为 function 预期的错误类型。 If the error types are the same and no conversion is required, then you can remove the Ok and ?如果错误类型相同且不需要转换,则可以删除Ok? and get the same results;并得到相同的结果; if the error types are different, the simplified version will not compile.如果错误类型不同,简化版将无法编译。

Here the 'optional' has disappeared in the suggested fix.这里的“可选”在建议的修复中消失了。

This seems like a bug in Clippy or whatever tool is applying the fix, as the resulting code is semantically different and would not even compile.这似乎是 Clippy 或任何应用修复的工具中的错误,因为生成的代码在语义上是不同的,甚至无法编译。 I'd remove the Ok and ?我会删除Ok? manually in that case, and report the bug.在这种情况下手动,并报告错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM