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.5k stars 1.55k forks source link

Suggest ptr::null, ptr::null_mut over casting. #1047

Closed Stebalien closed 7 years ago

Stebalien commented 8 years ago

Add a lint to suggest converting all instances of:

0 as *const T;
0 as *mut T;

To:

std::ptr::null();
std::ptr::null_mut();

See https://github.com/rust-lang/rust/pull/34456

oli-obk commented 8 years ago

These are also in core. We should probably figure out whether no_std is set and suggest core::ptr::* in that case.

mcarton commented 8 years ago

We should probably figure out whether no_std is set and suggest core::ptr::* in that case.

This could be done for a lot a paths in suggestions.

llogiq commented 8 years ago

Sounds like we could use a utility function that walks the parent chain to look for #[no_std].

mcarton commented 8 years ago

You don't even need to walk the parent chain, the current crate is available as LateContext::krate which has attributes.

llogiq commented 8 years ago

True. I had forgotten that this attribute is crate-level only.

alilleybrinker commented 8 years ago

I'm happy to give this a shot.

mcarton commented 8 years ago

Great! Feel free to ask if you have any question.