Commit graph

1969 commits

Author SHA1 Message Date
Timo
14dfc03597
feat: introduce path_to_local_with_projections (#15396)
As suggested in
https://github.com/rust-lang/rust-clippy/pull/15268#discussion_r2249249661

WIP because:
- [x] what should be done with the now-error-pattern-less
`tests/ui/double_ended_iterator_last_unfixable.rs`?
- [x] is the change in behaviour of `double_ended_iterator_last` okay in
general?
- cc @samueltardieu because this changes the code you added in
https://github.com/rust-lang/rust-clippy/pull/14140

changelog: none

r? @y21
2025-08-10 10:33:05 +00:00
Ada Alakbarova
04606e27dc
introduce path_to_local_with_projections
combine two similar arms

use in `eager_transmute`

use in `double_ended_iterator_last`

use different numbers in the new test case to avoid possible confusion

move the other "unfixable" case as well; it shouldn't lint anyway, so
having it in the main test file is fine
2025-08-09 20:27:30 +02:00
Ada Alakbarova
d5f2b66bd8
expect_fun_call: move helper functions out of the main one
allows removing the `#[allow(clippy::too_many_lines)]`
2025-08-09 11:50:00 +02:00
Philipp Krones
f2b7e9ff52
Merge remote-tracking branch 'upstream/master' into rustup 2025-08-07 16:47:42 +02:00
Huterenok
6249f33ca1 fix: unnecessary_sort_by lint method consistency in message and suggestion 2025-08-05 15:23:10 +03:00
Timo
e8185ec091
Extend implicit_clone to handle to_string calls (#14177)
Put another way, merge `string_to_string` into `implicit_clone`, as
suggested here:
https://github.com/rust-lang/rust-clippy/issues/14173#issuecomment-2645846915

Note: [I
wrote](b8913894a1)
this comment:
6cdb7f68c3/clippy_lints/src/methods/implicit_clone.rs (L43-L45)

Here is the context for why I wrote it:
https://github.com/rust-lang/rust-clippy/pull/7978#discussion_r769128853

Regardless, it's probably time for the comment to go away. Extending
`implicit_clone` to handle `to_string` calls yields many hits within
Clippy's codebase.

changelog: extend `implicit_clone` to handle `to_string` calls
2025-08-02 11:41:00 +00:00
Alejandra González
445d41909e
Fix iter_on_single_items FP on function pointers and let stmts (#15013)
Closes rust-lang/rust-clippy#14981

changelog: [`iter_on_single_items`] fix FP on function pointers and let
stmts
2025-07-30 16:42:12 +00:00
Cameron Steffen
86ff11e729 Rename impl_of_method -> impl_of_assoc 2025-07-28 09:54:53 -05:00
Cameron Steffen
b0740198ae Rename trait_of_item -> trait_of_assoc 2025-07-28 09:53:50 -05:00
Timo
3174c05949
fix: unnecessary_map_or don't add parens if the parent expr comes f… (#15345)
…rom a macro

changelog: [`unnecessary_map_or`]: don't add parens if the parent expr
comes from a macro

fixes rust-lang/rust-clippy#14714
2025-07-27 12:15:17 +00:00
Ada Alakbarova
81fc22753e
fix: unnecessary_map_or don't add parens if the parent expr comes from a macro 2025-07-27 13:54:47 +02:00
llogiq
f9ab9d0217
fix ip_constant when call wrapped in extra parens (#15339)
changelog: [`ip_constant`]: fix suggestion when call is inside
parentheses like `(Ipv4Addr::new(127, 0, 0, 1))`

Very similar to the fix in
https://github.com/rust-lang/rust-clippy/pull/15191
2025-07-26 15:23:47 +00:00
Philipp Krones
5c7418b38a Merge commit '1db89a1b1c' into clippy-subtree-update 2025-07-25 15:54:22 +02:00
Philipp Krones
c98e60a56e
Merge remote-tracking branch 'upstream/master' into rustup 2025-07-25 15:04:01 +02:00
Dan Johnson
0aa92fdba6 fix ip_constant when call wrapped in extra parens 2025-07-24 14:55:58 -07:00
Alex Macleod
fd7076bd00
Fix filter_map_bool_then wrongly suggests macro definitions (#15048)
Closes rust-lang/rust-clippy#15047

changelog: [`filter_map_bool_then`] fix wrongly showed macro definitions
in suggestions
2025-07-19 12:34:23 +00:00
Nicholas Nethercote
b7caf7593e Improve path segment joining.
There are many places that join path segments with `::` to produce a
string. A lot of these use `join("::")`. Many in rustdoc use
`join_with_double_colon`, and a few use `.joined("..")`. One in Clippy
uses `itertools::join`. A couple of them look for `kw::PathRoot` in the
first segment, which can be important.

This commit introduces `rustc_ast::join_path_{syms,ident}` to do the
joining for everyone. `rustc_ast` is as good a location for these as
any, being the earliest-running of the several crates with a `Path`
type. Two functions are needed because `Ident` printing is more complex
than simple `Symbol` printing.

The commit also removes `join_with_double_colon`, and
`estimate_item_path_byte_length` with it.

There are still a handful of places that join strings with "::" that are
unchanged. They are not that important: some of them are in tests, and
some of them first split a path around "::" and then rejoin with "::".

This fixes one test case where `{{root}}` shows up in an error message.
2025-07-17 08:37:19 +10:00
Jason Newcomb
e113e66606
Propose to exchange ranges only when it is safe to do so (#14432)
To avoid false positives, the `range_plus_one` and `range_minus_one`
lints will restrict themselves to situations where the iterator types
can be easily switched from exclusive to inclusive or vice-versa. This
includes situations where the range is used as an iterator, or is used
for indexing.

On the other hand, assignments of the range to variables, including
automatically typed ones or wildcards, will no longer trigger the lint.
However, the cases where such an assignment would benefit from the lint
are probably rare.

In a second commit, the `range_plus_one` and `range_minus_one` logic are
unified, in order to properly emit parentheses around the suggestion
when needed.

Fix rust-lang/rust-clippy#3307
Fix rust-lang/rust-clippy#9908

changelog: [`range_plus_one`, `range_minus_one`]: restrict lint to cases
where it is safe to switch the range type

*Edit:* as a consequence, this led to the removal of three
`#[expect(clippy::range_plus_one)]` in the Clippy sources to avoid those
false positives.
2025-07-16 16:21:45 +00:00
yanglsh
038295adf8 fix: filter_map_bool_then wrongly showed macro definition in suggestions 2025-07-15 23:07:39 +08:00
Samuel Tardieu
ba8da7a8e8
useless_conversion: move all the impl to the same lint pass (#15274)
Needed to split the lints crate.

changelog: none
2025-07-14 22:33:14 +00:00
Jason Newcomb
7796773d3e useless_conversion: move all the impl to the same lint pass 2025-07-14 12:05:07 -04:00
Samuel Tardieu
8d6de0b82e
Fix expect_fun_call producing invalid suggestions (#15122)
Previously expect_fun_call would too eagerly convert cases like
`foo.expect(if | block | match)` into `foo.unwrap_or_else`.
Additionally, it would also add to_string() even though the argument is
being passed into format!() which can accept a &str. I also discovered
some other cases where this lint would either produce invalid results,
or be triggered unnecessarily:

- Clippy would suggest changing expect to unwrap_or_else even if the
expression inside expect contains a return statement
- opt.expect(const_fn()) no longer triggers the lint
- The lint would always add braces to the closure body, even if the body
of expect is a single expression
- opt.expect({"literal"}) used to get turned into
```opt.unwrap_or_else(|| panic!("{}", {"literal"}.to_string()))```

Fixes rust-lang/rust-clippy#15056

changelog: [`expect_fun_call`]: fix expect_fun_call producing invalid
suggestions
2025-07-14 15:05:59 +00:00
Kamal Ahmad
fcd064da75 expect_fun_call: only lint const fn's if they're not inside a const context 2025-07-14 18:57:15 +05:00
Jason Newcomb
d9acd919ad
fix false negative of expect_used (#15253)
Fixes: rust-lang/rust-clippy#15247

changelog: Fix the false negative of [`expect_used`] when metting
`Option::ok().expect`.
2025-07-14 07:52:48 +00:00
Krishna Ketan Rai
b4fc33ae8b Lint: Improve manual_is_variant_and to support equality comparison
- Add equality pattern support to manual_is_variant_and
- Update documentation and Clippy manual
2025-07-13 19:51:45 +05:30
relaxcn
1d1b97d514 fix false negative of expect_used 2025-07-12 01:44:55 +08:00
llogiq
df5a0ee919
or_fun_call: lint method calls inside map_or first arg (#15074)
<strike>blocked on rust-lang/rust-clippy#15073</strike>

Lint method calls inside `map_or` too, so for this, lint will be showed:
```rust
Some(4).map_or("asd".to_string().len() as i32, f);
```
previously it worked only for:
```rust
Some(4).map_or(slow_fun(), f);
```

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=bfcda42a6af446e69bc883a8b45eb13c

Sorry for multiple `or_fun_call` PRs.

changelog: [`or_fun_call`]: lint method calls inside map_or first arg
2025-07-10 21:27:09 +00:00
Philipp Krones
4e614bf683 Merge commit 'cdbbf3afda' into clippy-subtree-update 2025-07-10 20:25:36 +02:00
Philipp Krones
9e0f749e08
Merge remote-tracking branch 'upstream/master' into rustup 2025-07-10 20:01:08 +02:00
Timo
012e5f538b
Fix manual_is_variant_and condition generation (#15206)
When comparing `x.map(func) == Some(bool_lit)`, the value of `bool_lit`
was ignored, despite the fact that its value should determine the value
of the proposed expression.

`func` can be either a closure or a path. For the latter, η-expansion
will be used if needed to invert the result of the function call.

changelog: [`manual_is_variant_and`]: fix inverted suggestions that
could lead to code with different semantics

Fixes rust-lang/rust-clippy#15202

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_SUMMARY_START -->

### Summary Notes

-
[Beta-nomination](https://github.com/rust-lang/rust-clippy/pull/15206#issuecomment-3034006613)
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-07-09 21:04:18 +00:00
Jason Newcomb
a24fb386b1
Do not remove method call if type is adjusted (#15181)
Propose to replace `x.map_or(false, |y| y == z)` by `x == Some(z)` only
if `x` is not adjusted. Otherwise, the type of `x` in the comparaison
may not be the expected one, as it may be the product of an auto-deref.

changelog: [`unnecessary_map_or`]: do not propose to replace the
`map_or` call by a comparaison if types wouldn't be correct

Fixes rust-lang/rust-clippy#15180
2025-07-09 15:04:39 +00:00
Samuel Tardieu
d8ba94b0ae
Fix manual_is_variant_and condition generation
When comparing `x.map(func) == Some(bool_lit)`, the value of `bool_lit` was
ignored, despite the fact that its value should determine the value of
the proposed expression.

`func` can be either a closure or a path. For the latter, η-expansion
will be used if needed to invert the result of the function call.
2025-07-06 23:36:45 +02:00
klensy
addf309a5a also lint method calls inside map_or first arg 2025-07-06 18:45:29 +03:00
llogiq
c943f4c0e9
or_fun_call: also lint and method for Option/Result (#15073)
<strike>build on top of rust-lang/rust-clippy#15071</strike>

This also adds ability to lint Option/Result::and method. Yes, this is
not `or` method, but uses the same eager/lazy linting logic. Should i
update lint description to list all checked structs/methods?

changelog: [`or_fun_call`]: lint Option/Result::and
2025-07-06 15:31:13 +00:00
llogiq
cc7b2f57e0
Properly check that an expression might be the one returned (#15115)
The `TyCtxt::hir_get_fn_id_for_return_block()` function was too broad,
as it will return positively even when given part of an expression that
can be used as a return value. A new
`potential_return_of_enclosing_body()` utility function has been made to
represent the fact that an expression might be directly returned from
its enclosing body.

changelog: [`return_and_then`]: prevent false positives in case of a
partially used expression

Fixes rust-lang/rust-clippy#15111
Fixes rust-lang/rust-clippy#14927

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_SUMMARY_START -->

### Summary Notes

-
[Beta-nomination](https://github.com/rust-lang/rust-clippy/pull/15115#issuecomment-2996222332)
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-07-06 15:24:46 +00:00
Samuel Tardieu
c69905a995
Do not remove method call if type is adjusted
Propose to replace `x.map_or(false, |y| y == z)` by `x == Some(z)` only
if `x` is not adjusted. Otherwise, the type of `x` in the comparaison
may not be the expected one, as it may be the product of an auto-deref.
2025-06-30 19:51:13 +02:00
Nicholas Nethercote
6709720e4c Introduce ByteSymbol.
It's like `Symbol` but for byte strings. The interner is now used for
both `Symbol` and `ByteSymbol`. E.g. if you intern `"dog"` and `b"dog"`
you'll get a `Symbol` and a `ByteSymbol` with the same index and the
characters will only be stored once.

The motivation for this is to eliminate the `Arc`s in `ast::LitKind`, to
make `ast::LitKind` impl `Copy`, and to avoid the need to arena-allocate
`ast::LitKind` in HIR. The latter change reduces peak memory by a
non-trivial amount on literal-heavy benchmarks such as `deep-vector` and
`tuple-stress`.

`Encoder`, `Decoder`, `SpanEncoder`, and `SpanDecoder` all get some
changes so that they can handle normal strings and byte strings.

This change does slow down compilation of programs that use
`include_bytes!` on large files, because the contents of those files are
now interned (hashed). This makes `include_bytes!` more similar to
`include_str!`, though `include_bytes!` contents still aren't escaped,
and hashing is still much cheaper than escaping.
2025-06-30 20:42:27 +10:00
Samuel Tardieu
ad7de948a8
Consider deref'ed argument as non-temporary
If there are more than one dereference (there is one corresponding
matched with a borrow in any case), consider that the argument might
point to a place expression, which is the safest choice.

Also, use an appropriate number of dereferences in suggestions involving
arguments using themselves multiple dereferences.
2025-06-29 16:36:50 +02:00
Philipp Krones
074ccaf3c0 Merge commit 'c5dbd1de07' into clippy-subtree-update 2025-06-27 12:20:57 +02:00
Philipp Krones
90364dd178
Merge remote-tracking branch 'upstream/master' into rustup 2025-06-26 19:29:33 +02:00
Kamal Ahmad
07e2d96eff Fix expect_fun_call producing invalid suggestions 2025-06-24 17:16:13 +05:00
Samuel Tardieu
89f7a4301d
Properly check that an expression might be the one returned
The `TyCtxt::hir_get_fn_id_for_return_block()` function was too broad,
as it will return positively even when given part of an expression that can
be used as a return value. A new `potential_return_of_enclosing_body()`
utility function has been made to represent the fact that an expression
might be directly returned from its enclosing body.
2025-06-23 18:30:27 +02:00
klensy
a82c142f3e or_fun_call: lint Option/Result::and(_then) too 2025-06-21 12:42:41 +03:00
klensy
ea72620f62 lint Option::get_or_insert and Result::map_or too 2025-06-20 23:59:36 +03:00
Samuel Tardieu
65402ab41a clippy: replace path uses by diagnostic items 2025-06-20 17:53:09 +02:00
Samuel Tardieu
d66e4f6a66
Restrict the cases where Clippy proposes to switch range types
To limit false positives, the `range_plus_one` and `range_minus_one` lints
will restrict themselves to situations where the iterator types can be easily
switched from exclusive to inclusive or vice-versa. This includes situations
where the range is used as an iterator, or is used for indexing. Also,
when the range is used as a function or method call argument and the
parameter type is generic over traits implemented by both kind of
ranges, the lint will trigger.

On the other hand, assignments of the range to variables, including
automatically typed ones or wildcards, will no longer trigger the lint.
However, the cases where such an assignment would benefit from the lint
are probably rare.

This fix doesn't prevent false positives from happening. A more complete
fix would check if the obligations can be satisfied by the new proposed
range type.
2025-06-18 06:41:40 +02:00
David Wood
c7ee095bd6 clippy: add MetaSized conditions
Existing lints that had special-casing for `Sized` predicates ought
to have these same special cases applied to `MetaSized` predicates.
2025-06-16 23:04:37 +00:00
Philipp Krones
cbd683fe31 Merge commit '4ef75291b5' into clippy-subtree-update 2025-06-13 11:24:45 +02:00
nekename
982abc48b3
docs: make unbuffered_bytes docs more consistent 2025-06-08 18:37:39 +01:00
yanglsh
20670f78fd fix: iter_on_single_item FP on let stmt with type annotation 2025-06-08 22:46:40 +08:00