Commit graph

9436 commits

Author SHA1 Message Date
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
yanglsh
270e52f6da fix: manual_unwrap_or_default 2025-02-04 19:23:03 -07: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
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
lapla-cogito
f9669e4caf
add MSRV check for lines_filter_map_ok 2025-02-01 15:20:34 +09:00
dswij
d79f86255d
Do not remove semicolon if it changes the block type (#14103)
Removing the semicolon of the last statement of an expressionless block
may change the block type even if the statement's type is `()`. If the
block type is `!` because of a systematic early return, typing it as
`()` may make it incompatible with the expected type for the block (to
which `!` is cast).

Fix #14100

changelog: [`unnecessary_semicolon`]: do not remove semicolon if it
could change the block type from `!` to `()`
2025-01-31 10:47:24 +00:00
llogiq
398a5c2db9
New lint: precedence_bits, with recent additions to precedence (#14115)
Commit 2550530266 has extended the
`precedence` lint to include bitmasking and shift operations. The lint
is warn by default, and this generates many hits, especially in embedded
or system code, where it is very idiomatic to use expressions such as `1
<< 3 | 1 << 5` without parentheses.

This commit splits the recent addition into a new lint, which is put
into the "restriction" category, while the original one stays in
"complexity", because mixing bitmasking and arithmetic operations is
less typical.

Fix #14097

changelog: [`precedence_bits`]: new lint
2025-01-30 20:39:07 +00:00
Catherine Flores
f51e18de30
feat: new lint for and_then when returning Option or Result (#14051)
close #6436

changelog: [`return_and_then`]: new lint
2025-01-30 20:14:48 +00:00
Samuel Tardieu
8db9ecfd74 New lint: precedence_bits, with recent additions to precedence
Commit 2550530266 has extended the
`precedence` lint to include bitmasking and shift operations. The lint
is warn by default, and this generates many hits, especially in embedded
or system code, where it is very idiomatic to use expressions such as
`1 << 3 | 1 << 5` without parentheses.

This commit splits the recent addition into a new lint, which is put
into the "restriction" category, while the original one stays in
"complexity", because mixing bitmasking and arithmetic operations is
less typical.
2025-01-30 17:07:50 +01:00
llogiq
ad05bc055c
Include generic arguments when suggesting a closure η-reduction (#14105)
Fix #14096

changelog: [`redundant_closure_for_method_calls`]: repeat generic args
in suggestion
2025-01-30 05:15:35 +00:00
Alejandra González
88a00a87fa
autofix for redundant_else lint (#13936)
changelog: [`redundant_else`]: autofix for some cases

`redundant_else` can be fixed automatically.
2025-01-30 00:01:31 +00:00
Samuel Tardieu
e4505fb670 Include generic arguments when suggesting a closure η-reduction 2025-01-29 23:59:23 +01:00
Samuel Tardieu
78d6b2ea4e Do not remove semicolon if it changes the block type
Removing the semicolon of the last statement of an expressionless block
may change the block type even if the statement's type is `()`. If the
block type is `!` because of a systematic early return, typing it as
`()` may make it incompatible with the expected type for the block (to
which `!` is cast).
2025-01-29 23:01:08 +01:00
Aaron Ang
84fb6b1651 Add new lint return_and_then 2025-01-29 10:43:59 -08:00
Samuel Tardieu
b8275293ba Don't use labeled block as top-level blocks
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.
2025-01-29 18:46:38 +01:00
Catherine Flores
6a209cdc90
Fix escaping problem in write_literal and print_literal lint suggestion (#13990)
fix #13959

The current implementation of the `write_literal` and `print_literal`
lint performs escaping for the second argument of `write!` ,`writeln!`,
`print!` and `println!` of the suggestion by first replacing `"` with
`\"`, and then replacing `\` with `\\`. Performing these replacements in
this order may lead to unnecessary backslashes being added if the
original code contains `"` (e.g. `"` -> `\\"`), potentially resulting in
a suggestion that causes the code to fail to compile.
In the issue mentioned above, it’s suggested to use raw strings as raw
strings, but implementing this would require an ad-hoc change to the
current implementation, so it has been deferred. (I'll implement this in
another PR)

changelog: [`write_literal`]: fix incorrect escaping of suggestions
changelog: [`print_literal`]: fix incorrect escaping of suggestions
2025-01-28 19:29:57 +00:00
llogiq
9ede32fe00
needless_option_take: add autofix (#14042)
changelog: [`needless_option_take`]: add autofix
2025-01-28 18:49:27 +00:00
Philipp Krones
9da9ddb7db Merge commit '51d49c1ae2' into clippy-subtree-update 2025-01-28 19:33:54 +01:00
Philipp Krones
145d5adf04
Merge remote-tracking branch 'upstream/master' into rustup 2025-01-28 19:14:45 +01:00
Yutaro Ohno
4693d0a9ff Add new lint doc_overindented_list_items
Add a new lint `doc_overindented_list_items` to detect and fix list items
in docs that are overindented.

For example,

```rs
/// - first line
///      second line
fn foo() {}
```

this would be fixed to:

```rs
/// - first line
///   second line
fn foo() {}
```

This lint improves readabiliy and consistency in doc.
2025-01-28 16:22:28 +09:00
Catherine Flores
66dc8a1a30
proper applicability for obfuscated_if_else (#14061)
fix #14034

The currect implementation of `obfuscated_if_else` sometimes makes
incorrect suggestions when the original code have side effects (see the
example in the above issue). I think this can be fixed by changing the
applicability depending on whether it can have side effects or not.

changelog: [`obfuscated_if_else`]: change applicability when the
original code can have side effects
2025-01-28 06:57:12 +00:00
Samuel Tardieu
16e2196fc2 useless_asref: no lint if in a closure to change the ref depth
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.
2025-01-28 00:08:39 +01:00
Manish Goregaokar
83bde363b6
correct suggestions in no_std (#13999)
I opened https://github.com/rust-lang/rust-clippy/pull/13896 before.
However, I found that there're more cases where Clippy suggests to use
modules that belong to the `std` crate even in a `no_std` environment.
Therefore, this PR include the changes I've made in #13896 and new
changes to fix cases I found this time to prevent wrong suggestions in
`no_std` environments as well.

changelog: [`redundant_closure`]: correct suggestion in `no_std`
changelog: [`repeat_vec_with_capacity`]: correct suggestion in `no_std`
changelog: [`single_range_in_vec_init`]: don't emit suggestion to use
`Vec` in `no_std`
changelog: [`drain_collect`]: correct suggestion in `no_std`
changelog: [`map_with_unused_argument_over_ranges`]: correct suggestion
in `no_std`

also close #13895
2025-01-27 21:36:12 +00:00
lapla-cogito
3b0b8b080e
correct suggestion for map_with_unused_argument_over_ranges in a no_std environment 2025-01-28 06:29:47 +09:00
lapla-cogito
d99eae4325
correct suggestion for drain_collect in a no_std environment 2025-01-28 06:29:45 +09:00
lapla-cogito
e146039e17
correct suggestion for repeat_vec_with_capacity in a no_std environment 2025-01-28 06:29:37 +09:00
lapla-cogito
43b29da91e
correct suggestion for redundant_closure in a no_std environment 2025-01-28 06:24:44 +09:00
Manish Goregaokar
85bbba69b0
New lint sliced_string_as_bytes (#14002)
resurrection of https://github.com/rust-lang/rust-clippy/pull/10984

fixes https://github.com/rust-lang/rust-clippy/issues/10981

changelog: [`sliced_string_as_bytes`]: add new lint
`sliced_string_as_bytes`
2025-01-27 20:34:48 +00:00
Andre Bogus
13be95ab11 new manual_option_as_slice lint 2025-01-26 21:27:44 +01:00
Alejandra González
057f5b36f5
Make unnecessary_map_or work with ref and Deref to Option/Result (#14024)
Receivers which are references to `Option` and `Result`, or who
implement `Deref` to one of those types, will be linted as well.

changelog: [`unnecessary_map_or`]: work with ref and `Deref` to `Option`
and `Result` as well

Fixes #14023

**Note:** this patch must be merged after #13998 – only the second
commit must be reviewed, the first one repeats the patch in #13998 for
mergeability reasons.
2025-01-26 18:06:49 +00:00
Kalle Wachsmuth
25a77cf4f4 remove clippy::double_neg 2025-01-26 12:15:12 +01:00
lapla-cogito
2640f657ec
change the applicability of obfuscated_if_else depending on whether the original code can have side effects 2025-01-26 10:03:20 +09:00
llogiq
913592373d
trigger obfuscated_if_else for .then(..).unwrap_or(..) (#14021)
part of https://github.com/rust-lang/rust-clippy/issues/9100

The `obfuscated_if_else` lint currently only triggers for the pattern
`.then_some(..).unwrap_or(..)`, but there're other cases where this lint
should be triggered, one of which is `.then(..).unwrap_or(..)`.

changelog: [`obfuscated_if_else`]: trigger lint for the
`.then(..).unwrap_or(..)` pattern as well
2025-01-25 23:48:02 +00:00