false warning “value assigned is never read”

2019-09-22 11:43发布

I am getting this warning:

 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?

On compilation of this code:

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)))
    },
}

I have added log messages and after dumping the variables I can confirm that they have the values that they should have (after the code is executed), i.e. the priority field in Transaction struct was indeed changed:

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

Actually, without even dumping the values, it is obvious from the code itself that the value is used in the creation of the struct Transaction. So, is this a bug in Rust ?

标签: rust
1条回答
对你真心纯属浪费
2楼-- · 2019-09-22 11:46

You wrote let mut altered_priority = priority; followed immediately by altered_priority = 1;. The compiler is warning you that the initial value priority you assigned to altered_priority in the first of these two statements is never read. It is correct: the only value ever read from the altered_priority variable is 1, not priority.

There’s no reason to write one value and then immediately and unconditionally replace it with another. You could have replaced those two statements with simply let altered_priority = 1;. (Or you could get rid of the variable and write priority: 1 instead of priority: altered_priority below.)

查看更多
登录 后发表回答