Commit graph

1844 commits

Author SHA1 Message Date
Samuel Tardieu
8b7cfc75dd Rust 1.81 and later support elision with explicit self types
Commit 9ef6e2199c introduced a check to
ensure that Clippy doesn't consider a lifetime present in an explicit
self type as being the default for an elided output lifetime. For
example, elision did not work in the case like:

```rust
  fn func(self: &Rc<Self>, &str) -> &str { … }
```

Since Rust 1.81.0, the lifetime in the self type is now considered
the default for elision. Elision should then be suggested when
appropriate.
2025-01-13 23:34:19 +01:00
Alejandra González
579571d9cf
New lint: manual_ok_err (#13740)
changelog: [`manual_ok_err`]: new lint

Detect manual implementations of `.ok()` or `.err()`, as in

```rust
let a = match func() {
    Ok(v) => Some(v),
    Err(_) => None,
};
let b = if let Err(v) = func() {
    Some(v)
} else {
    None
};
```

which can be replaced by

```rust
let a = func().ok();
let b = func().err();
```

This pattern was detected in the wild in the Rust reimplementation of
coreutils:
https://github.com/uutils/coreutils/pull/6886#pullrequestreview-2465160137
2025-01-10 19:19:58 +00:00
Samuel Tardieu
4a69d0d4d8 New lint: manual_ok_err 2025-01-10 18:26:01 +01:00
Philipp Krones
b5bf09e57a
Merge remote-tracking branch 'upstream/master' into rustup 2025-01-09 18:00:37 +01:00
Matthias Krüger
11f38ade90 Rollup merge of #134989 - max-niederman:guard-patterns-hir, r=oli-obk
Lower Guard Patterns to HIR.

Implements lowering of [guard patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see the [tracking issue](#129967)) to HIR.
2025-01-07 21:39:40 +01:00
Ralf Jung
f416f266b0 turn hir::ItemKind::Fn into a named-field variant 2025-01-04 11:35:31 +01:00
Quentin Santos
d67c00f723 Dogfood double_ended_iterator_last 2025-01-01 22:19:16 +01:00
Quentin Santos
0d213aa09a Revert "Dogfood double_ended_iterator_last"
This reverts commit 09c5d34f98.
2025-01-01 22:16:33 +01:00
Quentin Santos
09c5d34f98 Dogfood double_ended_iterator_last 2025-01-01 19:47:22 +01:00
Max Niederman
54e5116b44 cover guard patterns in clippy lints 2024-12-31 17:59:34 -08:00
Guillaume Gomez
34b1765e65 Move more def paths into clippy_utils::paths 2024-12-29 20:39:43 +01:00
Timo
a8968e5dd8
Make inconsistent_struct_constructor "all fields are shorthand" requirement configurable (#13737)
Fixes #11846.

This PR has three commits:
- The first commit adds an `initializer-suggestions` configuration to
control suggestion applicability when initializers are present. The
following are the options:
  - "none": do not suggest
- "maybe-incorrect": suggest, but do not apply suggestions with `--fix`
  - "machine-applicable": suggest and apply suggestions with `--fix`
- The second commit fixes suggestions to handle field attributes
(problem [noticed by
@samueltardieu](https://github.com/rust-lang/rust-clippy/pull/13737#discussion_r1859261645)).
- The third commit adds `initializer-suggestions = "machine-applicable"`
to Clippy's `clippy.toml` and applies the suggestions. (Nothing seems to
break.)

---

changelog: make `inconsistent_struct_constructor` "all fields are
shorthand" requirement configurable
2024-12-27 22:05:03 +00:00
Timo
553a381764
Remove obsolete comment (#13850)
`is_integer_const()` does the const folding.

changelog: none
2024-12-27 15:04:29 +00:00
Samuel Moelius
8a38bcc390 Make "all fields are shorthand" requirement configurable
Handle field attributes in suggestions

Fix adjacent code

Address review comments

https://github.com/rust-lang/rust-clippy/pull/13737#discussion_r1861352124

Address all review comments but one

This comment is not yet addressed: https://github.com/rust-lang/rust-clippy/pull/13737#discussion_r1874544907

`initializer_suggestions` -> `lint_inconsistent_struct_field_initializers`
2024-12-26 19:36:58 -05:00
Philipp Krones
1cc50519d1 Merge commit '609cd310be' into clippy-subtree-update 2024-12-26 15:15:54 +01:00
Philipp Krones
14af404672
Merge remote-tracking branch 'upstream/master' into rustup 2024-12-26 14:46:57 +01:00
bors
b5fe6ec47b Auto merge of #134625 - compiler-errors:unsafe-binders-ty, r=oli-obk
Begin to implement type system layer of unsafe binders

Mostly TODOs, but there's a lot of match arms that are basically just noops so I wanted to split these out before I put up the MIR lowering/projection part of this logic.

r? oli-obk

Tracking:

- https://github.com/rust-lang/rust/issues/130516
2024-12-24 00:51:51 +00:00
Samuel Tardieu
1f8ba334a5 Fix has_iter_method documentation 2024-12-23 00:42:34 +01:00
Michael Goulet
4d735d831e Begin to implement type system layer of unsafe binders 2024-12-22 21:57:57 +00:00
Scott McMurray
c772140a1f Update clippy 2024-12-22 06:12:45 -08:00
Samuel Tardieu
639f40568e Remove obsolete comment
`is_integer_const()` does the const folding.
2024-12-18 00:37:41 +01:00
Jonathan Dönszelmann
4d05825f3e rename rustc_attr to rustc_attr_parsing and create rustc_attr_data_structures 2024-12-16 19:08:19 +01:00
Jonathan Dönszelmann
6f65a813a2 split attributes 2024-12-16 19:08:19 +01:00
llogiq
968669b00a
Do not suggest using Error in no_std before Rust 1.81 (#13834)
changelog: [`result_unit_err`]: do not suggest using `Error` in `no_std`
mode before Rust 1.81

Fix #9767
2024-12-16 05:15:20 +00:00
Alex Macleod
8da8da8428
Initial impl of repr_packed_without_abi (#13398)
Fixes #13375

I've added the lint next to the other attribute-related ones. Not sure
if this is the correct place, since while we are looking after the
`packed`-attribute (there is nothing we can do about types defined
elsewhere), we are more concerned about the type's representation set by
the attribute (instead of "duplicate attributes" and such).

The lint simply looks at the attributes themselves without concern for
the item-kind, since items where `repr` is not allowed end up in a
compile-error anyway.

I'm somewhat concerned about the level of noise this lint would cause
if/when it goes into stable, although it does _not_ come up in
`lintcheck`.

```
changelog: [`repr_packed_without_abi`]: Initial implementation
```
2024-12-16 02:17:59 +00:00
Samuel Tardieu
4c0177cbbc Do not suggest using Error in no_std before Rust 1.81 2024-12-16 00:40:50 +01:00
Lukas Lueg
7a80f7b790 Initial impl repr_packed_without_abi
Fixes #13375
2024-12-15 20:36:47 +01:00
Jonathan Dönszelmann
6dfa37f02a Add hir::Attribute 2024-12-15 19:18:46 +01:00
Oli Scherer
f332026bc7 Rename value field to expr to simplify later commits' diffs 2024-12-15 18:47:45 +01:00
Philipp Krones
12edfb82e5 Merge remote-tracking branch 'upstream/master' into rustup 2024-12-15 16:48:56 +01:00
Stuart Cook
6a92b851f5 Rollup merge of #134285 - oli-obk:push-vwrqsqlwnuxo, r=Urgau
Add some convenience helper methods on `hir::Safety`

Makes a lot of call sites simpler and should make any refactorings needed for https://github.com/rust-lang/rust/pull/134090#issuecomment-2541332415 simpler, as fewer sites have to be touched in case we end up storing some information in the variants of `hir::Safety`
2024-12-15 20:01:38 +11:00
Oli Scherer
acf9177431 Add some convenience helper methods on hir::Safety 2024-12-14 20:31:07 +00:00
Michael Goulet
c9c62c4329 (Re-)Implement impl_trait_in_bindings 2024-12-14 03:21:24 +00:00
llogiq
c607408df5
Fix single_match lint being emitted when it should not (#13765)
We realized when running `clippy --fix` on rustdoc (PR comment
[here](https://github.com/rust-lang/rust/pull/133537/files#r1861377721))
that some comments were removed, which is problematic. This PR checks
that comments outside of `match` arms are taken into account before
emitting the lint.

changelog: Fix `single_match` lint being emitted when it should not
2024-12-13 16:52:31 +00:00
Matthias Krüger
9ce2645780 Rollup merge of #134140 - compiler-errors:unsafe-binders-ast, r=oli-obk
Add AST support for unsafe binders

I'm splitting up #130514 into pieces. It's impossible for me to keep up with a huge PR like that. I'll land type system support for this next, probably w/o MIR lowering, which will come later.

r? `@oli-obk`
cc `@BoxyUwU` and `@lcnr` who also may want to look at this, though this PR doesn't do too much yet
2024-12-13 17:25:31 +01:00
Matthias Krüger
e2a0e387a4 Rollup merge of #133937 - estebank:silence-resolve-errors-from-mod-with-parse-errors, r=davidtwco
Keep track of parse errors in `mod`s and don't emit resolve errors for paths involving them

When we expand a `mod foo;` and parse `foo.rs`, we now track whether that file had an unrecovered parse error that reached the end of the file. If so, we keep that information around in the HIR and mark its `DefId` in the `Resolver`. When resolving a path like `foo::bar`, we do not emit any errors for "`bar` not found in `foo`", as we know that the parse error might have caused `bar` to not be parsed and accounted for.

When this happens in an existing project, every path referencing `foo` would be an irrelevant compile error. Instead, we now skip emitting anything until `foo.rs` is fixed. Tellingly enough, we didn't have any test for errors caused by expansion of `mod`s with parse errors.

Fix https://github.com/rust-lang/rust/issues/97734.
2024-12-13 17:25:28 +01:00
Guillaume Gomez
efe3fe9b8c Fix single_match lint being emitted when it should not 2024-12-13 16:49:09 +01:00
Michael Goulet
87af9d0a0a Fix tools 2024-12-12 16:43:36 +00:00
Michael Goulet
f495cec548 Remove more traces of anonymous ADTs 2024-12-10 19:50:47 +00:00
Esteban Küber
a8d2960935 Keep track of parse errors in mods and don't emit resolve errors for paths involving them
When we expand a `mod foo;` and parse `foo.rs`, we now track whether that file had an unrecovered parse error that reached the end of the file. If so, we keep that information around. When resolving a path like `foo::bar`, we do not emit any errors for "`bar` not found in `foo`", as we know that the parse error might have caused `bar` to not be parsed and accounted for.

When this happens in an existing project, every path referencing `foo` would be an irrelevant compile error. Instead, we now skip emitting anything until `foo.rs` is fixed. Tellingly enough, we didn't have any test for errors caused by `mod` expansion.

Fix #97734.
2024-12-10 18:17:24 +00:00
Esteban Küber
59392bec75 Introduce default_field_values feature
Initial implementation of `#[feature(default_field_values]`, proposed in https://github.com/rust-lang/rfcs/pull/3681.

Support default fields in enum struct variant

Allow default values in an enum struct variant definition:

```rust
pub enum Bar {
    Foo {
        bar: S = S,
        baz: i32 = 42 + 3,
    }
}
```

Allow using `..` without a base on an enum struct variant

```rust
Bar::Foo { .. }
```

`#[derive(Default)]` doesn't account for these as it is still gating `#[default]` only being allowed on unit variants.

Support `#[derive(Default)]` on enum struct variants with all defaulted fields

```rust
pub enum Bar {
    #[default]
    Foo {
        bar: S = S,
        baz: i32 = 42 + 3,
    }
}
```

Check for missing fields in typeck instead of mir_build.

Expand test with `const` param case (needs `generic_const_exprs` enabled).

Properly instantiate MIR const

The following works:

```rust
struct S<A> {
    a: Vec<A> = Vec::new(),
}
S::<i32> { .. }
```

Add lint for default fields that will always fail const-eval

We *allow* this to happen for API writers that might want to rely on users'
getting a compile error when using the default field, different to the error
that they would get when the field isn't default. We could change this to
*always* error instead of being a lint, if we wanted.

This will *not* catch errors for partially evaluated consts, like when the
expression relies on a const parameter.

Suggestions when encountering `Foo { .. }` without `#[feature(default_field_values)]`:

 - Suggest adding a base expression if there are missing fields.
 - Suggest enabling the feature if all the missing fields have optional values.
 - Suggest removing `..` if there are no missing fields.
2024-12-09 21:55:01 +00:00
llogiq
ec37eb6039
Move top-level files to mod.rs when applicable (#13732)
There are two modules in `clippy_utils` that are currently in the form
of:

```
src/
  | ast_utils/
  | ty/
  |
  | ast_utils.rs
  | ty.rs
```

This PR moves the top-level modules to become `mod.rs`, within their
respective folders.

```
src/
  | ast_utils/
  |   | mod.rs
  |
  | ty/
  |   | mod.rs
```

This reduces clutter in the `src` folder, and makes it easier to find
related files in certain editors.[^0] This also appears to be the
standard used in other crates in this repository, though I looked very
briefly.

I do realize that this is a style / opinionated change, so I'll close it
if it receives much push-back. :)

[^0]: I use VSCode, which groups all folders together and all files
separately. This means that `ty.rs` is quite "far" away from the `ty/`
folder, which makes it move difficult to navigate between the two.

```
changelog: none
```

- \[x] `cargo test` passes locally
- \[x] Run `cargo dev fmt`
2024-12-08 10:43:15 +00:00
Jason Newcomb
a5e46a6b08
update borrow_as_ptr to suggest &raw syntax (#13689)
This PR updates the `borrow_as_ptr` lint to no longer suggest `addr_of!`
and `addr_of_mut!` and instead use the preferred `&raw const` and `&raw
mut` syntax.

Not sure about two things:
1. Do I need to set or update a MSRV for the lint anywhere?
2. There is a `borrow_as_ptr_no_std` test as well as a `borrow_as_ptr`
test. They used to be more relevant as the lint needed to select `std`
or `core`, but that is gone now, so maybe the `borrow_as_ptr_no_std`
should be deleted?

changelog: update `borrow_as_ptr` to suggest `&raw` syntax
2024-12-05 10:18:19 +00:00
cyrgani
9925f999f6 update borrow_as_ptr to suggest &raw when the MSRV allows it 2024-12-05 09:48:55 +01:00
Guillaume Gomez
f850d15b99 Rollup merge of #133746 - oli-obk:push-xwyrylxmrtvq, r=jieyouxu
Change `AttrArgs::Eq` to a struct variant

Cleanups for simplifying https://github.com/rust-lang/rust/pull/131808

Basically changes `AttrArgs::Eq` to a struct variant and then avoids several matches on `AttrArgsEq` in favor of methods on it. This will make future refactorings simpler, as they can either keep methods or switch to field accesses without having to restructure code
2024-12-02 23:08:58 +01:00
Guillaume Gomez
a0a948ddaa Rollup merge of #133751 - lcnr:no-trait-solving-on-type, r=compiler-errors
remove `Ty::is_copy_modulo_regions`

Using these functions is likely incorrect if an `InferCtxt` is available, I moved this function to `TyCtxt` (and added it to `LateContext`) and added a note to the documentation that one should prefer `Infer::type_is_copy_modulo_regions` instead.

I didn't yet move `is_sized` and `is_freeze`, though I think we should move these as well.

r? `@compiler-errors` cc #132279
2024-12-02 17:36:11 +01:00
lcnr
b330d9637a remove Ty::is_copy_modulo_regions 2024-12-02 13:57:56 +01:00
Oli Scherer
5082adf9b5 Change AttrArgs::Eq into a struct variant 2024-12-02 10:28:58 +00:00
Jacob Pratt
5e799b25b7 Rollup merge of #133589 - voidc:remove-array-len, r=boxyuwu
Remove `hir::ArrayLen`

This refactoring removes `hir::ArrayLen`, replacing it with `hir::ConstArg`. To represent inferred array lengths (previously `hir::ArrayLen::Infer`), a new variant `ConstArgKind::Infer` is added.

r? `@BoxyUwU`
2024-12-01 22:10:23 -05:00
Dominik Stolz
a6a6936019 Remove hir::ArrayLen, introduce ConstArgKind::Infer
Remove Node::ArrayLenInfer
2024-11-30 21:00:31 +01:00