Commit graph

22394 commits

Author SHA1 Message Date
Cornelius Roemer
8858b35c66 Fix a few more clippy::versions 2025-04-27 19:12:40 +02:00
Cornelius Roemer
0b27121212 Fix clippy::version of elidable_lifetime_names to 1.87
The lint becomes part of stable in 1.87, not in 1.84
2025-04-19 17:20:26 +02:00
Timo
781fdab9a9
Move internal lints to their own crate (#13223)
This makes it so switching the internal feature on/off no longer
rebuilds `clippy_lints`.

r? @flip1995

changelog: none
2025-04-18 12:04:08 +00:00
Timo
bcd76c3384
empty_enum_variants_with_brackets: Do not lint reachable enums and enum variants used as functions in the same crate (#12971)
Fixes #12551

changelog: [`empty_enum_variants_with_brackets`]: Do not lint reachable
enums or enums which are used as functions within the same crate.

r? @xFrednet
2025-04-18 09:45:02 +00:00
Philipp Krones
e294f94448
Rustup (#14647)
r? @ghost

changelog: none
2025-04-17 16:43:03 +00:00
Philipp Krones
8a0b225605
Bump nightly version -> 2025-04-17 2025-04-17 18:38:03 +02:00
Philipp Krones
de90fa42dd
Merge remote-tracking branch 'upstream/master' into rustup 2025-04-17 18:36:59 +02:00
Aneesh K
fa9254feaf
Do not lint reachable enums and enum variants used as functions in the same crate 2025-04-17 21:59:06 +05:30
dswij
94f0994b1b
Check for lifetime uses in closures as well (#14608)
The `BodyLifetimeChecker` which checks for the use of any non-anonymous
non-static lifetime did not recurse into closures, missing lifetime
uses. This would lead to a bogus elision suggestion.

The `BodyLifetimeChecker` is not refined enough to avoid false
positives, as any conforming lifetime, including one coming from the
outer context, would be considered a hit. The number of false positives
might increase now that we check closures as well, in case those
closures define and use lifetimes themselves.

changelog: [`needless_lifetimes`]: do not suggest removing a lifetime
which is later used in a closure

Fixes rust-lang/rust-clippy#14607
2025-04-17 16:14:31 +00:00
Jason Newcomb
26f43ff346
match_single_binding: allow macros in scrutinee and patterns (#14635)
changelog: [`match_single_binding`]: allow macros in scrutinee and
patterns

Fixes rust-lang/rust-clippy#14634
2025-04-17 14:58:53 +00:00
Samuel Tardieu
a49ea2d63d
fix: unnecessary_lazy_evaluations suggests wrongly for async closure (#14644)
Closes rust-lang/rust-clippy#14578

changelog: [`unnecessary_lazy_evaluations`] fix wrong suggestions for
async closure
2025-04-17 14:42:15 +00:00
yanglsh
5a1dbea052 fix: unnecessary_lazy_evaluations suggests wrongly for async closure 2025-04-17 22:00:57 +08:00
Timo
ac88357f83
New lint: swap_with_temporary (#14046)
This lint detects inefficient or useless `{std,core}::mem::swap()` calls
such as:

```rust
    // Should be `a = temp();`
    swap(&mut a, &mut temp());
    // Should be `*b = temp();`
    swap(b, &mut temp());
    // Should be `temp1(); temp2();` if we want to keep the side effects
    swap(&mut temp1(), &mut temp2());
```

It also takes care of using a form appropriate for a `()` context if
`swap()` is part of a larger expression (don't ask me why this wouldn't
happen, I have no idea), by suggesting `{ x = y; }` (statement in block)
or `{std,core}::mem::drop((temp1(), temp2())`.

changelog: [`swap_with_temporary`]: new lint

Close #1968
2025-04-17 12:42:46 +00:00
Samuel Tardieu
05448bd9c7 New lint: swap_with_temporary 2025-04-17 13:17:43 +02:00
Timo
30e9cd5a26
Do not recurse forever in significant_drop_tightening (#14641)
Limit the recursion depth, as each level of nesting adds another deeper
projection.

There might be a more complex way of handling the problem, but infinite
recursions are bad, and don't allow Clippy to terminate properly.

changelog: [`significant_drop_tightening`]: do not recurse forever when
checking for attribute on type or its constituent

Fixes rust-lang/rust-clippy#13544

@rustbot label +L-nursery
2025-04-17 11:09:01 +00:00
Samuel Tardieu
cc6a3d8cd5 Do not recurse forever in significant_drop_tightening
Limit the recursion depth, as each level of nesting adds another deeper
projection.
2025-04-17 12:58:14 +02:00
Philipp Krones
3479fdfe4f
Add note feature to triagebot's config (#14620)
This will let people [add summary
notes](https://forge.rust-lang.org/triagebot/note.html) to an issue, for
example to add that an issue is best kept for first-timers.

changelog: none

I wanted to use it in
https://github.com/rust-lang/rust-clippy/issues/14617#issuecomment-2807090281.
2025-04-17 09:37:22 +00:00
Manish Goregaokar
949b3bcfb5
bool_to_int_with_if: properly handle macros (#14629)
- Do not replace macro results in then/else branches
- Extract condition snippet from the right context
- Make suggestion `MaybeIncorrect` if it would lead to losing comments

changelog: [`bool_to_int_with_if`]: properly handle macros

Fixes rust-lang/rust-clippy#14628
2025-04-17 02:15:21 +00:00
Alejandra González
222660bb36
Build complete usable type from a type-relative prefix (#14586)
Instead of looking for angle brackets in the source code, use the HIR
and Ty interfaces to either copy the original type, or complete it with
`_` placeholders if all type and const generic arguments are inferred.

Fixes rust-lang/rust-clippy#14581

changelog: [`from_iter_instead_of_collect`]: show correct type in
suggestion
2025-04-17 00:09:51 +00:00
Samuel Tardieu
91658a7739
samueltardieu: vacation for ~3 weeks (#14636)
changelog: none

r? ghost
2025-04-16 21:41:21 +00:00
Samuel Tardieu
6c4c3a5e1a samueltardieu: vacation for ~3 weeks 2025-04-16 23:35:28 +02:00
Samuel Tardieu
10a83274e2 match_single_binding: allow macros in scrutinee and patterns 2025-04-16 23:15:20 +02:00
Samuel Tardieu
0bfcd0d3bc bool_to_int_with_if: properly handle macros
- Do not replace macro results in then/else branches
- Extract condition snippet from the right context
- Make suggestion `MaybeIncorrect` if it would lead to losing comments
2025-04-16 20:25:15 +02:00
Samuel Tardieu
8eed35023f
New lint: redundant_test_prefix (#13710)
This PR has started as an effort to proceed from the feedback in
rust-lang/rust-clippy#12861.

- Checks test functions (functions marked with `#[test]` annotation) for
redundant "test_" prefix.
- Auto-fix is supported (and handles collisions gracefully, see below).
- If removing "test_" prefix from, say, `test_foo()` results in a name
collision (either because function `foo()` is already defined within the
current scope, or because the `foo()` call exists within function --
thus creating an unwanted recursion), lint suggests function rename,
warning the user that a simple trimming of `test_` prefix will result in
a name collision.
- If removing "test_" prefix results in invalid identifier (consider
`test_const`, `test_`, `test_42`), then again no auto-fix is suggested,
user is asked to rename function, with a note that a simple prefix
trimming will result in an invalid function name.
(`Applicability::HasPlaceholders` is used and user is suggested to: drop
`test_` prefix + add `_works` suffix, i.e. `test_foo` becomes
`foo_works` -- but again, user has to apply those changes manually).
- If trimmed version of the function name is a valid identifier, doesn't
result in name collision or unwanted recursion, then user is able to run
auto-fix.

fixes rust-lang/rust-clippy#8931

changelog: new lint: [`redundant_test_prefix`]
2025-04-16 06:15:18 +00:00
Alejandra González
08c78e0095
Replace stray println!() in lint code by bug!() (#14618)
To avoid crashing Clippy, the `bug!()` is used only when debug
assertions are enabled. In regular usage, the result will be the same as
before, but without the extra line printed on the standard output which
has the potential for disrupting shell scripts.

changelog: none
2025-04-16 00:13:31 +00:00
Samuel Tardieu
0ef2e75a33 Add note feature to triagebot's config
This will let people [add summary
notes](https://forge.rust-lang.org/triagebot/note.html) to an issue, for
example to add that an issue is best kept for first-timers.
2025-04-15 20:58:35 +02:00
Samuel Tardieu
9663da39d2
Run UI tests in edition 2024 mode (#14602)
The `ui_test` package runs test in edition 2021 mode by default. This PR
switches our tests to edition 2024. The commits progressively make our
test suite compatible with both edition 2021 and edition 2024, then
switches the testing mode to edition 2024, which is compatible with what
`cargo dev lint` also uses by default.

The changes are (without functionality changes in tests):
- Add `unsafe` when [calling unsafe constructs inside `unsafe
fn`](https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-op-in-unsafe-fn.html),
to [unsafe
attributes](https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-attributes.html),
and to [`extern`
blocks](https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-extern.html).
- Use stricter reference patterns to accomodate with the [new match
ergonomics
rules](https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html).
- Add some `use<>` markers where required to keep the same semantics
under the [new RPIT lifetime
rules](https://doc.rust-lang.org/edition-guide/rust-2024/rpit-lifetime-capture.html).

Some other changes ensure that non-regression tests are still enforced:
- Add edition 2021 specific tests when switching rules would make some
explicitly tested constructs untested, or when the test require using
the older [temporary tail expression scoping
rules](https://doc.rust-lang.org/edition-guide/rust-2024/temporary-tail-expr-scope.html).
- In `misnamed_getters`, check that getters containing an `unsafe` block
(for example a getter on an `Union` field) trigger the lint.

The last commit switches the default edition for running UI tests to
edition 2024.

changelog: [`misnamed_getters`]: getters containing an `unsafe` block
are also linted
2025-04-15 18:37:41 +00:00
Samuel Tardieu
c7640e0f83 Run UI tests with edition 2024 by default
The `ui_test` crate still uses 2021 as the default edition for running
rustc-like tests. This creates an unwelcome discrepancy between
`cargo dev lint` which uses Rust 2024 by default, and running UI tests
through `compile-test`.
2025-04-15 20:33:01 +02:00
Samuel Tardieu
ecd20250bc Add search_is_some_fixable_* edition 2021 specific tests 2025-04-15 20:33:01 +02:00
Samuel Tardieu
62a9cad8f1 Add needless_pass_by_ref_mut edition 2021 specific test
There is a specific test that `unsafe fn` are not lint, and that
`unsafe` blocks are not lint either. Since in edition 2024 `unsafe`
blocks are required inside `unsafe fn` to do unsafe things, set up a
specific test for edition 2021.
2025-04-15 20:33:01 +02:00
Samuel Tardieu
cd26de607a misnamed_getters: support unsafe blocks around getters
In edition 2024, `unsafe` blocks must be used inside `unsafe fn` to do
unsafe things. The `misnamed_getters` would not lint if the getter
expression was embedded inside an `unsafe` block.
2025-04-15 20:33:01 +02:00
Samuel Tardieu
20649a87d1 Add use<> markers for edition 2024 compatibility
By default, edition 2024 will capture all types and lifetimes present in
the function signature when using RPIT, while edition 2021 will capture
only the lifetimes present in the RPIT itself. Adding explicit `use<>`
markers will disable the edition-specific automatic rules when they
differ.
2025-04-15 20:33:01 +02:00
Samuel Tardieu
443547425d Use reference patterns compatible with edition 2024
Reference patterns have become stricter in edition 2024. The binding
modes cannot be mixed.
2025-04-15 20:33:01 +02:00
Samuel Tardieu
67d5056f03 Add blocks_in_conditions edition 2021 specific tests
The borrowing rules in Rust 2024 prevented those tests from compiling.
2025-04-15 20:33:01 +02:00
Samuel Tardieu
b3d401ecb1 Add unsafe markers to accomodate edition 2024 rules
- Add `unsafe` blocks inside `unsafe fn` using unsafe constructs
- Add `unsafe` qualifier to `extern` blocks
- Add `unsafe` qualifier to `no_mangle` attributes
2025-04-15 20:33:01 +02:00
Manish Goregaokar
459897bab1
missing_asserts_for_indexing: consider assert_eq!() as well (#14258)
`assert_eq!()` and `assert_ne!()` are not expanded the same way as
`assert!()` (they use a `match` instead of a `if`). This makes them
being recognized as well.

Fix rust-lang/rust-clippy#14255

changelog: [`missing_asserts_for_indexing`]: consider `assert_eq!()` as
well
2025-04-15 18:11:36 +00:00
Samuel Tardieu
e0c8b4bf53 Replace stray println!() in lint code by bug!()
To avoid crashing Clippy, the `bug!()` is used only when debug assertions are
enabled. In regular usage, the result will be the same as before, but without
the extra line printed on the standard output which has the potential for
disrupting shell scripts.
2025-04-15 19:50:14 +02:00
Victor Farazdagi
e2422a618d new lint: redundant_test_prefix 2025-04-15 14:42:09 +03:00
Alejandra González
d3267e9230
Consider nested lifetimes in mut_from_ref (#14471)
fixes #7749.
This issue proposes searching for `DerefMut` impls, which is not done
here: every lifetime parameter (aka `<'a>`) is the input types is
considered to be potentially mutable, and thus deactivates the lint.

changelog: [`mut_from_ref`]: Fixes false positive, where lifetimes
nested in the type (e.g. `Box<&'a mut T>`) were not considered.
2025-04-14 23:43:42 +00:00
Alejandra González
02764f611a
Various fixes for manual_is_power_of_two (#14463)
Fix #14461:

- insert parentheses as required in suggestion
- check MSRV before suggesting fix in `const` context
- do not lint macro expansion result

Commits have been logically separated to facilitate review, and start
with a refactoring (and simplification) of the existing code.

changelog: [`manual_is_power_of_two`]: insert parentheses as required in
suggestion, check MSRV before suggesting fix in `const` context, do not
lint macro expansion results
2025-04-14 22:56:38 +00:00
Nicholas Nethercote
4784074753 Move name field from AssocItem to AssocKind variants.
To accurately reflect that RPITIT assoc items don't have a name. This
avoids the use of `kw::Empty` to mean "no name", which is error prone.

Helps with #137978.
2025-04-15 08:07:15 +10:00
Nicholas Nethercote
67dc0d083c Move opt_rpitit_info field to hir::AssocKind::Type.
From `hir::AssocItem`.
2025-04-15 08:06:59 +10:00
Samuel Tardieu
c43c87de26 Recognize <uint>::count_ones(x) as x.count_ones() 2025-04-14 22:12:39 +02:00
Samuel Tardieu
4d343d56e1 Check MSRV before suggesting fix in const context 2025-04-14 22:05:44 +02:00
Samuel Tardieu
1cab0b412e Do not lint result from macro expansion
If parts of the expression comes from macro expansion, it may match an
expression equivalent to `is_power_of_two()` by chance only.
2025-04-14 22:05:44 +02:00
Samuel Tardieu
77b3ac3d57 Check for lifetime uses in closures as well
The `BodyLifetimeChecker` which checks for the use of any non-anonymous
non-static lifetime did not recurse into closures, missing lifetime
uses. This would lead to a bogus elision suggestion.

The `BodyLifetimeChecker` is not refined enough to avoid false
positives, as any conforming lifetime, including one coming from the outer
context, would be considered a hit. The number of false positives might
increase now that we check closures as well, in case those closures
define and use lifetimes themselves.
2025-04-14 09:55:38 +02:00
Nicholas Nethercote
d891f77363 Move has_self field to hir::AssocKind::Fn.
`hir::AssocItem` currently has a boolean `fn_has_self_parameter` field,
which is misplaced, because it's only relevant for associated fns, not
for associated consts or types. This commit moves it (and renames it) to
the `AssocKind::Fn` variant, where it belongs.

This requires introducing a new C-style enum, `AssocTag`, which is like
`AssocKind` but without the fields. This is because `AssocKind` values
are passed to various functions like `find_by_ident_and_kind` to
indicate what kind of associated item should be searched for, and having
to specify `has_self` isn't relevant there.

New methods:
- Predicates `AssocItem::is_fn` and `AssocItem::is_method`.
- `AssocItem::as_tag` which converts `AssocItem::kind` to `AssocTag`.

Removed `find_by_name_and_kinds`, which is unused.

`AssocItem::descr` can now distinguish between methods and associated
functions, which slightly improves some error messages.
2025-04-14 16:13:04 +10:00
Nicholas Nethercote
e6dc2e9be0 Use Symbol in LateContext::get_associated_type.
To avoid unnecessary interning.
2025-04-14 16:12:21 +10:00
Alex Macleod
69ade776fa
Fulfill the lint expectation even if allowed at the type level (#14604)
`clippy::len_without_is_empty` can be allowed at the type declaration
site, and this will prevent the lint from triggering even though the
lint is shown on the `len()` method definition.

This allows the lint to be expected even though it is allowed at the
type declaration site.

changelog: [`len_without_is_empty`]: the lint can now be `#[expect]`ed
on the `len()` method even when it is `#[allow]`ed on the definining
type.

Fixes rust-lang/rust-clippy#14597
2025-04-13 20:54:58 +00:00
Samuel Tardieu
327da45aee Fulfill the lint expectation even if allowed at the type level
`clippy::len_without_is_empty` can be allowed at the type declaration
site, and this will prevent the lint from triggering even though the
lint is shown on the `len()` method definition.

This allows the lint to be expected even though it is allowed at the
type declaration site.
2025-04-13 22:04:15 +02:00