Commit graph

22348 commits

Author SHA1 Message Date
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
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
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
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
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
Samuel Tardieu
b90c80a766
Deprecate match_on_vec_items lint (#14217)
This lint does more harm than good: in its description, it proposes to
rewrite `match` on `Vec<_>` indexes or slices by a version which cannot
panic but masks the failure by choosing the default variant.

The `clippy::indexing_slicing` restriction lint covers those cases more
safely, by suggesting to use a non-panicking version to retrieve the
value from the container, without suggesting to fallback to the default
success variant in case of failure.

This PR is an (opposite) alternative to #14208 (which will add a
suggestion to the lint matching the lint description). Discussion on
both PRs can be found [on
Zulip](https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/Suggestions.20that.20suppress.20panics).

changelog: [`match_on_vec_items`]: deprecate lint
2025-04-13 15:52:46 +00:00
Alex Macleod
dbc07558ef
Change Test for Issue 14422 (Cognitive Complexity lowered through mul… (#14603)
changelog: [cognitive_complexity]: Changed Test for this issue from
making sure its not a false positive to making sure its not a false
negative which was the original issue at hand. The test as it was would
also not fail without the change introduced. This test will show if any
regression is made in further pushes.

adds tests to https://github.com/rust-lang/rust-clippy/issues/14422
2025-04-13 13:54:25 +00:00
cerdelen
aa2f48b09d Change Test for Issue 14422 (Cognitive Complexity lowered through multiple returns) to test it does'nt regress to false negative 2025-04-13 15:35:18 +02:00
Jason Newcomb
d19c651c62
Remove some clippy::all uses from UI tests (#14600)
It's either unneeded (`warn`/`deny`) or can be replaced by individual
lints (`allow`). Also removes some redundant `allow`s covered by the
default `-Aunused` that I saw along the way

changelog:  none
2025-04-12 22:23:04 +00:00
Timo
b157411dba
Consecutive returns dont decrease cognitive Complexity level anymore (#14460)
changelog: [`cognitive_complexity`]: Consecutive return calls decreased
complexity level of the function by 1.

fixes rust-lang/rust-clippy#14422
2025-04-12 16:53:50 +00:00
Timo
8dfcaa0590
Do not propose to auto-derive Clone in presence of unsafe fields (#14559)
`unsafe_fields` is an incomplete feature; comments have been put near
`#![expect(incomplete_features)]` to ensure that we revisit the
situation when the feature becomes complete.

changelog: [`expl_impl_clone_on_copy`]: do not lint in the presence of
`unsafe` fields

Fixes #14558
2025-04-12 16:50:12 +00:00
Timo
3f6025a11d
book: indicate that at least one error marker is required (#14589)
This is a recent change, and no error marker is inserted by default in
the file generated by `cargo dev new_lint`.

changelog: none

r? @y21
2025-04-12 15:05:55 +00:00
Timo
3dd1a9b56e
dogfood: run with -D clippy::dbg_macro (#14579)
This prevents forgotten `dbg!()` calls from entering Clippy codebase by
mistake.

Suggested by @y21 when one of my PR forgot to remove one `dbg!()` call.

changelog: none
2025-04-12 14:41:40 +00:00
Alex Macleod
4b140cb48f Remove some clippy::all uses from UI tests 2025-04-12 13:40:49 +00:00
Samuel Tardieu
ec105bab2f
fix: redundant_clone FP in overlapping lifetime (#14237)
fixes #13900

changelog: [`redundant_clone`]: fix FP in overlapping lifetime
2025-04-11 15:47:20 +00:00
yanglsh
07d8a9d331 fix: redundant_clone FP in overlapping lifetime 2025-04-11 23:42:26 +08:00
Alex Macleod
ac4c69f423
implicit_return: better handling of asynchronous code (#14446)
Blocks created by desugaring will not contain an explicit `return`. Do
not suggest to add it when the user has no control over the desugared
code.

Also, ensure that in a `xxx.await` expression, the suggested `return` is
emitted before the whole expression, not before the `await` keyword.

Fix #14411

changelog: [`implicit_return`]: fix proposed `return` position in the
presence of asynchronous code
2025-04-11 13:39:03 +00:00
Alex Macleod
fad1bfc568
Reinstate checking of tokio-rustls in lintcheck (#14591)
`cmake` has been pinned to version 3.31.6 in Ubuntu runner images.
Reference: https://github.com/actions/runner-images/pull/11933

changelog: none
2025-04-11 11:34:20 +00:00
Samuel Tardieu
dd21b6ecea book: indicate that at least one error marker is required
This is a recent change, and no error marker is inserted by default in
the file generated by `cargo dev new_lint`.
2025-04-11 13:11:25 +02:00
Philipp Krones
7e60c1fafb
Assign PR touching triagebot.toml to Philipp (#14590)
`triagebot.toml` is related to the repo behavior wrt interactions, and
must be kept inline with what is done in the CI.

changelog: none

r? @flip1995
2025-04-11 08:54:23 +00:00
Philipp Krones
8418338d26
Enable no mentions in commits of triagebot (#14576)
This PR enables the no-mentions in commits handler in triagebot.

Documentation pending at
https://github.com/rust-lang/rust-forge/pull/827

Original motivation in https://github.com/rust-lang/rust/issues/137990.

changelog: add `[no-mentions]` in `triagebot.toml`
2025-04-11 08:52:56 +00:00
Samuel Tardieu
c67df0530a
arbitrary_source_item_ordering should ignore test modules (#14585)
Close rust-lang/rust-clippy#14570

The test is provided
[here](https://github.com/rust-lang/rust-clippy/blob/master/tests/ui-toml/arbitrary_source_item_ordering/ordering_good_var_1.rs#L171)
but it's passed without `//@compile-flags: --test`

changelog: [`arbitrary_source_item_ordering`]: should ignore test
modules
2025-04-11 08:48:20 +00:00
Alexey Semenyuk
e1bd4e4b6c arbitrary_source_item_ordering should ignore test modules 2025-04-11 13:42:21 +05:00
Samuel Tardieu
2c021ca19a Reinstate checking of tokio-rustls in lintcheck
`cmake` has been pinned to version 3.31.6 in Ubuntu runner images.
Reference: https://github.com/actions/runner-images/pull/11933
2025-04-11 10:41:36 +02:00
Samuel Tardieu
fa5e855397 Assign PR touching triagebot.toml to Philipp
`triagebot.toml` is related to the repo behavior wrt interactions, and
must be kept inline with what is done in the CI.
2025-04-11 10:20:50 +02:00
cerdelen
c7fbcf4c33 Test for Multiple return statements doesnt decrease cognitive complexity 2025-04-11 09:30:28 +02:00
Samuel Tardieu
0cd5b6261a
Avoid some uses of empty identifiers (#14580)
This contributes towards
https://github.com/rust-lang/rust/issues/137978.

changelog: none
2025-04-11 06:59:38 +00:00
Samuel Tardieu
0286d46952
Fix a help message of empty_line_after_doc_comments for cases where the following item is nameless (#14584)
Fixes rust-lang/rust-clippy#14515.

changelog: [`empty_line_after_doc_comments`]: Fix a help message for
cases where the following item is nameless.
2025-04-10 18:12:45 +00:00
León Orell Valerian Liehr
15fd2ab73e
Fix a help message of empty_line_after_doc_comments for cases where the following item is nameless 2025-04-10 18:24:45 +02:00
Nicholas Nethercote
3690f3b37f Avoid an Ident::empty usage.
By moving a couple of identifier checks earlier, we no longer need to
use an empty identifier for the `impl` case.

changelog: none
2025-04-10 16:49:18 +10:00
Nicholas Nethercote
822b931652 Avoid a kw::Empty usage.
`Option<Symbol>` is a much nicer and idiomatic way of representing "no
name" using empty symbols. And it works naturally for the item ordering
checking because `None < Some(_)` just like the empty string compares
less than all non-empty strings.

changelog: none
2025-04-10 16:46:46 +10:00
Samuel Tardieu
529bb5f253
Correctly handle bracketed type in default_constructed_unit_struct (#14367)
There were two bugs here. Let's assume `T` is a singleton type
implementing `Default` and that `f()` takes a `T`:

- `f(<T>::default())` cannot be replaced by `f(<T)` as it was (incorrect
spans – this is tricky because the type relative path uses a base span
covering only `T`, not `<T>`) (third commit)
- The argument of `f(<_>::default())` is inferred correctly, but cannot
be replaced by `<_>` or `_`, as this cannot be used to infer an instance
of a singleton type (first commit).

The second commit offers better error messages by pointing at the whole
expression.

Fix #12654

changelog: [`default_constructed_unit_struct`]: do not suggest incorrect
fix when using a type surrounded by brackets
2025-04-10 05:51:39 +00:00
Samuel Tardieu
0aa0d074cd Remove brackets around type name when it is no longer a prefix
When `<T>::default()` is replaced by `T` when `T` is a singleton,
the brackets around the type name can be removed.
2025-04-10 07:46:45 +02:00
Samuel Tardieu
85bf0b2f78 dogfood: run with -D clippy::dbg_macro
This prevents `dbg!()` calls from entering Clippy codebase by mistake.
2025-04-10 07:38:16 +02:00
Urgau
723f5b1761
Enable no mentions in commits of triagebot 2025-04-09 20:18:36 +02:00
Philipp Krones
e0e2a93cf2
Enable GitHub review synchronization with S-waiting-on-* labels (#14567)
changelog: none

r? @flip1995
2025-04-09 15:31:57 +00:00