Commit graph

21788 commits

Author SHA1 Message Date
Guillaume Gomez
f666fd6417 Update UI tests 2025-02-15 13:38:16 +01:00
Guillaume Gomez
95c6866dd4 Make UI tests annotations mandatory 2025-02-15 13:37:00 +01:00
Timo
379c8f4bfb
Clarify example for macro_use (#14220)
Clarify example for macro_use

changelog: none
2025-02-15 10:22:11 +00:00
alexey semenyuk
133882920b
Add example for macro_use 2025-02-15 14:47:18 +05:00
Jason Newcomb
0dd5c4ddeb
Fix used_underscore_items lint uses of foreign functions (#14205)
Fixed #14156

changelog: none
2025-02-15 02:46:49 +00:00
Jason Newcomb
4e899e16f9
unnecessary_map_or: do not consume the comparison value if it does not implement Copy (#14207)
Fix #14201

changelog: [`unnecessary_map_or`]: do not consume the comparison value
if it does not implement `Copy`
2025-02-15 02:45:59 +00:00
Philipp Krones
823b81869a
Make rust_tools_util package compatible with current stable by lowering rust edition to 2021. (#14211)
Between the release of v0.4.0 and v0.4.1 the edition was pushed to 2024
by
a4805ff610

changelog: Use edition=2021 in `rustc_tools_util`
2025-02-14 14:49:00 +00:00
Sebastian Jeltsch
f7f7888041
Make rust_tools_util package compatible with current stable by lowering rust edition to 2021. 2025-02-14 15:44:08 +01:00
Catherine Flores
50ecb6e846
doc_link_code: add check for links with code spans that render weird (#14121)
This is the lint described at
https://github.com/rust-lang/rust/pull/136308#issuecomment-2625485331
that recommends using HTML to nest links inside code.

changelog: [`doc_link_code`]: warn when a link with code and a code span
are back-to-back
2025-02-14 13:55:33 +00:00
Timo
b83762c578
Fix literal_string_with_formatting_args lint emitted when it should not (#13953)
Fixes #13885.
Fixes https://github.com/rust-lang/rust-clippy/issues/14007.

Problem was that I forgot to check whether or not the `span` was a
"real" one. Because if not, then it starts pointing to pretty much only
wrong content, hence the problems we saw with clippy linting on
`clippy.toml`.

changelog: Fix `literal_string_with_formatting_args` lint emitted when
it should not

r? @samueltardieu
2025-02-13 16:18:44 +00:00
Samuel Tardieu
f826193aec unnecessary_map_or: do not consume the non-Copy comparison value 2025-02-13 12:00:38 +01:00
Philipp Krones
943d604e59
Fix rustc_tools_util's version.host_compiler release channel, expose the rustc version, and add tests (#14123)
changelog: Fix rustc_tools_util's `version.host_compiler` release
channel, expose the rustc version, and add tests.

Previously the host_compiler would be set to "nighly" on the stable
channel. Generally, the field felt a bit neglected neither being printed
not tested.
2025-02-13 10:15:24 +00:00
Sebastian Jeltsch
8cac5b03f3
rustc_tools_util: Change release channel default to stable
Fixes the exposed release channel

Bump version to 0.4.1
2025-02-13 11:10:45 +01:00
Philipp Krones
8ae4750155
fix broken link of UI test in the book (#14077)
r? flip1995

changelog: none
2025-02-13 09:47:06 +00:00
llogiq
4129f5c824
New lint: mem_replace_option_with_some (#14197)
`mem::replace(opt, Some(v))` can be replaced by `opt.replace(v)`.

Close #14195

changelog: [`mem_replace_option_with_some`]: new lint
2025-02-12 19:02:06 +00:00
Samuel Tardieu
342ac8ee28 New lint: mem_replace_option_with_some
`mem::replace(opt, Some(v))` can be replaced by `opt.replace(v)`.
2025-02-12 19:57:14 +01:00
WeiTheShinobi
b167895a19
Fix used_underscore_items lint uses of foreign functions 2025-02-13 01:06:32 +08:00
Samuel Tardieu
39bde6d46a Reorganize code in mem_replace.rs
Make each lint be more self-contained, and trigger only the next lint
when the previous one didn't already rewrite the expression.
2025-02-12 17:16:37 +01:00
Jason Newcomb
1cb4236a95
New lint: unbuffered_bytes (#14089)
Checks for `Read::bytes()` on an unbuffered Read type.
The default implementation calls `read` for each byte, which can be very
inefficient for data that’s not in memory, such as `File`.

Considerations which I'd like to have feedback on:
* Currently this lint triggers when `.bytes()` is called on any type
that implements `std::io::Read` but not `std::io::BufRead`. This is
quite aggressive and in and may result in false positives. Alternatives:
* Only trigger on concrete types, not generic types. This does mean that
cases where a function is generic over a `R: Read` and calls `.bytes()`
are not caught by the lint, which could be quite a nasty case of this
bug.
  * Only trigger on an allowlist of stdlib types
* Compromise: Is it possible to make this lint `pedantic` on types that
are not on a allowlist?
* Theoretically, a trait implementation of `Read` could override
`.bytes()` with an efficient implementation. I'm not sure how to add
this check to the lint, and I can't find any cases of this being done in
practice.
* I don't think an automatic fix for this lint is possible, but I'd love
to be proven wrong
* This is my first time contributing to clippy, please let me know if I
did anything wrong

Fixes #14087
```
changelog: [`unbuffered_bytes`]: new lint
```
2025-02-12 15:17:52 +00:00
Jason Newcomb
a342340e6d
add index checks for the slice in manual_slice_fill (#14193)
fix #14192

changelog: [`manual_slice_fill`]: resolve FP caused by missing index
checks for the slice
2025-02-12 15:14:36 +00:00
jonathan
8b6de49ef7
New lint: unbuffered_bytes 2025-02-12 14:57:07 +01:00
llogiq
32aef114c6
{expect,unwrap}_used: add options to lint at compilation time (#14200)
By default, do not lint `.unwrap()` and `.expect(…)` in always const
contexts, as a failure would be detected at compile time anyway.

New options `allow_expect_in_consts` and `allow_unwrap_in_consts`,
defaulting to `true`, can be turned unset to still lint in always const
contexts.

Close #14198

changelog: [`unwrap_used`, `expect_used`]: add new option to lint in
always constant contexts
2025-02-12 12:38:00 +00:00
Samuel Tardieu
da6a05977d {expect,unwrap}_used: add options to lint at compilation time
By default, do not lint `.unwrap()` and `.expect(…)` in always const
contexts, as a failure would be detected at compile time anyway.

New options `allow_expect_in_consts` and `allow_unwrap_in_consts`,
defaulting to `true`, can be turned unset to still lint in always const
contexts.
2025-02-11 22:14:52 +01:00
Alejandra González
ffa1caf420
just_underscores_and_digits: fix false positive in error recovery scenario (#14168)
Fixes #12302

changelog: [`just_underscores_and_digits`]: fix false positive in error
recovery scenario
2025-02-11 19:24:48 +00:00
Michael Howell
ac15a10b55 just_underscores_and_digits: ignore empty ident
Empty idents come from error recovery, and should imply that a
better error has already been emitted.
2025-02-11 09:35:43 -07:00
Alejandra González
c40898e186
declare_interior_mutable_const, borrow_interior_mutable_const: resolve <T as Trait>::AssocT projections (#14125)
changelog: [`declare_interior_mutable_const`,
`borrow_interior_mutable_const`]: resolve `<T as Trait>::AssocT`
projections

---

This came up during https://github.com/rust-lang/rust/pull/130543 where
we have `<T as AtomicPrimitive>::Assoc = AtomicT` instead of just
`AtomicT` and clippy failed to resolve that properly.

This really needs a review, because

- I don't know if `try_normalize_erasing_regions` is the right thing to
call here.
- I'm not sure if I peel off the correct amount of `ValTree::Branch`
layers (I think I do).

Also, shouldn't this lint's infrastructure rely on `Freeze` trait
(https://github.com/rust-lang/rust/issues/121675) instead of hardcoding
a list of known-to-be-interior-mutable types?

---

Previously filed this in the main rust repo
(https://github.com/rust-lang/rust/pull/136369), was asked to do it here
instead
(https://github.com/rust-lang/rust/pull/136369#issuecomment-2628527774).
2025-02-11 16:32:35 +00:00
lapla-cogito
7139436f98
add index checks for the slice in manual_slice_fill 2025-02-11 16:13:26 +09:00
Jason Newcomb
ff87bead4f
Use MIR body to identify more "default equivalent" calls for derivable_impls (#13988)
When looking for `Default` impls that could be derived, we look at the
body of their `fn default()` and if it is an fn call or literal we check
if they are equivalent to what `#[derive(Default)]` would have used.

Now, when checking those fn calls in the `fn default()` body, we also
compare against the corresponding type's `Default::default` body to see
if our call is equivalent to that one.

For example, given

```rust
struct S;

impl S {
    fn new() -> S { S }
}

impl Default for S {
    fn default() -> S { S::new() }
}
```

`<S as Default>::default()` and `S::new()` are considered equivalent.
Given that, if the user also writes

```rust
struct R {
    s: S,
}

impl Default for R {
    fn default() -> R {
        R { s: S::new() }
    }
}
```

the `derivable_impls` lint will now trigger.

changelog: [`derivable_impls`]: detect when a `Default` impl is using
the same fn call that that type's `Default::default` calls
changelog: [`mem_replace_with_default`]: detect when `std::mem::replace`
is being called with the same fn that that type's `Default::default`
calls, without the need of a manually maintained list
2025-02-11 03:50:55 +00:00
Esteban Küber
39d73d5bbb Use MIR body to identify more "default equivalent" calls
When looking for `Default` impls that could be derived, we look at the
body of their `fn default()` and if it is an fn call or literal we check
if they are equivalent to what `#[derive(Default)]` would have used.

Now, when checking those fn calls in the `fn default()` body, we also
compare against the corresponding type's `Default::default` body to see
if our call is equivalent to that one.

For example, given

```rust
struct S;

impl S {
    fn new() -> S { S }
}

impl Default for S {
    fn default() -> S { S::new() }
}
```

`<S as Default>::default()` and `S::new()` are considered equivalent.
Given that, if the user also writes

```rust
struct R {
    s: S,
}

impl Default for R {
    fn default() -> R {
        R { s: S::new() }
    }
}
```

the `derivable_impls` lint will now trigger.
2025-02-11 02:59:06 +00:00
Michael Howell
aff497f17f Use a separate loop to drive the check for code clusters
By using a separate loop, I can just skip nodes that I don't want
to process twice, instead of having to hand-build a state machine
with an enum.
2025-02-10 14:43:03 -07:00
Philipp Krones
b7ec4c13f3
correct "Affected lints" for allow-one-hash-in-raw-strings (#14186)
The `needless_raw_string_hashes` lint was implemented in #10884.
However, the name originally considered might have been
`unnecessary_raw_string_hashes`, so this part refers to a non-existent
lint.

r? flip1995

changelog: none
2025-02-10 14:19:42 +00:00
llogiq
8939915d5b
Use parentheses when needed in nonminimal_bool lint (#14187)
Since comparisons on types not implementing `Ord` (such as `f32`) are
not inverted, they must be enclosed in parentheses when they are
negated.

Fix #14184

changelog: [`nonminimal_bool`]: add required parentheses when negating a
binary expression
2025-02-10 12:42:10 +00:00
Samuel Tardieu
b32ad4ce0a Use parentheses when needed in nonminimal_bool lint
Since comparisons on types not implementing `Ord` (such as `f32`) are
not inverted, they must be enclosed in parentheses when they are
negated.
2025-02-10 12:47:54 +01:00
lapla-cogito
749eb371bf
correct "Affected lints" for allow-one-hash-in-raw-strings 2025-02-10 20:20:27 +09:00
llogiq
521a8001ca
Fix let_and_return with temporary variables, and distinguish between Rust editions (#14180)
The first commit fixes #14164 by making sure that temporaries with
non-static references are also looked for in expressions coming from
expansion. The shortcut that was done skipped those parts and reported
an absence of short-lived temporaries, which was incorrect.

The second commit distinguishes between edition 2024 and earlier ones.
Starting from edition 2024, the problematic drop order has been fixed,
and block variables, which might be referenced in a block expression,
are freed after the block expression itself. This allows more
`let_and_return` cases to be reported starting with edition 2024,
whereas in earlier editions an intermediary variable was necessary to
reorder the drops.

Incidentally, since Clippy is compiled in edition 2024 mode, the second
commit has led to a fix in
`clippy_lints/src/matches/significant_drop_in_scrutinee.rs`.

changelog: [`let_and_return`]: lint more cases in edition 2024, and fix
a false positive involving short-lived block temporary variables in
earlier editions.
2025-02-09 23:12:29 +00:00
llogiq
c3239baed0
Add single_option_map lint (#14033)
Checks for functions with method calls to `.map(_)` on an arg of type
`Option` as the outermost expression.

Fixes #774
```
changelog: [`single_option_map`]: Checks for functions with method calls to `.map(_)` on an arg of type `Option` as the outermost expression.
```
2025-02-09 21:57:59 +00:00
Yusuf Raji
4d4ef0000c Add single_option_map lint 2025-02-09 22:53:10 +01:00
Samuel Tardieu
5d2fe079ab let_and_return: lint more cases in edition ≥ 2024 2025-02-09 19:03:45 +01:00
Samuel Tardieu
657dda7b50 let_and_return: look for non-static references in expansion as well
One cannot avoid descending into expansion results when looking for
non-static references, or there is a risk of false negative which would
then trigger the `let_and_return` lint.
2025-02-09 18:18:02 +01:00
Alex Macleod
d7fd1c8e3c
make [manual_map] ignore types that contain dyn (#12712)
fixes: #12659

[`manual_map`] and [`manual_filter`] shares the same check logic, but
this issue doesn't seems like it could affect `manual_filter` (?)

---

changelog: make [`manual_map`] ignore types that contain `dyn`
2025-02-09 15:51:03 +00:00
Alejandra González
8c01600e23
Fix obfuscated_if_else suggestion on left side of a binary expr (#14124)
An `if … { … } else { … }` used as the left operand of a binary
expression requires parentheses to be parsed as an expression.

Fix #11141

changelog: [`obfuscated_if_else`]: fix bug in suggestion by issuing
required parentheses around the left side of a binary expression
2025-02-09 01:22:58 +00:00
Samuel Tardieu
ac0a11a8bc Fix obfuscated_if_else suggestion on left side of a binary expr
An `if … { … } else { … }` used as the left operand of a binary
expression requires parentheses to be parsed as an expression.
2025-02-09 02:11:37 +01:00
Alejandra González
77344b8c58
remove "Known problems" section for branches_sharing_code (#14176)
This is outdated.

changelog: none
2025-02-09 01:01:56 +00:00
Samuel Tardieu
aad3686823 Add error markers for obfuscated_if_else lint 2025-02-09 02:00:04 +01:00
lapla-cogito
5e3b968da0
remove "Known problems" section for branches_sharing_code 2025-02-09 06:49:38 +09:00
Catherine Flores
6cdb7f68c3
allow assign_op_pattern in the test of string_add (#14143)
Since `assign_op_pattern` is a style lint, not explicitly allowing it
can lead to undesirable output in other lint checks.

changelog: none
2025-02-08 12:34:52 +00:00
Catherine Flores
0ff95402c7
Two improvements to disallowed_* (#13669)
The improvements are as follows:
- Remove an [unnecessary `compile-flags`
directive](f712eb5cdc/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs (L1))
in `tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs`
- Support replacements as suggested by @mitsuhiko in
https://github.com/rust-lang/rust-clippy/issues/7609#issuecomment-908632622

---

changelog: support replacements in `disallowed_methods`
2025-02-08 11:32:11 +00:00
dswij
8cc596cf95
autofix for range_zip_with_len (#14136)
changelog: [`range_zip_with_len`]: add autofix
2025-02-08 05:20:10 +00:00
dswij
5211148dbd
add MSRV check for manual_flatten (#14086)
`manual_flatten` should respect MSRV.

changelog: [`manual_flatten`]: add MSRV check
2025-02-08 04:17:43 +00:00
dswij
a25e1526e4
Remove Known problems section invalid_upcast_comparisons (#14133)
Remove `Known problems` section `invalid_upcast_comparisons` since issue
was fixed #886

changelog: none
2025-02-07 17:34:49 +00:00