Commit Graph

1991 Commits

Author SHA1 Message Date
Alex Macleod
0972c3b565 Check for MSRV attributes in late passes using the HIR 2025-02-28 18:09:44 +00:00
Samuel Tardieu
baadee8fd3 New lint: manual_midpoint 2025-02-27 22:12:16 +01:00
Samuel Tardieu
06175f43b3 Add is_float_literal utility 2025-02-27 22:10:57 +01:00
Philipp Krones
b3d35b1af6 Bump nightly version -> 2025-02-27 2025-02-27 21:51:42 +01:00
Philipp Krones
53a1ff70e8 Check os_str_display MSRV instead of feature
This feature was stabilized, so the FormatArgs lints should check if the MSRV of
the stabilization is met, rather than checking if the feature is enabled.
2025-02-27 21:51:42 +01:00
Philipp Krones
02e812af4d Merge remote-tracking branch 'upstream/master' into rustup 2025-02-27 21:51:42 +01:00
Alejandra González
162b0e8c4a Add todo! & unimplemented! to format macros list (#14266)
For some reason, the `todo!` and `unimplemented!` macros were not
included in the list of format-supporting macros list. Since they seem
to behave exactly the same as all others like `write!` and `assert!`,
adding them now.

I wonder if we should delete the `FORMAT_MACRO_DIAG_ITEMS`, and instead
tag all macros with the `#[clippy::format_args]`?

changelog: all format-handling lints will now validate `todo!` and
`unimplemented!` macros.
2025-02-24 22:07:35 +00:00
Jana Dönszelmann
443b0f5ccf Fix rustdoc and clippy 2025-02-24 14:31:19 +01:00
Michael Goulet
ba8079d0db Make a fake body to store typeck results for global_asm 2025-02-22 00:12:07 +00:00
Andre Bogus
6366cca439 add io_other_error lint 2025-02-21 22:08:31 +01:00
bors
49082f94fc Auto merge of #137397 - matthiaskrgr:rollup-ls2pilo, r=matthiaskrgr
Rollup of 10 pull requests

Successful merges:

 - #132876 (rustdoc book: acknowledge --document-hidden-items)
 - #136148 (Optionally add type names to `TypeId`s.)
 - #136609 (libcore/net: `IpAddr::as_octets()`)
 - #137336 (Stabilise `os_str_display`)
 - #137350 (Move methods from Map to TyCtxt, part 3.)
 - #137353 (Implement `read_buf` for WASI stdin)
 - #137361 (Refactor `OperandRef::extract_field` to prep for MCP838)
 - #137367 (Do not exempt nonexistent platforms from platform policy)
 - #137374 (Stacker now handles miri using a noop impl itself)
 - #137392 (remove few unused fields)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-02-21 19:57:50 +00:00
Alejandra González
649cef0e81 rename MANUAL_DIV_CEIL MSRV alias and add missing conf info for manual_div_ceil (#14263)
- The name of an MSRV alias should describe its functionality, and it is
not appropriate for it to be the same as the name of the lint that uses
it.
- Additionally, while `manual_div_ceil` allows setting MSRV, this is not
correctly reflected in the configuration information.

changelog: none
2025-02-21 18:02:30 +00:00
Matthias Krüger
85329db705 Rollup merge of #137299 - nnethercote:simplify-PostOrder-customization, r=compiler-errors
Simplify `Postorder` customization.

`Postorder` has a `C: Customization<'tcx>` parameter, that gives it flexibility about how it computes successors. But in practice, there are only two `impls` of `Customization`, and one is for the unit type.

This commit simplifies things by removing the generic parameter and replacing it with an `Option`.

r? ````@saethlin````
2025-02-21 12:45:24 +01:00
Samuel Tardieu
4ac7a5361b Represent the capability instead of the lint name in msrv aliases
`INTEGER_BITS` better represents the addition of the `BITS` value on the
primitive integer types.
2025-02-21 09:44:18 +01:00
Yuri Astrakhan
373f809c1d Add todo! & unimplemented! to format macros list
For some reason, the `todo!` and `unimplemented!` macros were not included in the list of format-supporting macros list. Since they seem to behave exactly the same as all others like `write!` and `assert!`, adding them now.
2025-02-20 22:59:05 -05:00
Nicholas Nethercote
c2d75cff47 Move methods from Map to TyCtxt, part 3.
Continuing the work from #137162.

Every method gains a `hir_` prefix.
2025-02-21 14:31:09 +11:00
Philipp Krones
934471ce30 Bump nightly version -> 2025-02-20 2025-02-20 15:27:19 +01:00
Philipp Krones
8844a969ca Bump Clippy version -> 0.1.87 2025-02-20 15:26:51 +01:00
lapla-cogito
3c9a0ab38c rename the MSRV alias MANUAL_DIV_CEIL to DIV_CEIL 2025-02-20 23:26:51 +09:00
Philipp Krones
12025085b9 Merge remote-tracking branch 'upstream/master' into rustup 2025-02-20 15:26:07 +01:00
Nicholas Nethercote
90bd46bfc3 Simplify Postorder customization.
`Postorder` has a `C: Customization<'tcx>` parameter, that gives it
flexibility about how it computes successors. But in practice, there are
only two `impls` of `Customization`, and one is for the unit type.

This commit simplifies things by removing the generic parameter and
replacing it with an `Option`.
2025-02-20 14:00:36 +11:00
Matthias Krüger
a81c2648dc Rollup merge of #136923 - samueltardieu:push-vxxqvqwspssv, r=davidtwco
Lint `#[must_use]` attributes applied to methods in trait impls

The `#[must_use]` attribute has no effect when applied to methods in trait implementations. This PR adds it to the unused `#[must_use]` lint, and cleans the extra attributes in portable-simd and Clippy.
2025-02-19 21:16:11 +01:00
Catherine Flores
975a813c5a .last() to .next_back() requires a mutable receiver (#14140)
In the case where `iter` is a `DoubleEndedIterator`, replacing a call to
`iter.last()` (which consumes `iter`) by `iter.next_back()` (which
requires a mutable reference to `iter`) cannot be done when `iter` is a
non-mutable binding which is not a mutable reference. When possible, a
local immutable binding is made into a mutable one.

Also, the applicability is switched to `MaybeIncorrect` and a note is
added to the output when the element types have a significant drop,
because the drop order will potentially be modified because
`.next_back()` does not consume the iterator nor the elements before the
last one.

Fix #14139

changelog: [`double_ended_iterator_last`]: do not trigger on
non-reference immutable receiver, and warn about possible drop order
change
2025-02-19 09:05:46 +00:00
Nicholas Nethercote
835e6a694b Move methods from Map to TyCtxt, part 2.
Continuing the work started in #136466.

Every method gains a `hir_` prefix, though for the ones that already
have a `par_` or `try_par_` prefix I added the `hir_` after that.
2025-02-18 10:17:44 +11:00
Nicholas Nethercote
91481e3bcf Overhaul the intravisit::Map trait.
First of all, note that `Map` has three different relevant meanings.
- The `intravisit::Map` trait.
- The `map::Map` struct.
- The `NestedFilter::Map` associated type.

The `intravisit::Map` trait is impl'd twice.
- For `!`, where the methods are all unreachable.
- For `map::Map`, which gets HIR stuff from the `TyCtxt`.

As part of getting rid of `map::Map`, this commit changes `impl
intravisit::Map for map::Map` to `impl intravisit::Map for TyCtxt`. It's
fairly straightforward except various things are renamed, because the
existing names would no longer have made sense.

- `trait intravisit::Map` becomes `trait intravisit::HirTyCtxt`, so named
  because it gets some HIR stuff from a `TyCtxt`.
- `NestedFilter::Map` assoc type becomes `NestedFilter::MaybeTyCtxt`,
  because it's always `!` or `TyCtxt`.
- `Visitor::nested_visit_map` becomes `Visitor::maybe_tcx`.

I deliberately made the new trait and associated type names different to
avoid the old `type Map: Map` situation, which I found confusing. We now
have `type MaybeTyCtxt: HirTyCtxt`.
2025-02-17 13:21:35 +11:00
Nicholas Nethercote
8cf9eea5b3 Move some Map methods onto TyCtxt.
The end goal is to eliminate `Map` altogether.

I added a `hir_` prefix to all of them, that seemed simplest. The
exceptions are `module_items` which became `hir_module_free_items` because
there was already a `hir_module_items`, and `items` which became
`hir_free_items` for consistency with `hir_module_free_items`.
2025-02-17 13:21:02 +11:00
Samuel Tardieu
e0a62acc88 Add clippy_utils::is_mutable() 2025-02-15 15:56:41 +01:00
yanglsh
c47746ca67 fix: needless_option_as_deref FP in trait 2025-02-13 15:26:11 +08: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
Samuel Tardieu
8573725a03 Remove ignored #[must_use] attributes from Clippy
The `#[must_use]` attribute has no effect when applied to methods in
trait implementations.
2025-02-12 15:57:30 +01:00
jonathan
8b6de49ef7 New lint: unbuffered_bytes 2025-02-12 14:57:07 +01: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
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
Bastian Kersting
91ff190391 Rename rustc_middle::Ty::is_unsafe_ptr to is_raw_ptr
The wording unsafe pointer is less common and not mentioned in a lot of
places, instead this is usually called a "raw pointer". For the sake of
uniformity, we rename this method.
This came up during the review of
https://github.com/rust-lang/rust/pull/134424.
2025-02-10 12:49:18 +00: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
Jubilee Young
b18bb0a483 clippy: directly use rustc_abi instead of reexports 2025-02-07 11:21:53 -08: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
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
Matthias Krüger
b43dc00f17 Rollup merge of #136645 - flip1995:clippy-subtree-update, r=Manishearth
Clippy subtree update

r? `@Manishearth`
2025-02-06 21:56:28 +01: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
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
5aa9d7ae7f Merge commit '3e3715c31236bff56f1c63a1de2c7bbdfcfb0923' into clippy-subtree-update 2025-02-06 15:00:24 +01: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
bors
d2497ac87e Auto merge of #136471 - safinaskar:parallel, r=SparrowLii
tree-wide: parallel: Fully removed all `Lrc`, replaced with `Arc`

tree-wide: parallel: Fully removed all `Lrc`, replaced with `Arc`

This is continuation of https://github.com/rust-lang/rust/pull/132282 .

I'm pretty sure I did everything right. In particular, I searched all occurrences of `Lrc` in submodules and made sure that they don't need replacement.

There are other possibilities, through.

We can define `enum Lrc<T> { Rc(Rc<T>), Arc(Arc<T>) }`. Or we can make `Lrc` a union and on every clone we can read from special thread-local variable. Or we can add a generic parameter to `Lrc` and, yes, this parameter will be everywhere across all codebase.

So, if you think we should take some alternative approach, then don't merge this PR. But if it is decided to stick with `Arc`, then, please, merge.

cc "Parallel Rustc Front-end" ( https://github.com/rust-lang/rust/issues/113349 )

r? SparrowLii

`@rustbot` label WG-compiler-parallel
2025-02-06 10:50:05 +00: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
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
J-ZhengLi
2a4be5365a move expr_requires_coercion to clippy_utils & some other adjustments 2025-02-05 10:26:17 +08:00