11

Github Allow unused variables with todo! by charles-r-earp · Pull Request #79850...

 2 years ago
source link: https://github.com/rust-lang/rust/pull/79850
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Allow unused variables with todo! by charles-r-earp · Pull Request #79850 · rust-lang/rust · GitHub

Fixes #78136

As stated in the issue, todo! should imply that the function will be implemented at some point but isn't yet, and therefore the user shouldn't have to either apply the #[allow(unused_variables)] themselves or prefix the arguments with underscores, since they will at some point replace the todo! with an implementation. Unlike unimplemented, todo! is a placeholder, so the suggested prefixing of the arguments with underscores does not make sense because that would imply that those arguments are not going to be used, when in fact they may be used when the function is implemented. Once the todo! is removed, the lint will return if applicable.

The lint error is emitted in rustc_passes::liveness::Liveness::report_unused. This can be traced back to the <IrMaps as Visiter>::visit_body method. Unfortunately the Body argument here has been fully expanded, so we cannot match on the todo! macro directly. Instead, the idea is to detect a trailing expression, ie the last expression without a semicolon, that is core::panicking::panic (also std). If the panic is called with "not yet implemented", which is what is emitted by todo!, then we return early and don't do any checking for unused variables.

For example:

fn foo(x: i32) -> i32 {
    // some code
    todo!()
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK