Commit Graph

9215 Commits

Author SHA1 Message Date
abd002 aefb01b852 Fix clippy::ref_as_ptr for non-temporary references in let/const 2025-12-13 19:27:43 +02:00
Alex Macleod 961ea1c46b feat(manual_saturating_arithmetic): lint x.checked_sub(y).unwrap_or_default() (#15845)
Resolves https://github.com/rust-lang/rust-clippy/issues/15655

changelog: [`manual_saturating_arithmetic`]: lint
`x.checked_sub(y).unwrap_or_default()`
2025-12-10 15:53:23 +00:00
Alex Macleod 06ded880d2 fix(match_like_matches_macro): FP with guards containing if let (#15876)
Fixes https://github.com/rust-lang/rust-clippy/issues/15841

changelog: [`match_like_matches_macro`]: FP with guards containing `if
let`
2025-12-10 15:35:51 +00:00
Alex Macleod 16eaf79976 feat(transmute_ptr_to_ref): Handle a pointer wrapped in a struct (#15948)
Fixes rust-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
2025-12-10 15:33:36 +00:00
Samuel Onoja 1232c81a3e Add needless_type_cast lint 2025-12-09 22:56:23 +01:00
llogiq 0cf51b1b73 Fix typo in deprecated lint string_to_string message (#16208)
Closes: rust-lang/rust-clippy#16204

----

changelog: Fix typo in deprecated lint `string_to_string` message
2025-12-09 19:03:30 +00:00
irelaxcn d0e4b5b159 Fix typo in deprecated lint string_to_string message 2025-12-10 01:34:25 +08:00
Jason Newcomb 52fea21d5f Fix tuple_array_conversions FP when binded vars are used before conversion (#16197)
Closes rust-lang/rust-clippy#16192

changelog: [`tuple_array_conversions`] fix FP when binded vars are used
before conversion
2025-12-09 17:05:01 +00:00
Linshu Yang 3551aeb5c3 fix: tuple_array_conversions FP when binded vars are used before conversion 2025-12-09 16:34:40 +00:00
Jason Newcomb 70a4d957ec Fixes #16109: For non-sized types, use reborrow in suggestions to remove while-let loops (#16100)
Fixes rust-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`
```
2025-12-09 03:44:14 +00:00
Jason Newcomb d5d644f400 refactor(missing_asserts_for_indexing): overhaul diagnostics (#16120)
changelog: [`missing_asserts_for_indexing`]: overhaul diagnostics
2025-12-08 13:34:15 +00:00
dswij 9e970d7c05 Fix useless_conversion wrongly unmangled macros (#16171)
Closes rust-lang/rust-clippy#16165

changelog: [`useless_conversion`] fix wrongly unmangled macros
2025-12-07 10:41:42 +00:00
irelaxcn c788c7cb77 Fix map_entry FP when it would cause MutexGuard to be held across an
await point.
2025-12-07 00:57:31 +08:00
llogiq c59e7a9a82 Fix panicking_unwrap FP on field access with implicit deref (#16196)
Closes rust-lang/rust-clippy#16188

----

changelog: [`panicking_unwrap`] fix FP on field access with implicit
deref
2025-12-05 18:51:48 +00:00
Linshu Yang c905503474 fix: panicking_unwrap FP on field access with implicit deref 2025-12-05 18:18:58 +00:00
Ada Alakbarova d6b561376e fix(len_without_is_empty): allow is_empty(&self) with len(&mut self) 2025-12-05 14:03:02 +01:00
yanglsh f2af204d17 fix: zero_repeat_side_effects misses curlies 2025-12-02 19:44:57 +00:00
Alex Macleod a10cafebcd feat(manual_ilog2): new lint (#15865)
Revival of https://github.com/rust-lang/rust-clippy/pull/13331

Part of https://github.com/rust-lang/rust-clippy/issues/12894

changelog: [`manual_ilog2`]: new lint
2025-12-02 19:08:25 +00:00
Linshu Yang e4b8c5a6a5 fix: useless_conversion wrongly unmangled macros 2025-12-01 22:27:51 +00:00
ceptontech e9ceae44b7 feat(transmute_ptr_to_ptr): Handle a pointer wrapped in a struct
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
2025-12-01 10:12:36 -08:00
ceptontech 5ac265676a feat(transmute_ptr_to_ref): Handle 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
2025-12-01 10:12:36 -08:00
llogiq 65c339e711 add ptr_offset_by_literal lint (#15606)
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
2025-11-30 13:15:07 +00:00
Folkert de Vries a6162c3dd9 add ptr_offset_by_literal lint 2025-11-30 13:52:49 +01:00
Artur Sulej 9cb6306a48 New lint: decimal_bitwise_operands 2025-11-29 21:39:35 +01:00
Sergio Giro bbadb72919 while_let_on_iterator: consider all deref adjustments for by_ref 2025-11-29 18:40:32 +00:00
Philipp Krones 181601c342 Merge remote-tracking branch 'upstream/master' into rustup 2025-11-28 20:09:43 +01:00
Ada Alakbarova e6ad406d48 feat(manual_ilog2): new lint 2025-11-28 10:34:37 +01:00
Ada Alakbarova f3b905d4a8 use span_suggestion_verbose 2025-11-27 22:36:43 +01:00
Ada Alakbarova 4b021f6b62 point the suggestion at each individual span
This makes the labels redundant.
2025-11-27 22:36:17 +01:00
Ada Alakbarova 9ced0f5892 use note_once
This is a general advice, and so shouldn't be repeated
2025-11-27 22:33:03 +01:00
Alex Macleod e567e30a93 refactor(byte_char_slices): clean-up (#15985)
changelog: none
2025-11-27 19:26:37 +00:00
Alex Macleod a6dfbb3524 Stop inserting redundant parenthesis around desugared match expressions (#16102)
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.

fixes rust-lang/rust-clippy#16045

changelog: stop inserting unnecessary brackets around `x?` and `x.await`
expressions in suggestions
2025-11-27 18:49:07 +00:00
dswij df627dd681 fix(equatable_if_let): don't lint if pattern or initializer come from expansion (#15958)
This means some existing test cases won't get linted anymore, but imo
that's preferable to false-positives.

Fixes rust-lang/rust-clippy#14548

changelog: [`equatable_if_let`]: don't lint if pattern or initializer
come from expansion
2025-11-27 15:59:02 +00:00
Linshu Yang 16241e2763 fix: implicit_hasher wrongly unmangled macros 2025-11-23 20:10:03 +00:00
Matthias Krüger e74d96bb71 Rollup merge of #148407 - Urgau:suspicious_int_mutable_consts, r=JonathanBrouwer
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
2025-11-22 18:41:20 +01:00
Urgau bf0e3d9fe3 Allow const_item_interior_mutations in Clippy tests 2025-11-22 14:48:40 +01:00
Samuel Tardieu 45168a79cd Fix wrongly unmangled macros for transmute_ptr_to_ptr and transmute_bytes_to_str (#16105)
Closes rust-lang/rust-clippy#16104

changelog: [`transmute_ptr_to_ptr`] fix wrongly unmangled macros
[`transmute_bytes_to_str`] fix wrongly unmangled macros
2025-11-22 07:50:52 +00:00
Samuel Tardieu 37b485bbee feat(explicit_deref_methods): don't lint in impl Deref(Mut) (#16113)
Resolves https://github.com/rust-lang/rust-clippy/issues/15392
Resurrection of https://github.com/rust-lang/rust-clippy/pull/15688

changelog: [`explicit_deref_methods`]: don't lint in `impl Deref(Mut)`
2025-11-21 16:24:34 +00:00
mu001999 1f7ac87512 Emit error when using path-segment keyword as cfg pred 2025-11-21 18:48:04 +08:00
Ada Alakbarova 81ce47af57 feat(explicit_deref_methods): don't lint in impl Deref(Mut) 2025-11-21 07:24:26 +01:00
llogiq 93a4992789 Fix useless_asref suggests wrongly when used in ctor (#16115)
Closes rust-lang/rust-clippy#16098

changelog: [`useless_asref`] fix wrong suggestions when used in ctor
2025-11-20 12:52:44 +00:00
Linshu Yang e4f50906c3 fix: transmute_ptr_to_ptr wrongly unmangled macros 2025-11-20 00:09:31 +00:00
Linshu Yang 1a86849543 fix: useless_asref suggests wrongly when used in ctor 2025-11-18 20:12:23 +00:00
Jason Newcomb 24e16f992c Taking a raw pointer on a union field is a safe operation (#16079)
changelog: [`multiple_unsafe_ops_per_block`]: taking a raw pointer on a
union field is a safe operation

Fixes rust-lang/rust-clippy#16076

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_SUMMARY_START -->

### Summary Notes

- [Beta
nomination](https://github.com/rust-lang/rust-clippy/pull/16079#issuecomment-3524699179)
by [samueltardieu](https://github.com/samueltardieu)

*Managed by `@rustbot`—see
[help](https://forge.rust-lang.org/triagebot/note.html) for details*

<!-- TRIAGEBOT_SUMMARY_END -->
<!-- TRIAGEBOT_END -->
2025-11-18 16:46:24 +00:00
Samuel Tardieu 11636bb838 Taking a raw pointer on a union field is safe since Rust 1.92 2025-11-18 10:12:34 +01:00
Samuel Tardieu 0ea973e49c Fix adjustments 2025-11-18 09:21:21 +01:00
Linshu Yang e4b3f01c07 fix: transmute_bytes_to_str wrongly unmangled macros 2025-11-18 00:35:38 +00:00
robert-mccausland 466646f54f test: add test case to cast_possible_wrap 2025-11-17 20:07:55 +00:00
robert-mccausland 2d4c45ff62 feat: change desuragered match suggestions to NonParen 2025-11-17 19:50:35 +00:00
Timo 91d7c5730a Rework missing_docs_in_private_items (#14741)
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
2025-11-17 18:40:05 +00:00