Commit Graph

9554 Commits

Author SHA1 Message Date
Vishruth-Thimmaiah
23e602cd94 fix: correct suggestion for significant_drop_in_scrutinee in expressions
fixes: #13986
2025-01-17 23:54:42 +05:30
Samuel Tardieu
bbd58d19d4 Do not trigger [size_of_in_element_count] for u8
Counting in bytes for a pointer to `u8` is legitimate and must not
trigger the lint. Also, this prevents linting the
`{std,core}::ptr::write_bytes` as it manipulates bytes.
2025-01-17 00:43:36 +01:00
Samuel Tardieu
ded9354dcf Suggest using Vec::extend() in same_item_push
Using `Vec::extend(std::iter::repeat_n(item, N))` allows to use the more
natural number of elements to add `N`, as is probably done in the original
loop, instead of computing the difference between the existing number of
elements and the wanted one.

Before MSRV 1.82, the older suggestion to use `Vec::resize()` is still
issued.
2025-01-15 22:50:25 +01:00
Guillaume Gomez
00104a4167 Add more detailed explanations for #13885 regression test 2025-01-15 16:42:56 +01:00
Guillaume Gomez
e31493b9b8 Rollup merge of #135003 - RalfJung:deprecate-allowed-through-unstable, r=davidtwco
deprecate `std::intrinsics::transmute` etc, use `std::mem::*` instead

The `rustc_allowed_through_unstable_modules` attribute lets users call `std::mem::transmute` as `std::intrinsics::transmute`. The former is a reexport of the latter, and for a long time we didn't properly check stability for reexports, so making this a hard error now would be a breaking change for little gain. But at the same time, `std::intrinsics::transmute` is not the intended path for this function, so I think it is a good idea to show a deprecation warning when that path is used. This PR implements that, for all the functions in `std::intrinsics` that carry the attribute.

I assume this will need ``@rust-lang/libs-api`` FCP.
2025-01-15 16:30:11 +01:00
Jacob Pratt
1361c1f006 Rollup merge of #132397 - m-ou-se:warn-missing-abi, r=Nadrieril
Make missing_abi lint warn-by-default.

This makes the missing_abi lint warn-by-default, as suggested here: https://github.com/rust-lang/rfcs/pull/3722#issuecomment-2447719047

This needs a lang FCP.
2025-01-15 04:08:10 -05:00
Ralf Jung
caebd67ba9 intrinsics: deprecate calling them via the unstable std::intrinsics path 2025-01-15 09:41:33 +01:00
lapla-cogito
544f71f48d add manual_repeat_n lint 2025-01-15 13:15:35 +09:00
Manish Goregaokar
25d319d1b2 New lint useless-nonzero-new_unchecked (#13993)
changelog: [`useless-nonzero-new_unchecked`]: new lint

Close #13991

### What it does

Checks for `NonZero*::new_unchecked(<literal>)` being used in a `const`
context.

### Why is this bad?

Using `NonZero*::new_unchecked()` is an `unsafe` function and requires
an `unsafe` context. When used with an
integer literal in a `const` context, `NonZero*::new().unwrap()` will
provide the same result with identical
runtime performances while not requiring `unsafe`.

### Example
```no_run
const PLAYERS: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(3) };
```
Use instead:
```no_run
const PLAYERS: NonZeroUsize = NonZeroUsize::new(3).unwrap();
```
2025-01-15 01:00:54 +00:00
Alex Macleod
98761e4812 Rust 1.81 and later support elision with explicit self types (#13992)
Commit 9ef6e2199c introduced a check to
ensure that Clippy doesn't consider a lifetime present in an explicit
self types as being the default for an elided output lifetime. For
example, elision did not work in the case like:

```rust
  fn func(self: &Rc<Self>, &str) -> &str { … }
```

Since Rust 1.81.0, the lifetime in the self type is now considered the
default for elision. Elision should then be suggested when appropriate.

changelog: [`needless_lifetimes`]: suggest elision of lifetimes present
in explicit self types as well

r? @Alexendoo
because of #8278
2025-01-14 22:42:23 +00:00
Alejandra González
7e83ec57c6 Suggest manual_div_ceil even when right operand is a constant (#13951)
changelog: [`manual_div_ceil`]: lint constants as well

Fix #13950
2025-01-14 19:06:32 +00:00
dswij
8c1ea9fa01 Do not look for significant drop inside .await expansion (#13985)
Temporaries created inside the expansion of `.await` will be dropped and
need no checking. Looking inside the expansion will trigger false
positives.

changelog: [`significant_drop_in_scrutinee`]: do not falsely warn for
temporaries created by `.await` expansion

Fix #13927
2025-01-14 08:56:39 +00:00
Samuel Tardieu
35dbaf8a61 New lint useless-nonzero-new_unchecked 2025-01-13 23:38:29 +01:00
Samuel Tardieu
8b7cfc75dd Rust 1.81 and later support elision with explicit self types
Commit 9ef6e2199c introduced a check to
ensure that Clippy doesn't consider a lifetime present in an explicit
self type as being the default for an elided output lifetime. For
example, elision did not work in the case like:

```rust
  fn func(self: &Rc<Self>, &str) -> &str { … }
```

Since Rust 1.81.0, the lifetime in the self type is now considered
the default for elision. Elision should then be suggested when
appropriate.
2025-01-13 23:34:19 +01:00
Samuel Tardieu
dc23fa5e6c Suggest manual_div_ceil even when right operand is a constant 2025-01-13 19:29:02 +01:00
Philipp Krones
6ab6c3c809 Select Rust edition 2024 for compiling Clippy (#13751)
The Cargo feature is no longer experimental and is enabled by default in
our nightly compiler.

changelog: Clippy now uses Rust edition 2024
2025-01-13 18:05:05 +00:00
Alejandra González
dcbe3adc4b auto-fix slow_vector_initialization in some cases (#13947)
changelog: [`slow_vector_initialization`]: auto-fix when appropriate

I made a change for `slow_vector_initialization` lint suggestion to use
`vec!` with size and remove the unneeded `resize` (or similar one) call
in #13912, while only the former one was suggested in the previous
implementation. Now, I think this lint can be automatically fixed with
no unnecessary code in some cases. I wrote “in some cases” because if
there are comments between vector declaration and `resize`, Clippy
shouldn't apply auto-fix because the comment may informational.
2025-01-13 15:12:01 +00:00
Samuel Tardieu
0456e4d6a1 In edition 2024, gen is a reserved keyword 2025-01-13 15:59:34 +01:00
Samuel Tardieu
a73166872d In edition 2024, std::env::set_var is unsafe 2025-01-13 15:58:11 +01:00
lapla-cogito
65b95a2cfb fix escaping problem in write_literal and print_literal lint 2025-01-13 12:51:41 +09:00
Timo
8f257c71a3 don't suggest to use cloned for Cow in unnecessary_to_owned (#13853)
fix #13624

changelog: [`unnecessary_to_owned`]: don't suggest to use `cloned` on
`Cow` in `unnecessary_to_owned`
2025-01-12 21:27:10 +00:00
Catherine Flores
d648cc9a2c Do not trigger redundant_pub_crate in external macros (#13952)
Some widely used crates, such as `pin-project-lite`, make use of a
`pub(crate)` construct in a private module inside a public macro. This
makes unrelated project trigger the lint.

There is also an unfortunate situation for Clippy itself: when a new
version of `pin-project-lite` or similar lint-trigerring crates is
released, those lints which can be found in hundreds of occurrences in
dependent crates will change, and appear as diffs in unrelated Clippy PR
because the base lintcheck run will be cached with the ancient release
of the crates. We currently have the situation
[here](https://github.com/rust-lang/rust-clippy/actions/runs/12635410895?pr=13851#user-content-redundant-pub-crate-removed),
which 219 lints removed and 219 lints added because of a
`pin-project-lite` version change between runs, and the fact that
`redundant_pub_crate` triggers on external macros.

Also:
- Fix #10636
- Fix #12213

changelog: [`redundant_pub_crate`]: do not trigger on external macros
2025-01-12 15:15:51 +00:00
Samuel Tardieu
5f75715398 Do not trigger redundant_pub_crate in external macros 2025-01-12 09:24:04 +01:00
Catherine Flores
34d109fd26 Add new lint unneeded_struct_pattern (#13465)
Closes #13400.

changelog: [`unneeded_struct_pattern`]: Add new lint
2025-01-12 02:09:18 +00:00
Timo
a895bebcd7 add more test coverage for #11230 (#13915)
Closes #11230

changelog:  none
2025-01-11 23:28:57 +00:00
Samuel Tardieu
0b402baf15 Do not look for significant drop inside .await expansion
Temporaries created inside the expansion of `.await` will be dropped and need
no checking. Looking inside the expansion will trigger false positives.
2025-01-11 14:09:07 +01:00
Alejandra González
579571d9cf New lint: manual_ok_err (#13740)
changelog: [`manual_ok_err`]: new lint

Detect manual implementations of `.ok()` or `.err()`, as in

```rust
let a = match func() {
    Ok(v) => Some(v),
    Err(_) => None,
};
let b = if let Err(v) = func() {
    Some(v)
} else {
    None
};
```

which can be replaced by

```rust
let a = func().ok();
let b = func().err();
```

This pattern was detected in the wild in the Rust reimplementation of
coreutils:
https://github.com/uutils/coreutils/pull/6886#pullrequestreview-2465160137
2025-01-10 19:19:58 +00:00
Samuel Tardieu
4a69d0d4d8 New lint: manual_ok_err 2025-01-10 18:26:01 +01:00
Fridtjof Stoldt
5c2601af15 Do not propose to elide lifetimes if this causes an ambiguity (#13929)
Some lifetimes in function return types are not bound to concrete
content and can be set arbitrarily. Clippy should not propose to replace
them by the default `'_` lifetime if such a lifetime cannot be
determined unambigously.

I added a field to the `LifetimeChecker` and `Usage` to flag lifetimes
that cannot be replaced by default ones, but it feels a bit hacky.

Fix #13923

changelog: [`needless_lifetimes`]: remove false positives by checking
that lifetimes can indeed be elided
2025-01-10 11:58:29 +00:00
Philipp Krones
d0a74af979 Merge commit '19e305bb57a7595f2a8d81f521c0dd8bf854e739' into clippy-subtree-update 2025-01-09 18:57:00 +01:00
Quentin Santos
d5264c7a46 Check for needless uses of str::bytes()
This builds upon the lint for `str::as_bytes()`, and also covers
needless uses of the iterator version `str::bytes()`.
2025-01-09 18:44:43 +01:00
Philipp Krones
b5bf09e57a Merge remote-tracking branch 'upstream/master' into rustup 2025-01-09 18:00:37 +01:00
Alex Macleod
894e87cd51 Fix type suggestion for manual_is_ascii_check (#13916)
Fixes #13913 .

changelog: [`manual_is_ascii_check`]: fix type suggestions for
references

Previously it only derived `char` and `u8` types, now it should always
annotate the lambda parameter with the correct type (e.g. `&char`).

I'm quite new to Rust and this is my first contact with clippy, so I'm
open for suggetions :)
2025-01-08 13:36:11 +00:00
Samuel Tardieu
c686ffd193 Do not propose to elide lifetimes if this causes an ambiguity
Some lifetimes in function return types are not bound to concrete
content and can be set arbitrarily. Clippy should not propose to replace
them by the default `'_` lifetime if such a lifetime cannot be
determined unambigously.
2025-01-08 12:50:00 +01:00
Oli Scherer
0faf8c7c62 Rename PatKind::Lit to Expr 2025-01-08 07:34:59 +00:00
Oli Scherer
28d2363de8 Exhaustively handle expressions in patterns 2025-01-08 07:33:46 +00:00
Mara Bos
9ad75b327a Update tests. 2025-01-07 16:04:14 +01:00
Guillaume Gomez
a7fb37c1d8 Correctly handle expanded macros for literal_string_with_formatting_args lint 2025-01-07 15:55:41 +01:00
Guillaume Gomez
17f9344a96 Fix literal_string_with_formatting_args lint emitted when it should not 2025-01-07 15:35:21 +01:00
Alex Macleod
f5ca68f9db Do not remove identity mapping if mandatory mutability would be lost (#13905)
Removing `.map(identity)` may result in invalid code if the receiver of
`map()` is an immutable binding, and the result of `map()` is used as
the receiver of a method call expecting a mutable reference.

Fix #13904

changelog: [`map_identity`]: do not lint if this would cause mandatory
mutability to be lost
2025-01-07 13:43:46 +00:00
Alex Macleod
b7b69b1354 Remove unneeded parentheses in unnecessary_map_or lint output (#13932)
When the expression is transformed into an equality, parentheses are
needed only if the resulting equality is used:

- as a receiver in a method call
- as part of a binary or unary expression
- as part of a cast

In other cases, which will be the majority, no parentheses are required.
This makes the lint suggestions cleaner.

changelog: `none`
2025-01-07 13:37:52 +00:00
Fridtjof Stoldt
d0d5b8a34a Don't emit machine applicable map_flatten lint if there are code comments (#13940)
Fixes https://github.com/rust-lang/rust-clippy/issues/8528.

Similar to #13911, if there are code comments, we don't want to remove
them automatically.

changelog: Don't emit machine applicable `map_flatten` lint if there are
code comments

r? @xFrednet
2025-01-07 09:09:19 +00:00
Manish Goregaokar
0e2505ba8f Do not trigger missing_const_for_fn for tests (#13945)
Close #13938

changelog: [`missing_const_for_fn`]: do not trigger for tests
2025-01-06 19:47:54 +00:00
Guillaume Gomez
78225ccca8 Add regression test for #8528 2025-01-06 17:35:55 +01:00
Alexey Semenyuk
ca55534c92 Do not trigger clippy::missing_const_for_fn triggering for tests 2025-01-06 02:07:16 +05:00
lapla-cogito
39269aaaae auto-fix slow_vector_initialization 2025-01-05 21:53:12 +09:00
Fridtjof Stoldt
ad69c65906 Only emit useless_vec suggestion if the macro does not contain code comments (#13911)
Fixes #13692.

If the `vec!` macro call contains comments, we should not provide
suggestions and let users handle it however they see fit.

changelog: Only emit `useless_vec` suggestion if the macro does not
contain code comments
2025-01-04 23:33:30 +00:00
Alejandra González
54f88c3c75 [needless_continue]: lint if the last stmt in loop is continue recurisvely (#13891)
fixes: #4077

Continuation of #11546. r? @y21 if you don't mind?

changelog: [`needless_continue`] lint if the last stmt in loop is
`continue` recurisvely
2025-01-04 21:52:42 +00:00
Guillaume Gomez
b76e0426a1 Add regression test for useless_vec with code comments 2025-01-03 22:55:25 +01:00
Ralf Jung
ad36f2b053 turn rustc_box into an intrinsic 2025-01-03 12:01:31 +01:00