Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

rust - false warning "value assigned is never read"

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...