rust - Cargo Clippy 抛出错误 'Question mark operator is useless here' - 建议可以实现吗?

标签 rust lint rust-cargo rust-clippy

我已经接管了部署过程,其中一部分运行 Cargo Clippy,直到上周晚些时候我开始收到此错误:

error: Question mark operator is useless here

我已阅读建议的链接 https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark 并了解?不再需要,并查看了它提供的建议更改。我只是想知道我是否可以按照建议进行更改,因为有些似乎删除了一些代码,所以我不确定结果是否会相同。

一些建议的更改似乎很简单也不错: 从此

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

对此:

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

所以我猜上述类型的更改可以安全接受?

那我有这些

  1. 这里的“可选”已在建议的修复中消失。来自:

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

    dsl::orgs
        .filter(
            dsl::customer_id
                .eq(new_org.customer_id)
                .and(dsl::name.eq(&new_org.name)),
    )
    
  2. 而这个,内部连接在建议的修复中消失了:

    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)?)
    

    对此:

    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)
    

这 2 个建议的修复可以实现吗?有人可以提供一些帮助吗?

最佳答案

I've taken over a deploy process and part of this runs Cargo Clippy which was working fine up until late last week,

上周出现了 lint,因为 Rust 1.51 和 new accompanying Clippy version上周四发布,其中添加了needless_question_mark .

when I started getting this error:

您的 CI 可能不应该将所有 Clippy lint 视为错误。 Clippy 旨在针对许多简单的代码风格改进或可能问题发出警告;将它们算作错误将导致将来不必要的损坏。

So I'm guessing that the above type of change is safe to accept?

Ok(some_expression?)some_expression几乎相同。唯一的区别是带有 ? 的版本可以将表达式的错误类型( E 中的 Result<T, E> )隐式转换为函数期望的错误类型。如果错误类型相同且不需要转换,则可以删除 Ok?并得到相同的结果;如果错误类型不同,简化版本将无法编译。

Here the 'optional' has disappeared in the suggested fix.

这似乎是 Clippy 或任何正在应用修复的工具中的错误,因为生成的代码在语义上不同,甚至无法编译。我会删除 Ok?在这种情况下手动,并报告错误。

关于rust - Cargo Clippy 抛出错误 'Question mark operator is useless here' - 建议可以实现吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66846199/

相关文章:

c - 将 Rust 变量传递给期望能够修改它的 C 函数

rust - 如何将 rustfmt 配置为不为匹配的不安全 block 模式发出对齐空格?

rust - 我如何将 'pass down' 功能标志标记为 Cargo 中的子依赖项?

image - 无法在Rust中使用Rgba <u8>(无方法)

Rust:对向量中结构的多态调用

rust - 我如何获得对解构结构的引用或失败?

java - 为什么一些 Android 代码在以前的 API 级别上工作而不是应该的?

javascript - Lint 双逗号数组

debugging - 有没有办法在gdb或lldb中直接运行Cargo构建的程序?