add lint deref_nullptr detecting when a null ptr is dereferenced
fixes#83856
changelog: add lint that detect code like
```rust
unsafe {
&*core::ptr::null::<i32>()
};
unsafe {
addr_of!(std::ptr::null::<i32>())
};
let x: i32 = unsafe {*core::ptr::null()};
let x: i32 = unsafe {*core::ptr::null_mut()};
unsafe {*(0 as *const i32)};
unsafe {*(core::ptr::null() as *const i32)};
```
```
warning: Dereferencing a null pointer causes undefined behavior
--> src\main.rs:5:26
|
5 | let x: i32 = unsafe {*core::ptr::null()};
| ^^^^^^^^^^^^^^^^^^
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
|
= note: `#[warn(deref_nullptr)]` on by default
```
Limitation:
It does not detect code like
```rust
const ZERO: usize = 0;
unsafe {*(ZERO as *const i32)};
```
or code where `0` is not directly a literal
rustdoc: links from items in a trait impl are inconsistent
Depending on where the struct implementing a trait is coming from, or the current page, the items in a trait impl are not linking to the same thing:
|item| trait page, implementors| trait page, implementations on Foreign Types|struct page, trait implementations|
|-|-|-|-|
|function| link to current impl|link to first impl in the list|link to trait def
|default function | not present |not present |link to trait def
|default function with custom impl|link to current impl|link to trait def |link to trait def
|constant| link to current impl|link to trait def |link to trait def
|associated type| link to current impl|link to trait def |link to trait def
||*missing link to trait def*|*function link wrong + missing link to current impl*|*missing link to current impl*|
<details>
<summary>rust code with those cases</summary>
```rust
pub trait MyTrait {
type Assoc;
const VALUE: u32;
fn trait_function(&self);
fn defaulted(&self) {}
fn defaulted_override(&self) {}
}
impl MyTrait for String {
/// will link to trait def
type Assoc = ();
/// will link to trait def
const VALUE: u32 = 5;
/// will link to first foreign implementor
fn trait_function(&self) {}
/// will link to trait def
fn defaulted_override(&self) {}
}
impl MyTrait for Vec<u8> {
/// will link to trait def
type Assoc = ();
/// will link to trait def
const VALUE: u32 = 5;
/// will link to first foreign implementor
fn trait_function(&self) {}
/// will link to trait def
fn defaulted_override(&self) {}
}
impl MyTrait for MyStruct {
/// in trait page, will link to current impl
///
/// in struct page, will link to trait def
type Assoc = bool;
/// in trait page, will link to current impl
///
/// in struct page, will link to trait def
const VALUE: u32 = 20;
/// in trait page, will link to current impl
///
/// in struct page, will link to trait def
fn trait_function(&self) {}
/// in trait page, will link to current impl
///
/// in struct page, will link to trait def
fn defaulted_override(&self) {}
}
pub struct MyStruct;
```
</details>
In this PR, I fixed all links to target the trait definition, and added an anchor-link to the current implementation appearing on mouse hover.
Avoid an `Option<Option<_>>`
By simply swapping the calls to `map` and `and_then` around the complexity of
handling an `Option<Option<_>>` disappears.
`@rustbot` modify labels +C-cleanup +T-compiler
Improve code example for length comparison
Small fix/improvement: it's much safer to check that you're under the length of an array rather than chacking that you're equal to it. It's even more true in case you update the length of the array while iterating.
Fix typo in error message
Also tweaked the message a bit by
- removing the hyphen, because in my opinion the hyphen makes the
message a bit harder to read, especially combined with the backticks;
- adding the word "be", because I think it's a bit clearer that way.
Update RELEASES.md
A couple of things that were missing in the release notes:
- `Div` and `Rem` by their `NonZero` variant is now implemented for all unsigned integers (#79134)
- Stabilization of `VecDeque::range` and `VecDeque::range_mut` (#79022, stabilization version corrected to 1.51.0 #80448)
- Deprecation of `spin_loop_hint` (#80966)
Check for asm support in UI tests that require it
Add `needs-asm-support` compiletest directive, and use it in asm tests
that require asm support without relying on any architecture specific
features.
Closes#84038.
Stabilize `bufreader_seek_relative`
This PR marks `BufReader::seek_relative` as stable - the associated issue, #31100, has passed the final comment period without any issues, and from what I understand, the only thing left to stabilize this is to submit a PR marking the method as stable.
Closes#31100.
Turn old edition lint (anonymous-parameters) into warn-by-default on 2015
This makes `anonymous_parameters` <s>and `keyword_idents` </s>warn-by-default on the 2015 edition. I would also like to do this for `absolute_paths_not_starting_with_crate`, but I feel that case is slightly less clear-cut.
Note that this only affects code on the 2015 edition, such code is illegal in future editions anyway.
This was spurred by https://github.com/dtolnay/syn/issues/972: old edition syntax breaks tooling (like syn), and while the tooling should be free to find its balance on how much to support prior editions, it does seem like we should be nudging such code towards the newer edition, and we can do that by turning this Allow lint into a Warn.
In general, I feel like migration lints from an old edition should be made Warn after a year or so, and idiom lints for the new edition should be made Warn after a couple months.
cc `@m-ou-se,` this is for stuff from the 2015-2018 migration but you might be interested.
Also tweaked the message a bit by
- removing the hyphen, because in my opinion the hyphen makes the
message a bit harder to read, especially combined with the backticks;
- adding the word "be", because I think it's a bit clearer that way.
Update stdarch submodule (to before it switched to const generics)
https://github.com/rust-lang/rust/pull/83278#issuecomment-812389823: This unblocks #82539.
Major changes:
- More AVX-512 intrinsics.
- More ARM & AArch64 NEON intrinsics.
- Updated unstable WASM intrinsics to latest draft standards.
- std_detect is now a separate crate instead of a submodule of std.
I double-checked and the first use of const generics looks like 8d5017861e, which isn't included in this PR.
r? `@Amanieu`
This also includes a cherry-pick of
ec1461905b
and https://github.com/rust-lang/stdarch/pull/1108 to fix a build
failure.
It also adds a re-export of various macros to the crate root of libstd -
previously they would show up automatically because std_detect was defined
in the same crate.