Rfcs: Option to suppress unused variable warnings on unimplemented!() functions.

Created on 9 Aug 2016  ·  11Comments  ·  Source: rust-lang/rfcs

Pretty much what it says on the tin. Same with unused mutability, etc. I've no idea how complex or difficult this idea would be to implement. Thoughts?

T-compiler

Most helpful comment

[allow(unused_variables, unused_mut)]

Even better, we have lint groups!

#[allow(unused)]
fn my_stuff(a: u32, mut b: u64) -> bool {
    unimplemented!()
}

unused == unused_imports, unused_variables, unused_assignments, dead_code, unused_mut, unreachable_code, unused_must_use, unused_unsafe, path_statements, unused_attributes

All 11 comments

@sullyj3 You can make underscore prefix for each variable:

fn foo(i: i32) {}

To:

fn foo(_i: i32) {}

And what about unused mutability?

I'd prefer to keep the warnings in place. I often dummy out some code while iterating during development, and that results in warnings; the warnings help me avoid leaving that code dummied out.

How is this supposed to interact with code like this?

fn do_something(a: MyEnum, b: i32, c: i32) -> i32 {
    match a {
        MyEnum::First => b,
        MyEnum::Second => unimplemented!(),
        MyEnum::Third => 3,
    }
}

Does c not get an unused warning?

Perhaps it could take arguments as an explicit "use", like unimplemented!(c).

One potential alternative, if you want to make an entire function unimplemented: you could define a macro unimpl_fn! that takes a function declaration as an argument:

unimpl_fn! { fn do_something(a: MyEnum, b: i32, c: i32) -> i32 }

That macro could then walk over the arguments and modify them to have _ prefixes, which will suppress warnings about unused variables and unused mutability; it could also insert a body containing unimplemented!.

Is #[allow(unused_variables)] above a function insufficient?

#[allow(unused_variables, unused_mut)]
fn my_stuff(a: u32, mut b: u64) -> bool { // function is never used: `my_stuff`, #[warn(dead_code)] on by default
    unimplemented!()
}

fn main() {
    let proper_warning = (); // unused variable: `proper_warning`, #[warn(unused_variables)] on by default
}

With stmt_expr_attributes feature, you can even apply attributes to any statement or expression.

[allow(unused_variables, unused_mut)]

Even better, we have lint groups!

#[allow(unused)]
fn my_stuff(a: u32, mut b: u64) -> bool {
    unimplemented!()
}

unused == unused_imports, unused_variables, unused_assignments, dead_code, unused_mut, unreachable_code, unused_must_use, unused_unsafe, path_statements, unused_attributes

We prefer attributes in the source to compiler flags for this sort of thing, so I think that the solutions pointed out in this thread are likely to be the long-term solutions.

@sullyj3 do you think there is anything missing with these solutions?

Thanks guys, probably should've done my research beforehand!

Is there a way to enable this for all of a single project inline to the code itself (without env variables)?

Specifically I'm using VSCode with RLS support which uses rustc to check for errors and when first prototyping dead code warnings can be both pervasive and can hide actual bugs, but when I'm ready to clean up the dead wood I want to be able to switch out by just deleting one or two lines of code
[edit]
wrong place, moving to forums or something

Was this page helpful?
0 / 5 - 0 ratings