Can't fix 'unused_assignments' warning

I have a warning but i dont know how to fix it:
image

My code :

let mut old_block: U64 = U64::zero();
    loop {
        old_block = match get_new_block_http(provider, interval, old_block).await {
            Ok(res) => res,
            Err(e) => {
                eprintln!("Failed to watch to new blocks: {e}");
                continue
            }
        };

The function get_new_block_http used the argument old_block, so the value is read before new assignment?

I put this at the top of my function definition to pass the warning but it doesn't work:

#[allow(unused_assignments)]

Thank for your help

This should fix:

#![allow(unused)]

In addition:
"Create variables, when you need them.
Don't assign values, if you won't read them."

let mut block_number:u64;

Should also help.

This code indeed would not produce the warning you show, so there must be something else going on. Please show the entire code of the function so we can understand more. (In general, when troubleshooting Rust, it's always a good idea to consider the entire function, because type checking, borrow checking, and lints like this one always look at the whole function themselves.

General advice: Your first choice should always be to fix the problem, not suppress the warnings. Warnings from the Rust compiler are very rarely wrong, and only need to be allowed when you're doing something specific and unusual.

2 Likes

Thank for your response, i replace it and it doesnt work.

I need to assign a value because my function get_new_block_http will use it.

Here the entire function:

pub async fn test_detect_block_http() {
    let blockchain_used: Blockchain = get_blockchain_menu();
    // let _dex_type: DexType = get_dex_menu(blockchain_used.clone());

    println!("Please enter a interval (ms): ");
    let mut interval = String::new();
    io::stdin()
        .read_line(&mut interval)
        .expect("Please enter correct number");

    let interval:u64 = match interval.trim().parse() {
        Ok(address) => address,
        Err(e) => panic!("{:?}", e)
    };

    // create instance
    // let _contract_address = blockchain_used.address;
    let provider_url_http = blockchain_used.providers.http.expect("No HTTP provider URL set");

    // provider used to watch block
    let provider = Provider::<Http>::connect(provider_url_http).await;
    let mut old_block: U64 = U64::zero();
    loop {
        old_block = match get_new_block_http(provider.clone(), interval, old_block).await {
            Ok(res) => res,
            Err(e) => {
                eprintln!("Failed to watch to new blocks: {e}");
                continue
            }
        };
        // something todo
    }
}

and the function called:

/// goal: with http provider sent at regular intervals new request to get the latest block
pub async fn get_new_block_http(provider: Provider<Http>, interval: u64, old_block_number: U64) -> eyre::Result<U64> {
    let mut block_number = U64::zero();
    loop {
        block_number = provider.get_block_number().await?;

        if block_number > old_block_number {
            break;
        }
        tokio::time::sleep(Duration::from_millis(interval)).await;
    }
    Ok(block_number)

}

Thank for your advice, i posted the entire code of the function on my last response

The warning is about the variable block_number, not old_block.
The code in get_new_block_http assigns U64::zero to block_number. The code then enters the loop. And the first thing the loop does is overwriting the block_number variable without reading it prior. So the assignment of U64::zero() is entirely useless, as the compiler told you. Try this:

let mut block_number;
4 Likes

You could also move the variable into the loop, by doing something like

pub async fn get_new_block_http(provider: Provider<Http>, interval: u64, old_block_number: U64) -> eyre::Result<U64> {
    loop {
        let block_number = provider.get_block_number().await?;

        if block_number > old_block_number {
            break Ok(block_number);
        }
        tokio::time::sleep(Duration::from_millis(interval)).await;
    }
}

I would always encouraging pushing the declaration down to the initialization where possible, especially when it lets you avoid mut.

5 Likes

Thank you all for your response, i just miss read, sorry for this stupid question :grinning:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.