Commit graph

21786 commits

Author SHA1 Message Date
Eric
b8cd51313e allow needless_option_take to report for more cases
In general, needless_option_take should report whenever
take() is called on a temporary value, not just when the
temporary value is created by as_ref()
2024-12-11 18:53:11 -08:00
Oli Scherer
f865ada9db Require the constness query to only be invoked on things that can have constness 2024-12-11 11:07:02 +00:00
lapla-cogito
1ace535bc2
suggest modified code for if_not_else lint 2024-12-11 18:22:48 +09:00
Jason Newcomb
c2d23ad0df
Detect shadowing in pattern field (#13797)
Fix #13795

changelog: [`shadow_same`]: detect shadowing as a pattern field
2024-12-11 02:17:28 +00:00
Timo
59740a8eb1
Better help message for comparison_chain lint (#13762)
changelog: [`comparison_chain`]: give explicit help message showing a
clear suggestion

Close #13739
2024-12-10 21:11:42 +00:00
Michael Goulet
f495cec548 Remove more traces of anonymous ADTs 2024-12-10 19:50:47 +00:00
Timo
2a28347897
Fix: fixed multipart_suggestion in index_refutable_slice uitest (#13727)
This should address #13099 for the derivable_impls test. As this
combines everything into a single multipart_suggestion, the feedback
message is a little less "targeted" than it was before, but now it
provides a complete`--fix`able suggestion - e.g.:

```
error: this binding can be a slice pattern to avoid indexing
  --> tests/ui-toml/max_suggested_slice_pattern_length/index_refutable_slice.rs:5:17
   |
LL |     if let Some(slice) = slice {
   |                 ^^^^^
   |
note: the lint level is defined here
  --> tests/ui-toml/max_suggested_slice_pattern_length/index_refutable_slice.rs:1:9
   |
LL | #![deny(clippy::index_refutable_slice)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: replace the binding and indexed access with a slice pattern
   |
LL ~     if let Some([_, _, _, _, _, _, _, slice_7, ..]) = slice {
LL |
LL |         // This would usually not be linted but is included now due to the
LL |         // index limit in the config file
LL ~         println!("{}", slice_7);
   |
```

changelog: [index_refutable_slice]: Fixed multipart_suggestions to
provide correct rustfix-able lint
2024-12-10 19:03:59 +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
bors
1857833cdf Auto merge of #134125 - fmease:rollup-u38o3ob, r=fmease
Rollup of 11 pull requests

Successful merges:

 - #133478 (jsondocck: Parse, don't validate commands.)
 - #133967 ([AIX] Pass -bnoipath when adding rust upstream dynamic crates)
 - #133970 ([AIX] Replace sa_sigaction with sa_union.__su_sigaction for AIX)
 - #133980 ([AIX] Remove option "-n" from AIX "ln" command)
 - #134008 (Make `Copy` unsafe to implement for ADTs with `unsafe` fields)
 - #134017 (Don't use `AsyncFnOnce::CallOnceFuture` bounds for signature deduction)
 - #134023 (handle cygwin environment in `install::sanitize_sh`)
 - #134041 (Use SourceMap to load debugger visualizer files)
 - #134065 (Move `write_graphviz_results`)
 - #134106 (Add compiler-maintainers who requested to be on review rotation)
 - #134123 (bootstrap: Forward cargo JSON output to stdout, not stderr)

Failed merges:

 - #134120 (Remove Felix from ping groups and review rotation)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-12-10 13:16:09 +00:00
León Orell Valerian Liehr
ff0a96b7dc Rollup merge of #134008 - jswrenn:unsafe-fields-copy, r=compiler-errors
Make `Copy` unsafe to implement for ADTs with `unsafe` fields

As a rule, the application of `unsafe` to a declaration requires that use-sites of that declaration also entail `unsafe`. For example, a field declared `unsafe` may only be read in the lexical context of an `unsafe` block.

For nearly all safe traits, the safety obligations of fields are explicitly discharged when they are mentioned in method definitions. For example, idiomatically implementing `Clone` (a safe trait) for a type with unsafe fields will require `unsafe` to clone those fields.

Prior to this commit, `Copy` violated this rule. The trait is marked safe, and although it has no explicit methods, its implementation permits reads of `Self`.

This commit resolves this by making `Copy` conditionally safe to implement. It remains safe to implement for ADTs without unsafe fields, but unsafe to implement for ADTs with unsafe fields.

Tracking: #132922

r? ```@compiler-errors```
2024-12-10 13:51:10 +01:00
León Orell Valerian Liehr
6450014c7d Rollup merge of #134010 - RalfJung:promoted-type-error-ice, r=oli-obk
fix ICE on type error in promoted

Fixes https://github.com/rust-lang/rust/issues/133968

Ensure that when we turn a type error into a "this promoted failed to evaluate" error, we do record this as something that may happen even in "infallible" promoteds.
2024-12-10 08:55:59 +01: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
Wilfred Hughes
e767daa45e indexing_slicing: Clarify documentation
Clarify the relationship between indexing_slicing and
out_of_bound_indexing, clarify that this lint is about possible panics
based on runtime values, and fix array example to not trigger the
out_of_bound_indexing lint.
2024-12-09 10:38:53 -08:00
Jason Newcomb
6a3ef93492
remove unnecessary notations (#13801)
fix #13799

changelog: none
2024-12-09 16:51:28 +00:00
Scott Gerring
7d1e0da510 chore: use multipart_suggestions for match_same_arms 2024-12-09 17:46:00 +01:00
lapla-cogito
d5059d8c1d
remove unnecessary notations 2024-12-10 00:00:51 +09:00
Ralf Jung
a9594c1635 fix ICE on type error in promoted 2024-12-09 15:17:26 +01:00
bors
dc713014aa Auto merge of #134052 - matthiaskrgr:rollup-puxwqrk, r=matthiaskrgr
Rollup of 7 pull requests

Successful merges:

 - #133567 (A bunch of cleanups)
 - #133789 (Add doc alias 'then_with' for `then` method on `bool`)
 - #133880 (Expand home_dir docs)
 - #134036 (crash tests: use individual mir opts instead of mir-opt-level where easily possible)
 - #134045 (Fix some triagebot mentions paths)
 - #134046 (Remove ignored tests for hangs w/ new solver)
 - #134050 (Miri subtree update)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-12-09 03:24:24 +00:00
Samuel Tardieu
f51f72b400 Detect shadowing in pattern field 2024-12-08 22:45:01 +01:00
Samuel Tardieu
69411e0e76 Better help message for comparison_chain lint 2024-12-08 20:34:00 +01:00
Matthias Krüger
24ccb158b8 Rollup merge of #133424 - Nadrieril:guard-patterns-parsing, r=fee1-dead
Parse guard patterns

This implements the parsing of [RFC3637 Guard Patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see also [tracking issue](https://github.com/rust-lang/rust/issues/129967)). This PR is extracted from https://github.com/rust-lang/rust/pull/129996 with minor modifications.

cc `@max-niederman`
2024-12-08 17:18:50 +01:00
llogiq
c4aeb32f1d
chore: use multipart_suggestions for manual_async_fn (#13788)
This addresses #13099 for the manual_async_fn test.

changelog: [manual_async_fn]: Updated manual_async_fn to use
multipart_suggestions where appropriate
2024-12-08 16:00:54 +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
Catherine Flores
d1e1aff8c8
Update known problems for default_numeric_fallback (#13794)
The existing known problem is not actually true, and the many false
positives and false negatives should be mentioned.

Allowing the lint on a single statement:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=328de43690af13ed126f7c00c15cd695

changelog: [`default_numeric_fallback`]: update known problems docs
2024-12-08 07:12:15 +00:00
Jack Wrenn
cfee37d358 Make Copy unsafe to implement for ADTs with unsafe fields
As a rule, the application of `unsafe` to a declaration requires that use-sites
of that declaration also require `unsafe`. For example, a field declared
`unsafe` may only be read in the lexical context of an `unsafe` block.

For nearly all safe traits, the safety obligations of fields are explicitly
discharged when they are mentioned in method definitions. For example,
idiomatically implementing `Clone` (a safe trait) for a type with unsafe fields
will require `unsafe` to clone those fields.

Prior to this commit, `Copy` violated this rule. The trait is marked safe, and
although it has no explicit methods, its implementation permits reads of `Self`.

This commit resolves this by making `Copy` conditionally safe to implement. It
remains safe to implement for ADTs without unsafe fields, but unsafe to
implement for ADTs with unsafe fields.

Tracking: #132922
2024-12-07 20:50:00 +00:00
nora
0a99eec958
Update known problems for default_numeric_fallback
The existing known problem is not actually true, and the many false positives and false negatives should be mentioned.
2024-12-07 13:35:05 +01:00
Jason Newcomb
7f866c7cc6
New lint for as *const _ and as *mut _ pointer casts (#13251)
changelog: New lint for as *const _ and as *mut _ pointer casts

EDIT: lets go with the simpler version
2024-12-06 20:50:33 +00:00
Jason Newcomb
5198941bb7
chore: use multipart_suggestions for str_splitn (#13789)
This addresses #13099 for the manual_split_once test, using the
str_splitn lint.

changelog: [str_splitn]: Updated str_splitn to use multipart_suggestions
where appropriate
2024-12-06 19:44:38 +00:00
Soveu
149671192d new lint for as *const _ and as *mut _ casting 2024-12-06 20:44:03 +01:00
bjorn3
63e4979399 Remove all threading through of ErrorGuaranteed from the driver
It was inconsistently done (sometimes even within a single function) and
most of the rest of the compiler uses fatal errors instead, which need
to be caught using catch_with_exit_code anyway. Using fatal errors
instead of ErrorGuaranteed everywhere in the driver simplifies things a
bit.
2024-12-06 18:42:31 +00:00
Scott Gerring
dcc4025405 chore: use multipart_suggestions for str_splitn 2024-12-06 16:04:21 +01:00
Scott Gerring
c963e22f05 chore: use multipart_suggestions for manual_async_fn 2024-12-06 15:40:50 +01:00
Alejandra González
f83a227d39
Fix various typos (#13785)
I ran [Typos](https://github.com/crate-ci/typos) on the project and
fixed all of the easy spelling mistakes! I made sure to avoid UI tests,
since those changes are more challenging to review due to the changes in
`.stderr` files.

```
changelog: Fixed several typos.
```

- \[x] `cargo test` passes locally
- \[x] Run `cargo dev fmt`
2024-12-05 21:56:21 +00:00
BD103
4907940a55 fix: various typos 2024-12-05 11:11:15 -05: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
cyrgani
c8b10ac671 fix the order of emitting ref_as_ptr and borrow_as_ptr 2024-12-05 09:46:57 +01:00
León Orell Valerian Liehr
7cc19d7d1f Rollup merge of #118833 - Urgau:lint_function_pointer_comparisons, r=cjgillot
Add lint against function pointer comparisons

This is kind of a follow-up to https://github.com/rust-lang/rust/pull/117758 where we added a lint against wide pointer comparisons for being ambiguous and unreliable; well function pointer comparisons are also unreliable. We should IMO follow a similar logic and warn people about it.

-----

## `unpredictable_function_pointer_comparisons`

*warn-by-default*

The `unpredictable_function_pointer_comparisons` lint checks comparison of function pointer as the operands.

### Example

```rust
fn foo() {}
let a = foo as fn();

let _ = a == foo;
```

### Explanation

Function pointers comparisons do not produce meaningful result since they are never guaranteed to be unique and could vary between different code generation units. Furthermore different function could have the same address after being merged together.

----

This PR also uplift the very similar `clippy::fn_address_comparisons` lint, which only linted on if one of the operand was an `ty::FnDef` while this PR lints proposes to lint on all `ty::FnPtr` and `ty::FnDef`.

```@rustbot``` labels +I-lang-nominated

~~Edit: Blocked on https://github.com/rust-lang/libs-team/issues/323 being accepted and it's follow-up pr~~
2024-12-05 07:29:53 +01:00
Jason Newcomb
2550530266
Extend precedence for bitmasking and shift (#13743)
Now we can lint for the expressions like `_&_>>_`, `_<<_^_`, etc. And
will suggest to add parentheses like `_&(_>>_)` and `(_<<_)^_`.
I get implementation suggestions from
[https://github.com/rust-lang/rust-clippy/pull/8735#pullrequestreview-954273477](https://github.com/rust-lang/rust-clippy/pull/8735#pullrequestreview-954273477).
changelog: extended [`precedence`] to lint for bit masking and bit
shifting without parentheses
fixes #6632
2024-12-03 15:58:06 +00:00
Jason Newcomb
19426bfdfb
doc_lazy_continuation: Correctly count indent with backslashes (#13742)
changelog: [`doc_lazy_continuation`]: correctly count indent with
backslashes

Fixes #13705
2024-12-03 15:53:46 +00:00
Scott Gerring
e493664e15 index_refutable_slice: use multipart suggestions 2024-12-03 15:29:41 +01:00
Scott Gerring
ec243887d3 let_unit_value: Use multipart suggestions 2024-12-03 15:15:21 +01:00
Michael Howell
d3a7fb140c doc_lazy_continuation: Correctly count indent with backslashes
changelog: [`doc_lazy_continuation`]: correctly count indent with backslashes
2024-12-02 19:05:36 -07: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
Alejandra González
646d72a01d
Correct report_elidable_impl_lifetimes comment following #13752 (#13771)
Tiny change to make `report_elidable_impl_lifetimes`'s doc comment
consistent with #13752. (cc: @samueltardieu)

---

changelog: none
2024-12-02 20:04:47 +00:00
Samuel Moelius
4a342df09b Correct report_elidable_impl_lifetimes comment following #13752 2024-12-02 14:26:31 -05:00
Jason Newcomb
66b15ad853
doc_nested_refdefs: new lint for suspicious list syntax (#13707)
https://github.com/rust-lang/rust/issues/133150

This is more likely to be intended as an intra-doc link than it is to be
intended as a refdef. If a refdef is intended, it does not need to be
nested within a list item.

```markdown
- [`LONG_INTRA_DOC_LINK`]: this
  looks like an intra-doc link,
  but is actually a refdef.
  The first line will seem to
  disappear when rendered as HTML.
```

> - [`LONG_INTRA_DOC_LINK`]: this
>   looks like an intra-doc link,
>   but is actually a refdef.
>   The first line will seem to
>   disappear when rendered as HTML.

changelog: [`doc_nested_refdefs`]: add suspicious lint for link def at
start of list items and block quotes
2024-12-02 18:51:02 +00:00
Manish Goregaokar
df46e4cf13
add targeted help messages to zombie_processes diagnostic (#13760)
Fixes #13748

Diagnostic for that issue with this change:
```
warning: spawned process is not `wait()`ed on in all code paths
   --> x.rs:167:19
    |
167 |     let mut cmd = cmd.unwrap();
    |                   ^^^^^^^^^^^^
    |
note: no `wait()` call exists on the code path to this early return
   --> x.rs:178:47
    |
178 |             std::io::ErrorKind::BrokenPipe => return Some(0),
    |                                               ^^^^^^^^^^^^^^
note: `wait()` call exists, but it is unreachable due to the early return
   --> x.rs:185:10
    |
185 |     Some(cmd.wait().unwrap().code().unwrap()) // <-- wait()!
    |          ^^^
    = help: consider calling `.wait()` in all code paths
    = note: not doing so might leave behind zombie processes
    = note: see https://doc.rust-lang.org/stable/std/process/struct.Child.html#warning
```
Instead of saying "wait() is **never** called", it now says it's not
called by all code paths and points out the early return in particular.

changelog: none
2024-12-02 18:42:42 +00:00
Manish Goregaokar
74dd50c653
Fix lifetimes elision suggestion in where clauses (#13752)
Fix #13749

changelog: [`needless_lifetimes`]: do not suggest using `'_` in `where`
clauses
2024-12-02 18:42:13 +00:00
Urgau
c8d800e288 Drop uplifted clippy::fn_address_comparisons 2024-12-02 18:43:37 +01:00