Fixesrust-lang/rust-clippy#1966.
Now the program checks for transmutting from a struct containing a
single raw pointer to a reference.
```Rust
struct Foo(*const i32);
fn foo(foo: Foo) -> &i32 {
unsafe { transmute(foo) }
}
```
changelog: [`transmute_ptr_to_ref`]: now checks for a pointer wrapped in
a struct
Fixesrust-lang/rust-clippy#16019 by reborrowing, instead of using
by_ref, when the iterator expression is not `Sized`.
```
changelog: [`while_let_on_iterator`]: fixes broken suggestion by using reborrow instead of `by_ref` for references to traits that are not `Sized`
```
Now the program checks for transmutting from a struct containing a
single raw pointer to a raw pointer.
changelog: [`transmute_ptr_to_ptr`]: now checks for a pointer wrapped in
a struct
Now the program checks for transmutting from a struct containing a
single raw pointer to a reference.
```Rust
struct Foo(*const i32);
fn foo(foo: Foo) -> &i32 {
unsafe { transmute(foo) }
}
```
changelog: [`transmute_ptr_to_ref`]: now checks for a pointer wrapped in
a struct
Related to
https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast
(and very much modelled off of its implementation).
This lint is motivated by cleaning up the output of
[c2rust](https://c2rust.com/), which contains many `.offset` calls with
by literal. The lint also nicely handles offsets by zero, which are
unnecessary.
I'm aware that the feature freeze is still in place, but this was
top-of-mind now. I'll patiently wait until the freeze is lifted.
changelog: [`ptr_offset_by_literal`]: add `ptr_offset_by_literal` lint
Currently the `sugg` utility treats all `ExprKind::Match` expressions as
potentially needing brackets, and therefore many lints will add
parenthesis around them. However this includes desugared match
expressions like the `?` and `.await` operators.
In this PR I have updated the utility to only treat match expressions
which include a code block as needing parenthesis, as the other types
have similar precedence rules and expectations to things like member
access and I think can be treated like as such.
While this change is small on paper it touches a large amount of code
due to changing a cross cutting concern, I am happy to add additional
tests if we think it is needed, but I wanted to get a feel for if this
is even a sensible change to be doing and what the expectations were
around the level of testing needed before investing more time into it.
Regarding not putting a specific lint in the changelog, determining all
the lints this could possibly effect would be possible but take some
time, and I wonder if it would be a bit too noisy in the changelog. Open
to suggestions about how best to address that.
fixesrust-lang/rust-clippy#16045
changelog: stop inserting unnecessary brackets around `x?` and `x.await`
expressions in suggestions
This means some existing test cases won't get linted anymore, but imo
that's preferable to false-positives.
Fixesrust-lang/rust-clippy#14548
changelog: [`equatable_if_let`]: don't lint if pattern or initializer
come from expansion
Warn against calls which mutate an interior mutable `const`-item
## `const_item_interior_mutations`
~~`interior_mutable_const_item_mutations`~~
~~`suspicious_mutation_of_interior_mutable_consts`~~
*warn-by-default*
The `const_item_interior_mutations` lint checks for calls which mutates an interior mutable const-item.
### Example
```rust
use std::sync::Once;
const INIT: Once = Once::new(); // using `INIT` will always create a temporary and
// never modify it-self on use, should be a `static`
// instead for shared use
fn init() {
INIT.call_once(|| {
println!("Once::call_once first call");
});
}
```
```text
warning: mutation of an interior mutable `const` item with call to `call_once`
--> a.rs:11:5
|
11 | INIT.call_once(|| {
| ^---
| |
| _____`INIT` is a interior mutable `const` item of type `std::sync::Once`
| |
12 | | println!("Once::call_once first call");
13 | | });
| |______^
|
= note: each usage of a `const` item creates a new temporary
= note: only the temporaries and never the original `const INIT` will be modified
= help: for more details on interior mutability see <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: `#[warn(const_item_interior_mutations)]` on by default
help: for a shared instance of `INIT`, consider making it a `static` item instead
|
6 - const INIT: Once = Once::new(); // using `INIT` will always create a temporary and
6 + static INIT: Once = Once::new(); // using `INIT` will always create a temporary and
|
```
### Explanation
Calling a method which mutates an interior mutable type has no effect as const-item are essentially inlined wherever they are used, meaning that they are copied directly into the relevant context when used rendering modification through interior mutability ineffective across usage of that const-item.
The current implementation of this lint only warns on significant `std` and `core` interior mutable types, like `Once`, `AtomicI32`, ... this is done out of prudence and may be extended in the future.
----
This PR is an targeted alternative to rust-lang/rust#132146. It avoids false-positives by adding an internal-only attribute `#[rustc_should_not_be_called_on_const_items]` on methods and functions that mutates an interior mutale type through a shared reference (mutable refrences are already linted by the `const_item_mutation` lint).
It should also be noted that this is NOT an uplift of the more general [`clippy::borrow_interior_mutable_const`](https://rust-lang.github.io/rust-clippy/master/index.html#/borrow_interior_mutable_const) lint, which is a much more general lint regarding borrow of interior mutable types, but has false-positives that are completly avoided by this lint.
A simple [GitHub Search](https://github.com/search?q=lang%3Arust+%2F%28%3F-i%29const+%5Ba-zA-Z0-9_%5D*%3A+Once%2F&type=code) reveals many instance where the user probably wanted to use a `static`-item instead.
----
````@rustbot```` labels +I-lang-nominated +T-lang
cc ````@traviscross````
r? compiler
Fixes [IRLO - Forbidding creation of constant mutexes, etc](https://internals.rust-lang.org/t/forbidding-creation-of-constant-mutexes-etc/19005)
Fixes https://github.com/rust-lang/rust/issues/132028
Fixes https://github.com/rust-lang/rust/issues/40543
This is blocking rust-lang/rust-clippy#14724 as it triggers the debug
assertions it adds.
The inter item span parsing was introduced to solve
rust-lang/rust-clippy#12197. This is better handled by just not linting
anything within bodies.
changelog: [`missing_docs_in_private_items`]: Don't lint items in bodies
and automatically derived impls
changelog: [`missing_docs_in_private_items`]: Better detect when things
are accessible from the crate root
changelog: [`missing_docs_in_private_items`]: Lint unnameable items
which are accessible outside the crate