错误警告“从未读取分配的值”

时间:2019-05-14 02:53:12

标签: rust

我收到此警告:

 Compiling substrate-transaction-graph v2.0.0 (/home/niko/sub/substrate/core/transaction-pool/graph)
warning: value assigned to `altered_priority` is never read
   --> core/transaction-pool/graph/src/pool.rs:137:15
    |
137 |                         let mut altered_priority=priority;
    |                                 ^^^^^^^^^^^^^^^^
    |
    = note: #[warn(unused_assignments)] on by default
    = help: maybe it is overwritten before being read?

有关此代码的编译:

match self.api.validate_transaction(at, xt.clone())? {
    TransactionValidity::Valid { priority, requires, provides, longevity } => {
        info!(target: "txpool","priority before alteration: priority={:?}",priority);
        let mut altered_priority=priority;
        altered_priority=1;
        Ok(base::Transaction {
            data: xt,
            bytes,
            hash,
            priority: altered_priority,
            requires,
            provides,
            valid_till: block_number.as_().saturating_add(longevity),
         })
     },
     TransactionValidity::Invalid(e) => {
         bail!(error::Error::from(error::ErrorKind::InvalidTransaction(e)))
     },
     TransactionValidity::Unknown(e) => {
         self.listener.write().invalid(&hash);
         bail!(error::Error::from(error::ErrorKind::UnknownTransactionValidity(e)))
    },
}

我已经添加了log消息,并且在转储变量之后,我可以确认它们具有应具有的值(在执行代码之后),即{{1}中的priority字段}结构确实已更改:

Transaction

实际上,即使没有转储值,从代码本身也可以明显看出,该值用于创建结构2019-05-13 21:41:17 priority before alteration: priority=107 2019-05-13 21:41:17 Map TX begins 2019-05-13 21:41:17 TX IS OK: Transaction { hash: 0x79832c9790aee4b199a046cce27e46bb7e941f38e41d25629c922c318cf7c3a2, priority: 1, valid_till: 18446744073709551615, bytes: 107, requires: [], provides: [d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0200000000000000], data: 81ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27df063b12602c33fa92df4895ab3ecd9f2ad72544bd4b55f1c6c91c8c107dba3654fd13ca5e81612a7fe011414ca604e8f99feb1ed35ce471361ee2c14defdc503080003000ca10f} 2019-05-13 21:41:17 TX priority: 1 2019-05-13 21:41:17 Ok(Transaction { hash: 0x79832c9790aee4b199a046cce27e46bb7e941f38e41d25629c922c318cf7c3a2, priority: 1, valid_till: 18446744073709551615, bytes: 107, requires: [], provides: [d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0200000000000000], data: 81ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27df063b12602c33fa92df4895ab3ecd9f2ad72544bd4b55f1c6c91c8c107dba3654fd13ca5e81612a7fe011414ca604e8f99feb1ed35ce471361ee2c14defdc503080003000ca10f}) 2019-05-13 21:41:17 Map TX ends 。 那么,这是Transaction中的错误吗?

1 个答案:

答案 0 :(得分:5)

您写了let mut altered_priority = priority;,紧接着是altered_priority = 1;。编译器警告您,永远不会读取您在这两个语句中的第一个中分配给priority的初始值altered_priority。正确:从altered_priority变量读取的唯一值是1,而不是priority

没有理由先写一个值,然后立即无条件地将其替换为另一个值。您可以简单地用let altered_priority = 1;替换这两个语句。 (或者您可以摆脱变量,而在下面写priority: 1而不是在priority: altered_priority。)