Commit graph

9470 commits

Author SHA1 Message Date
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
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
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
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
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
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
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
Samuel Tardieu
aad3686823 Add error markers for obfuscated_if_else lint 2025-02-09 02:00:04 +01: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
4e5d00a0a7
Deprecate redundant lint option_map_or_err_ok and take manual_ok_or out of pedantic (#14027)
While extending the `option_map_or_err_ok` lint (warn by default,
"style") to recognize η-expanded forms of `Ok`, as in

```rust
    // Should suggest `opt.ok_or("foobar")`
   let _ = opt.map_or(Err("foobar"), |x| Ok(x));
```

I discovered that the `manual_ok_or` lint (allow by default, "pedantic")
already covered exactly the cases handled by `option_map_or_err_ok`,
including the one I was adding. Apparently, `option_map_or_err_ok` was
added without realizing that the lint already existed under the
`manual_ok_or` name. As a matter of fact, artifacts of this second lint
were even present in the first lint `stderr` file and went unnoticed for
more than a year.

This PR:
- deprecates `option_map_or_err_ok` with a message saying to use
`manual_ok_or`
- moves `manual_ok_or` from "pedantic" to "style" (the category in which
`option_map_or_err_ok` was)

In addition, I think that this lint, which is short, machine applicable,
and leads to shorter and clearer code with less arguments (`Ok`
disappears) and the removal of one level of call (`Err(x)` is replaced
by `x`), is a reason by itself to be in "style".

changelog: [`option_map_or_err_ok` and `manual_ok_or`]: move
`manual_ok_or` from "pedantic" to "style", and deprecate the redundant
style lint `option_map_or_err_ok`.
2025-02-07 17:34:21 +00:00
Catherine Flores
0d3bf65bd4
useless_asref: no lint if in a closure to change the ref depth (#14090)
Removing the `.as_ref()` or `.as_mut()` as the top-level expression in a
closure may change the type of the result. In this case, it may be
better not to lint rather than proposing a fix that would not work.

changelog: [`useless_asref`]: do not remove the `.as_ref()` or
`.as_mut()` call if this would change the type of the enclosing closure

Fix #14088
2025-02-07 13:33:18 +00:00
llogiq
f6d23c8d59
Handle more cases in is_normalizable (#13833)
By assuming that a recursive type is normalizable within the deeper
calls to `is_normalizable_helper()`, more cases can be handled by this
function.

In order to fix stack overflows, a recursion limit has also been added
for recursive generic type instantiations.

Fix #9798
Fix #10508
Fix #11915

changelog: [`large_enum_variant`]: more precise detection of variants
with large size differences
2025-02-07 13:20:11 +00:00
Catherine Flores
b0ad06daa8
add MSRV check for lines_filter_map_ok (#14130)
fixes #14127

changelog: [`lines_filter_map_ok`]: respect MSRV
2025-02-07 12:24:50 +00:00
Jason Newcomb
888365d7e5
Don't use labeled block as top-level blocks (#14102)
Labeled blocks cannot be used as-is in the "then" or "else" part of an
`if` expression. They must be enclosed in an anonymous block.

Fix #14099

changelog: [`match_bool`]: fix suggestion when the rewritten block has a
label
2025-02-06 20:51:41 +00:00
Jason Newcomb
7cda242e3c
don't emit suggestion inside macro in manual_async_fn (#14142)
fixes #12407

I think it is meaningful to emit a warning even if the span is in a
macro.

changelog: [`manual_async_fn`]: don't emit suggestion inside macro
2025-02-06 20:38:46 +00:00
Alex Macleod
20b2461938
Skip use_self inside macro expansions of a impl Self block (#13128)
changelog: [`use_self`] Skip if inside macro expansions of a `impl Self`
block
Fixes #13092.
r? Alexendoo
2025-02-06 14:43:39 +00:00
Philipp Krones
f549562b81
Merge remote-tracking branch 'upstream/master' into rustup 2025-02-06 14:31:01 +01:00
Lzu Tao
9ea2b6501e add test to check for popping wrong items
co-authored-by: Alex Macleod <alex@macleod.io>
2025-02-06 14:34:55 +07:00
Lzu Tao
bcfd0d1aba Skip use_self inside macro expansion of impl Self items 2025-02-06 14:34:55 +07:00
Lzu Tao
e3e6e6ea41 add bug 13092 2025-02-06 14:31:39 +07:00
Alejandra González
f09701ab6a
Do not trigger [size_of_in_element_count] for u8 (#14011)
Counting in bytes for a pointer to `u8` is legitimate and must not
trigger the lint. Also, this prevents linting the
`{std,core}::ptr::write_bytes` as it manipulates bytes.

Fix #6590

changelog: [`size_of_in_element_count`]: do not lint if the pointee type
is `u8`
2025-02-05 22:17:10 +00:00
lapla-cogito
60f9445900
don't emit lint inside macro in manual_async_fn 2025-02-05 15:06:35 +09:00
J-ZhengLi
2a4be5365a move expr_requires_coercion to clippy_utils & some other adjustments 2025-02-05 10:26:17 +08:00
yanglsh
270e52f6da fix: manual_unwrap_or_default 2025-02-04 19:23:03 -07:00
J-ZhengLi
a2f9861df8 fix [manual_map] not catching type adjustment 2025-02-05 10:18:13 +08:00
lapla-cogito
4220dbd4b3
allow assign_op_pattern in the test of string_add 2025-02-04 12:19:04 +09:00
Alejandra González
c5218d509b
new manual_option_as_slice lint (#13901)
Hey folks. It's been a while since I added the `as_slice` method to
`Option`, and I totally forgot about a lint to suggest it. Well, I had
some time around Christmas, so here it is now.

---

changelog: add [`manual_option_as_slice`] lint
2025-02-03 22:00:38 +00:00
llogiq
2c51951dba
add manual_slice_fill lint (#14082)
close #13856

changelog: [`manual_slice_fill`]: new lint
2025-02-03 05:03:14 +00:00
lapla-cogito
07ede9c027
ignore manual_slice_fill in other test files 2025-02-03 10:09:40 +09:00
lapla-cogito
e82b1f4653
add manual_slice_fill lint 2025-02-03 10:09:39 +09:00
lapla-cogito
a5329bd8d3
autofix for range_zip_with_len 2025-02-02 23:44:22 +09:00
Alex Macleod
b8d0b16096
autofix for cmp_null (#14122)
changelog: [`cmp_null`]: add autofix
2025-02-01 22:59:06 +00:00
lapla-cogito
3155dabeaa
add autofix for cmp_null 2025-02-02 04:22:19 +09:00
Samuel Tardieu
7848488e26 Use a better message for toplevel_ref_arg lint
A `ref` pattern applied to an argument is not ignored. It creates a
reference as expected, but still requires the function to take ownership
of the argument given to it.
2025-02-01 14:23:31 +01:00