Commit graph

12559 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
Catherine Flores
b5ea2491b3
[path_buf_push_overwrite]: mark suggestion as MaybeIncorrect (#14010)
Proposing to replace

```rust
let mut x = PathBuf::from("/foo");
x.push("/bar");
```

by

```rust
let mut x = PathBuf::from("/foo");
x.push("bar");
```

changes the content of `x` (`/bar` ⇒ `/foo/bar`). This is not equivalent
and should not be `MachineApplicable`, even if the original code is
suspicious.

changelog: none
2025-02-07 12:27:35 +00:00
Catherine Flores
9289cca712
remove an outdated line in transmute/mod.rs (#14159)
The `useless_transmute` already belongs to the complexity lint.

changelog: none
2025-02-07 12:25:26 +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
Catherine Flores
c529b70e14
Fix ICE in unnecessary_mut_passed (#14065)
fix #12171

changelog: none
2025-02-07 12:17:54 +00:00
llogiq
33394d28be
Correct version of doc_overindented_list_items (#14152)
Fix the version of `doc_overindented_list_items`. It actually will be in
1.86.0, not 1.80.0.
(https://github.com/rust-lang/rust/pull/136209 has a milestone of
1.86.0)

changelog: none
2025-02-07 10:07:50 +00:00
Timo
4a94ad6c52
Simplify reindent_multiline() signature (#14101)
- `reindent_multiline()` always returns the result of
`reindent_multiline_inner()` which returns a `String`. Make
`reindent_multiline()` return a `String` as well, instead of a
systematically owned `Cow<'_, str>`.
- There is no reason for `reindent_multiline()` to force a caller to
build a `Cow<'_, str>` instead of passing a `&str` directly, especially
considering that a `String` will always be returned.

Also, both the input parameter and return value (of type `Cow<'_, str>`)
shared the same (elided) lifetime for no reason: this worked only
because the result was always the `Cow::Owned` variant which is
compatible with any lifetime.

As a consequence, the signature changes from:

```rust
fn reindent_multiline(s: Cow<'_, str>, …) -> Cow<'_, str> { … }
```

to

```rust
fn reindent_multiline(s: &str, …) -> String { … }
```

changelog: none
2025-02-07 00:45:33 +00:00
Jason Newcomb
512b08f8b1
change the applicability of if_then_some_else_none to MachineApplicable (#14106)
`MachineApplicable` is appropriate for the applicability of this lint.

changelog: [`if_then_some_else_none`]: change the applicability to
`MachineApplicable`
2025-02-06 20:54:14 +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
bcfd0d1aba Skip use_self inside macro expansion of impl Self items 2025-02-06 14:34:55 +07:00
Lzu Tao
925718d8eb Make the "expensive" comment belong to a branch 2025-02-06 14:31:39 +07:00
Lzu Tao
83b97ae713 Use clippy_utils::ty::ty_from_hir_ty to avoid ICE 2025-02-06 14:31:39 +07:00
Lzu Tao
daab21ef9d Pulicize clippy_utils::ty::ty_from_hir_ty
And use it in the next commit to avoid ICE.
2025-02-06 14:31:39 +07:00
lapla-cogito
5f6dd7a8eb
remove an outdated line 2025-02-06 12:47:47 +09: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
Alex Macleod
390286d664
Update version for unneeded_struct_pattern (#14031)
This lint was merged recently
https://github.com/rust-lang/rust-clippy/pull/13465 and should go with
the next version 1.86.0 https://github.com/rust-lang/rust-clippy/tags

changelog: none
2025-02-05 13:55:22 +00:00
Yutaro Ohno
44fda914d3 Correct version of doc_overindented_list_items 2025-02-05 19:16:50 +09: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
Jason Newcomb
75e3a2e905
Move mutex_integer to restriction and improve mutex_{integer,atomic} docs (#14110)
Similar to https://github.com/rust-lang/rust-clippy/pull/10115 (for
`mutex_atomic`), but for `mutex_integer`.

This also improve docs of `mutex_integer`/`mutex_atomic` (extend known
problems section to mention condvar issue
(https://github.com/rust-lang/rust-clippy/issues/1516) and portability
issue).

changelog: Moved [`mutex_integer`] to `restriction`
2025-02-04 10:44:57 +00:00
Samuel Tardieu
64dec0760e Simplify reindent_multiline() signature
- `reindent_multiline()` always returns the result of
  `reindent_multiline_inner()` which returns a `String`. Make
  `reindent_multiline()` return a `String` as well, instead of a
  systematically owned `Cow<'_, str>`.
- There is no reason for `reindent_multiline()` to force a caller to
  build a `Cow<'_, str>` instead of passing a `&str` directly,
  especially considering that a `String` will always be returned.

Also, both the input parameter and return value (of type `Cow<'_, str>`)
shared the same (elided) lifetime for no reason: this worked only because
the result was always the `Cow::Owned` variant which is compatible with
any lifetime.

As a consequence, the signature changes from:

```rust
fn reindent_multiline(s: Cow<'_, str>, …) -> Cow<'_, str> { … }
```

to

```rust
fn reindent_multiline(s: &str, …) -> String { … }
```
2025-02-03 23:47:08 +01: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
e82b1f4653
add manual_slice_fill lint 2025-02-03 10:09:39 +09:00
Matthias Krüger
f8d702bf2d Rollup merge of #136422 - nnethercote:convert-lint-functions, r=Noratrieb
Convert two `rustc_middle::lint` functions to `Span` methods.

`rustc_middle` is a huge crate and it's always good to move stuff out of it. There are lots of similar methods already on `Span`, so these two functions, `in_external_macro` and `is_from_async_await`, fit right in. The diff is big because `in_external_macro` is used a lot by clippy lints.

r? ``@Noratrieb``
2025-02-02 18:05:24 +01:00
llogiq
0d3fd98892
remove "Known problems" of verbose_bit_mask (#14137)
changelog: none
2025-02-02 15:25:10 +00:00
lapla-cogito
af18dce76b
remove "Known problems" of verbose_bit_mask 2025-02-03 00:12:07 +09:00
Nicholas Nethercote
d30f045fce Convert two rustc_middle::lint functions to Span methods.
`rustc_middle` is a huge crate and it's always good to move stuff out of
it. There are lots of similar methods already on `Span`, so these two
functions, `in_external_macro` and `is_from_async_await`, fit right in.
The diff is big because `in_external_macro` is used a lot by clippy
lints.
2025-02-02 13:57:34 +11: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
Zalathar
64fcb00308 Rename tcx.ensure() to tcx.ensure_ok() 2025-02-01 12:38:54 +11: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
Catherine Flores
e188c7d029
sliced_string_as_bytes: fix typo in lint description (#14117)
changelog: none
2025-01-30 20:55:55 +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
Samuel Tardieu
5aa8bc11cd sliced_string_as_bytes: fix typo in lint description 2025-01-30 21:27:57 +01: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
Lukas Markeffsky
73993387fd introduce ty::Value
Co-authored-by: FedericoBruzzone <federico.bruzzone.i@gmail.com>
2025-01-30 17:47:44 +01: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
Taiki Endo
f8be51862a Move mutex_integer to restriction and improve mutex_{integer,atomic} docs 2025-01-30 23:32:05 +09: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
lapla-cogito
d13897b829
change the applicability of if_then_some_else_none to MachineApplicable 2025-01-30 11:57:45 +09: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