rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.1k stars 1.49k forks source link

unused_lifetime false positive #2678

Open YaLTeR opened 6 years ago

YaLTeR commented 6 years ago

I'm using this type of stuff in a -sys-crate:

unsafe extern "C" fn callback<'a>(data: *mut c_void) {
    let _s = Box::from_raw(data as *mut S<'a>);
}

and this triggers unused_lifetime.

Playground with more context (also shows the #2677 issue): https://play.rust-lang.org/?gist=21a412537258ede9cbda42350501b90a&version=nightly

Manishearth commented 6 years ago

That lifetime is always going to be 'static, you should just do data as *mut S<'static> here.

YaLTeR commented 6 years ago

Thanks, didn't know that. Then perhaps that should be specified in the warning text?

oli-obk commented 6 years ago

A note stating that all useless uses of the liftetime should be replaced with 'static is a good idea

andreicristianpetcu commented 6 years ago

Can I pick this up? I'm a rust n00b :)

oli-obk commented 6 years ago

@andreicristianpetcu great! it's all yours. If you have any questions, you can post here or open a WIP PR showing what you tried and ask for help there.

andreicristianpetcu commented 6 years ago

Sorry I did not have time to work on this and I might not have in the future either :(

snowsignal commented 6 years ago

Is it possible that I could handle this issue, @oli-obk?

oli-obk commented 6 years ago

@JacksonCoder jup!

snowsignal commented 6 years ago

Great, thanks!

snowsignal commented 6 years ago

Should I make this a separate warning, or integrate it with the unused_lifetime lint? In the former case, we could call the lint unnecessary_lifetime or something else, where it points out that since the lifetime is unused in the function definition, we can just replace the usage of that unused lifetime with 'static. In the latter case, is it possible to make 'notes' for a lint, just like with compiler warnings/errors for Rust? Because if so, we could basically make a note for each usage of that useless lifetime in similar wording to the former case.

oli-obk commented 6 years ago

The existing unused_lifetime lint should be extended. The best thing would be to add a suggestion (with the span_suggestion method) that replaces each lifetime with 'static. You probably need to record the spans of all lifetimes, because I think this isn't happening today.