Commit graph

12147 commits

Author SHA1 Message Date
lapla-cogito
65b95a2cfb
fix escaping problem in write_literal and print_literal lint 2025-01-13 12:51:41 +09:00
dswij
716a3a4778
Do not intersect spans coming from different contexts (#13942)
The code should not attempt to obtain a snippet by capping the function
signature span with its identifier span without checking that they are
in the same context.

This is the only instance I could identify where placeholders were used
instead of the real snippet when running the CI lintcheck. Moreover, the
placeholders were not even used, as they snippet was obtained
prematurely.

Found in the context of #13941

changelog: none
2025-01-10 12:33:19 +00: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
Samuel Tardieu
d8301d762f Do not intersect spans coming from different contexts
The code should not attempt to obtain a snippet by capping the
function signature span with its identifier span without checking that
they are in the same context.
2025-01-10 10:02:27 +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
Quentin Santos
e42e354f65 Remove needless check of returned type
We are checking that we are calling the `as_bytes()` method of `String`
or `str`. Checking that it returns a `slice()` does not add anything.
2025-01-09 18:40:33 +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
A_A
8461d3febd Remove unnecessary string allocation 2025-01-08 07:52:09 +01:00
Matthias Krüger
11f38ade90 Rollup merge of #134989 - max-niederman:guard-patterns-hir, r=oli-obk
Lower Guard Patterns to HIR.

Implements lowering of [guard patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see the [tracking issue](#129967)) to HIR.
2025-01-07 21:39:40 +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
hrxi
63487dde53 clippy::redundant_locals is not a correctness lint
Even its documentation says so. According to the documentation, it might
either be a "suspicious" or a "perf" lint.
2025-01-06 22:42:11 +01: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
891e38788a Don't emit machine applicable map_flatten lint if there are code comments 2025-01-06 17:35:55 +01:00
Catherine Flores
a9c0e22dfa
Changelog for Clippy 1.84 🧨 (#13937)
Roses are red,
Violets are blue,
Happy new year,
full of happiness and cheer!

---

### The cat of this release is lizzy nominated by @jdonszelmann:

<img height=700
src="https://github.com/user-attachments/assets/612834d0-d584-4bf3-b11e-3564456c10ee"
alt="The cats of this Clippy release" />

Cats for the next release can be nominated in the comments :D

---

I've also updated the template to include the text I usually use for
moves, renames, or new lints.

changelog: none

---

That's it happy new year, kings, queens, and all other royalty! ❤️
2025-01-06 01:00:22 +00:00
Alexey Semenyuk
ca55534c92 Do not trigger clippy::missing_const_for_fn triggering for tests 2025-01-06 02:07:16 +05: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
Ralf Jung
f416f266b0 turn hir::ItemKind::Fn into a named-field variant 2025-01-04 11:35:31 +01:00
Guillaume Gomez
1b85ae3a60 Only emit useless_vec suggestion if the macro does not contain code comments 2025-01-03 22:55:25 +01:00
xFrednet
4736004c2c
Update version attribute for 1.84 clippy lints 2025-01-03 19:39:32 +01:00
Samuel Tardieu
9c46e1173f Remove unneeded parentheses in unnecessary_map_or lint output
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.
2025-01-03 00:32:46 +01:00
Manish Goregaokar
631d9a2c5c
Add lint for calling Iterator::last() on DoubleEndedIterator (#13922)
I [recently realized that `.last()` might not call `next_back()` when it
is
available](https://qsantos.fr/2025/01/01/rust-gotcha-last-on-doubleendediterator/).
Although the implementor could make sure to implement `last()` to do so,
this is not what will happen by default. As a result, I think it is
useful to add a lint to promote using `.next_back()` over `.last()` on
`DoubleEndedIterator`.

If this is merged, we might want to close #1822.

changelog: [`double_ended_iterator_last`]: Add lint for calling
`Iterator::last()` on `DoubleEndedIterator`
2025-01-02 18:30:38 +00:00
yanglsh
f18399fb69 Emit redundant if when duplicated in needless_continue 2025-01-02 07:45:16 -07:00
Quentin Santos
d67c00f723 Dogfood double_ended_iterator_last 2025-01-01 22:19:16 +01:00
Quentin Santos
0d213aa09a Revert "Dogfood double_ended_iterator_last"
This reverts commit 09c5d34f98.
2025-01-01 22:16:33 +01:00
Quentin Santos
7331cc0f81 Only complain about default Iterator::last() 2025-01-01 22:16:07 +01:00
Quentin Santos
27acfd8a5b Prefer if chain to let-else 2025-01-01 20:33:58 +01:00
Alejandra González
034f3d224c
Improve slow_vector_initialization suggestion (#13912)
close #13781

The `slow_vector_initialization` lint currently only suggests using the
`vec!` macro with size, but it does not suggest removing the `resize`
method call.

changelog: [`slow_vector_initialization`]: improve
`slow_vector_initialization` suggestion
2025-01-01 19:21:01 +00:00
Quentin Santos
09c5d34f98 Dogfood double_ended_iterator_last 2025-01-01 19:47:22 +01:00
Quentin Santos
707653f268 Add lint for calling last() on DoubleEndedIterator 2025-01-01 18:47:23 +01:00
Alejandra González
33a6590ce1
Check if deref target implements is_empty for len_zero lint (#13871)
In this case, the lint can be triggered as well as `is_empty()` will be
found on the target type.
One such case was found in Clippy sources (first commit)

changelog: [`len_zero`]: trigger if deref target implements `is_empty()`

Close #13861
2025-01-01 15:09:26 +00:00
Samuel Tardieu
4c9c2cca2b Check if deref target implements is_empty for len_zero lint
In this case, the lint can be triggered as well as `is_empty()` will be
found on the target type.
2025-01-01 14:14:39 +01:00
Max Niederman
54e5116b44 cover guard patterns in clippy lints 2024-12-31 17:59:34 -08:00
A_A
3bc089e9fe refactor manual_is_ascii_check to remove unused parameter 2024-12-31 14:18:43 +01:00
A_A
2a4dadd985 fix type suggestion for manual_is_ascii_check (#13913) 2024-12-31 13:48:08 +01:00
lapla-cogito
4899333886
better suggestion for slow_vector_initialization 2024-12-31 16:07:06 +09:00
Manish Goregaokar
7a834b5d3a
Use the full lifetime name in suggestions (#13907)
Using `lifetime.ident.name` in suggestions will not output the raw
modifier. For example, `'r#struct` will be rendered as `'struct` which
would be incorrect.

Fix #13899

changelog: [`needless_arbitrary_self_type`]: use the raw lifetime name
in suggestions
2024-12-30 17:57:59 +00:00
Timo
2aea7a080d
Fix parentheses when replacing matches!(…, None) with .is_none() (#13906)
Proper parentheses need to be added to some expressions in receiver
position.

Fix #13902

changelog: [`redundant_pattern_matching`]: use proper parentheses when
suggesting replacing `matches!(…, None)` by `.is_none()`
2024-12-30 14:06:30 +00:00
Guillaume Gomez
b515064fd2 Correctly handle char paths 2024-12-30 12:28:47 +01:00
Samuel Tardieu
a657fcc89a Use the full lifetime name in suggestions
Using `lifetime.ident.name` in suggestions will not output the raw
modifier. For example, `'r#struct` will be rendered as `'struct` which
would be incorrect.
2024-12-30 00:07:18 +01:00
Samuel Tardieu
e4b11a7266 Fix parentheses when replacing matches!(…, None) with .is_none() 2024-12-29 23:25:59 +01:00
Samuel Tardieu
a9fe04335a Do not remove identity mapping if mandatory mutability would be lost
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.
2024-12-29 23:01:14 +01:00
Guillaume Gomez
34b1765e65 Move more def paths into clippy_utils::paths 2024-12-29 20:39:43 +01:00
Alejandra González
b57d98b00e
Do not remove required parentheses in borrow_as_ptr suggestion (#13884)
Also, simplify boolean shortcut expression, and ensure that
applicability is properly applied, as it was ignored and
`MachineApplicable` was always used.

changelog: [`borrow_as_ptr`]: do not remove required parentheses in
autofix suggestion

Close #13882
2024-12-29 16:29:53 +00:00
Fridtjof Stoldt
f5f1abd8fb
borrow_interior_mutable_const ICE into FN (#13877)
This PR just makes sure that we exit the function before getting an ICE
and adds a regression test. Related to #12979. We would need to keep
researching the issue, but as the ICEing code isn't that complicated,
getting a hotfix into nightly is urgent.

changelog:[`borrow_interior_mutable_const`] Fix ICE #12979
2024-12-29 15:52:35 +00:00
Samuel Tardieu
eef47fc0b7 Do not remove required parentheses in borrow_as_ptr suggestion
Also, simplify boolean expression, and ensure that proper applicability
is used.
2024-12-29 16:50:05 +01:00