Commit graph

21717 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
c6a861615e
add manual_option_as_slice MSRV to the lint documentation (#14171)
`manual_option_as_slice` takes MSRV into account, but this is not
mentioned in the lint documentation.

changelog: none
2025-02-07 12:28:06 +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
lapla-cogito
16781bf29d
add manual_option_as_slice to the lint documentation 2025-02-07 21:00:39 +09: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
Timo
dc330b89a8
Use edition 2024 when running cargo dev lint (#14166)
This part has been missed when switching Clippy to edition 2024 a while
back.

changelog: none
2025-02-07 00:31:01 +00:00
Samuel Tardieu
0e42ba908c Permit specifying a non-default edition when linting file
`cargo dev lint /tmp/file.rs -- --edition 2021` will select edition
2021.
2025-02-07 00:21:49 +01: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
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
Samuel Tardieu
8a9d55012d Use edition 2024 when running cargo dev lint 2025-02-06 18:59:33 +01: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
3e3715c312
Rustup (#14163)
r? @ghost

changelog: none
2025-02-06 13:48:29 +00:00
Philipp Krones
9f31768adc
Bump nightly version -> 2025-02-06 2025-02-06 14:31:22 +01:00
Philipp Krones
f549562b81
Merge remote-tracking branch 'upstream/master' into rustup 2025-02-06 14:31:01 +01:00
Philipp Krones
660d861058
Fix docs for #[clippy::format_args] (#14161)
changelog: none
2025-02-06 09:23:50 +00:00
Philipp Krones
ee1c15eb56
Add Nursery and Deprecated groups section at Book (#13926)
[RFC](https://github.com/rust-lang/rfcs/blob/master/text/2476-clippy-uno.md#lint-audit-and-categories)
has more groups so add them

changelog: none
2025-02-06 09:23:22 +00:00
Yuri Astrakhan
694b245b20 Fix docs for #[clippy::format_args] 2025-02-06 03:21:49 -05: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
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
Alexey Semenyuk
c718ae8e2d Add Nursery and Deprecated lints section at Book 2025-02-05 23:09:08 +05: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
llogiq
e6d9641272
fix: manual_unwrap_or_default suggests falsely when condition type is uncertain (#13889)
fixes #12670

Continuation of #12688. r? @Jarcho if you don't mind?

changelog: [`manual_unwrap_or_default`] fix wrong suggestions when
condition type is uncertain
2025-02-05 06:05:58 +00:00
León Orell Valerian Liehr
4c11087e51 Rollup merge of #128045 - pnkfelix:rustc-contracts, r=oli-obk
#[contracts::requires(...)]  + #[contracts::ensures(...)]

cc https://github.com/rust-lang/rust/issues/128044

Updated contract support: attribute syntax for preconditions and postconditions, implemented via a series of desugarings  that culminates in:
1. a compile-time flag (`-Z contract-checks`) that, similar to `-Z ub-checks`, attempts to ensure that the decision of enabling/disabling contract checks is delayed until the end user program is compiled,
2. invocations of lang-items that handle invoking the precondition,  building a checker for the post-condition, and invoking that post-condition checker at the return sites for the function, and
3. intrinsics for the actual evaluation of pre- and post-condition predicates that third-party verification tools can intercept and reinterpret for their own purposes (e.g. creating shims of behavior that abstract away the function body and replace it solely with the pre- and post-conditions).

Known issues:

 * My original intent, as described in the MCP (https://github.com/rust-lang/compiler-team/issues/759) was   to have a rustc-prefixed attribute namespace (like   rustc_contracts::requires). But I could not get things working when I tried   to do rewriting via a rustc-prefixed builtin attribute-macro. So for now it  is called `contracts::requires`.

 * Our attribute macro machinery does not provide direct support for attribute arguments that are parsed like rust expressions. I spent some time trying to add that (e.g. something that would parse the attribute arguments as an AST while treating the remainder of the items as a token-tree), but its too big a lift for me to undertake. So instead I hacked in something approximating that goal, by semi-trivially desugaring the token-tree attribute contents into internal AST constucts. This may be too fragile for the long-term.
   * (In particular, it *definitely* breaks when you try to add a contract to a function like this: `fn foo1(x: i32) -> S<{ 23 }> { ... }`, because its token-tree based search for where to inject the internal AST constructs cannot immediately see that the `{ 23 }` is within a generics list. I think we can live for this for the short-term, i.e. land the work, and continue working on it while in parallel adding a new attribute variant that takes a token-tree attribute alongside an AST annotation, which would completely resolve the issue here.)

* the *intent* of `-Z contract-checks` is that it behaves like `-Z ub-checks`, in that we do not prematurely commit to including or excluding the contract evaluation in upstream crates (most notably, `core` and `std`). But the current test suite does not actually *check* that this is the case. Ideally the test suite would be extended with a multi-crate test that explores the matrix of enabling/disabling contracts on both the upstream lib and final ("leaf") bin crates.
2025-02-05 05:03:01 +01: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
Celina G. Val
0a8331f681 Express contracts as part of function header and lower it to the contract lang items
includes post-developed commit: do not suggest internal-only keywords as corrections to parse failures.

includes post-developed commit: removed tabs that creeped in into rustfmt tool source code.

includes post-developed commit, placating rustfmt self dogfooding.

includes post-developed commit: add backquotes to prevent markdown checking from trying to treat an attr as a markdown hyperlink/

includes post-developed commit: fix lowering to keep contracts from being erroneously inherited by nested bodies (like closures).

Rebase Conflicts:
 - compiler/rustc_parse/src/parser/diagnostics.rs
 - compiler/rustc_parse/src/parser/item.rs
 - compiler/rustc_span/src/hygiene.rs

Remove contracts keywords from diagnostic messages
2025-02-03 12:54:00 -08:00
Felix S. Klock II
1112801251 Contracts core intrinsics.
These are hooks to:

  1. control whether contract checks are run
  2. allow 3rd party tools to intercept and reintepret the results of running contracts.
2025-02-03 12:53:57 -08:00
Oli Scherer
c1e4249e0d Use a different hir type for patterns in pattern types than we use in match patterns 2025-02-03 08:18:30 +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
3d6e090b31
add SLICE_FILL to msrv 2025-02-03 10:09:33 +09:00
Matthias Krüger
1a8e9b9917 Rollup merge of #136445 - bjorn3:diag_ctxt_cleanup, r=oli-obk
Couple of cleanups to DiagCtxt and EarlyDiagCtxt
2025-02-02 23:06:57 +01:00