5

Null fn lints by Niki4tap · Pull Request #10099 · rust-lang/rust-clippy · GitHub

 1 year ago
source link: https://github.com/rust-lang/rust-clippy/pull/10099
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.
neoserver,ios ssh client

Contributor

@Niki4tap Niki4tap commented Dec 18, 2022

edited by xFrednet

Adds lints to check for code, that assumes nullable fn().

Lint examples:

transmute_null_to_fn:

error: transmuting a known null pointer into a function pointer
  --> $DIR/transmute_null_to_fn.rs:9:23
   |
LL |         let _: fn() = std::mem::transmute(std::ptr::null::<()>());
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this transmute results in undefined behavior
   |
   = help: try wrapping your function pointer type in `Option<T>` instead, and using `None` as a null pointer value

fn_null_check:

error: function pointer assumed to be nullable, even though it isn't
  --> $DIR/fn_null_check.rs:13:8
   |
LL |     if (fn_ptr as *mut ()).is_null() {}
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value

Closes #1644


changelog: Improvement: [transmuting_null]: Now detects const pointers to all types
#10099
changelog: New lint: [transmute_null_to_fn]
#10099
changelog: New lint: [fn_null_check]
#10099


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK