Commit Graph

12009 Commits

Author SHA1 Message Date
GnomedDev
ef1db3f502 Check MethodCall/Call arg count earlier or at all 2024-10-13 11:24:47 +01:00
bors
05f3793392 Auto merge of #13534 - GnomedDev:fix-large-stack-arrays-nested-item, r=flip1995
Fix large_stack_arrays triggering when nesting const items

Fixes #13529.

r? `@flip1995`

changelog: [`large_stack_arrays`]: No longer triggers in static/const context when using nested items
2024-10-11 09:04:46 +00:00
bors
14c05c974c Auto merge of #13533 - blyxyas:fix-implicit_saturating_sub, r=dswij
Fix span issue on `implicit_saturating_sub`

Fixes #13524

changelog: [`implicit_saturating_sub`]: Fix span issue on else blocks
2024-10-11 06:25:08 +00:00
GnomedDev
87b4c1ffb7
Fix large_stack_arrays triggering when nesting const items 2024-10-10 22:01:46 +01:00
blyxyas
140a1275f2 Fix span issue on implicit_saturating_sub
Fixes #13524
2024-10-10 19:10:04 +02:00
bors
8e60f143d8 Auto merge of #13464 - y21:issue13458, r=flip1995
Don't warn on proc macro generated code in `needless_return`

Fixes #13458
Fixes #13457
Fixes #13467
Fixes #13479
Fixes #13481
Fixes #13526
Fixes #13486

The fix is unfortunately a little more convoluted than just simply adding a `is_from_proc_macro`. That check *does*  fix the issue, however it also introduces a bunch of false negatives in the tests, specifically when the returned expression is in a different syntax context, e.g. `return format!(..)`.

The proc macro check builds up a start and end pattern based on the HIR nodes and compares it to a snippet of the span, however that would currently fail for `return format!(..)` because we would have the patterns `("return", <something inside of the format macro>)`, which doesn't compare equal. So we now return an empty string pattern for when it's in a different syntax context.

"Hide whitespace" helps a bit for reviewing the proc macro detection change

changelog: none
2024-10-10 09:17:20 +00:00
bors
1f8f982f22 Auto merge of #13504 - samueltardieu:needless-raw-strings, r=Alexendoo
Check for needless raw strings in `format_args!()` template as well

changelog: [`needless_raw_strings`, `needless_raw_string_hashes`]: check `format_args!()` template as well

Fix #13503
2024-10-09 01:35:08 +00:00
bors
d9c8d976cb Auto merge of #13512 - samueltardieu:issue-13511, r=xFrednet
`infinite_loop`: continuing an outer loop leaves the inner loop

changelog: [`infinite_loop`]: detect a `continue` targeting an outer loop

Fix #13511
2024-10-08 08:49:07 +00:00
Samuel Tardieu
36c31db705 Apply updated needless_raw_strings to Clippy sources 2024-10-07 18:29:25 +02:00
Samuel Tardieu
5e78c15caa raw_strings: handle format template as well 2024-10-07 18:29:24 +02:00
bors
b013e69692 Auto merge of #13496 - y21:issue10619, r=Alexendoo
Show interior mutability chain in `mutable_key_type`

Fixes #10619

Just ran into this myself and I definitely agree it's not very nice to have to manually go through all the types involved to figure out why this happens and to evaluate if this is really a problem (knowing if the field of a struct is something that a hash impl relies on), so this changes the lint to emit notes for each step involved.

changelog: none
2024-10-07 15:32:34 +00:00
Samuel Tardieu
99ce411ed6 infinite_loop: continuing an outer loop leaves the inner loop 2024-10-06 19:19:27 +02:00
bors
1c9e5d0e5c Auto merge of #13513 - samueltardieu:push-zvvzytrovulq, r=y21
Style: do not defensively use `saturating_sub()`

Using `saturating_sub()` here in code which cannot fail brings a false sense of security. If for any reason a logic error was introduced and caused `self.loop_depth` to reach 0 before being decremented, using `saturating_sub(1)` would silently mask the programming error instead of panicking loudly as it should (at least in dev profile).

changelog: none
2024-10-06 17:15:53 +00:00
Samuel Tardieu
af6816c5b6 infinite_loop: do not defensively use saturating_sub()
Using `saturating_sub()` here in code which cannot fail brings a false
sense of security. If for any reason a logic error was introduced and
caused `self.loop_depth` to reach 0 before being decremented, using
`saturating_sub(1)` would silently mask the programming error instead of
panicking loudly as it should (at least in dev profile).
2024-10-06 18:52:24 +02:00
bors
f7aaecf3f5 Auto merge of #13506 - samueltardieu:push-zyqvkwlswvkk, r=xFrednet
unused_result_ok: added in Clippy 1.82.0, not 1.70.0

changelog: none

Fix #13497
2024-10-06 11:33:31 +00:00
bors
2d2e8cc21f Auto merge of #13485 - GnomedDev:reduce-large-threshold, r=xFrednet
Reduce default 'large array' threshold

As-is this threshold is `512kb`, but as #9449 points out this is way too high for most people to consider sensible (why would you want to copy `256kb` of data around on the stack or duplicate it via `const`) and didn't get any discussion when originally added. This PR reduces it the threshold to `1kb`, which is higher than the issue says ("a few cpu words") but helps out for actual codebases.

While reducing this, I found that `large_stack_arrays` was triggering for statically promoted arrays in constants/statics, so I also fixed that up as seen in the difference to [array_size_threshold.stderr](https://github.com/rust-lang/rust-clippy/compare/master...GnomedDev:rust-clippy:reduce-large-threshold?expand=1#diff-4c2a2a855d9ff7777f1d385be0c1bede2a3fc8aaab94837cde27a35235233fc7).

Closes #9449.

changelog: [`large_stack_arrays`]: No longer triggers in `static`/`const` context
changelog: [`large_const_arrays`]: Changed the default of [`array-size-threshold`] from `512kb` to `16kb`
2024-10-06 11:24:28 +00:00
Samuel Tardieu
0596a04bf4 unused_result_ok: added in Clippy 1.82.0, not 1.70.0 2024-10-05 23:50:14 +02:00
Samuel Tardieu
846bd30cfe raw_strings: separate raw string detection from lint logic 2024-10-05 23:17:29 +02:00
bors
753629bb33 Auto merge of #13412 - GnomedDev:regex-comp-in-loop, r=y21
Implement lint for regex::Regex compilation inside a loop

Closes #598.

Seems like a pretty simple one, I'm not sure if I sorted out all the lint plumbing correctly because I was adding it to the existing regex pass, but seems to work. The name is a bit jank and I'm super open to suggestions for changing it.

changelog: [`regex_creation_in_loops`]: Added lint for Regex compilation inside loops.
2024-10-05 18:41:45 +00:00
GnomedDev
196dbcf6d5
Avoid linting for Regexes compiled in items defined in loops 2024-10-05 17:10:21 +01:00
GnomedDev
995eb95000
Fix TRAIT_METHODS being large const array 2024-10-04 19:07:40 +01:00
GnomedDev
4b7c19018d
Fix large_stack_arrays triggering for static/constants 2024-10-04 19:06:39 +01:00
y21
19d1358b31 print cause chain in mutable_key_type 2024-10-03 21:27:05 +00:00
bors
a01975b152 Auto merge of #13493 - y21:trait_duplication_in_bounds_fix, r=Manishearth
Compare trait references in `trait_duplication_in_bounds` correctly

Fixes #13476
Fixes #11067
Fixes #9915
Fixes #9626

Currently, the `trait_duplication_in_bounds` lints has a helper type for a trait reference that can be used for comparison and hashing, represented as `{trait: Res, generic_args: Vec<Res>}`. However, there are a lot of issues with this. For one, a `Res` can't represent e.g. references, slices, or lots of other types, as well as const generics and associated type equality. In those cases, the lint simply ignores them and has no way of checking if they're actually the same.

So, instead of using `Res` for this, use `SpanlessEq` and `SpanlessHash` for comparisons with the trait path for checking if there are duplicates.

However, using `SpanlessEq` as is alone lead to a false negative in the test. `std::clone::Clone` + `foo::Clone` wasn't recognized as a duplicate, because it has different segments. So this also adds a new "mode" to SpanlessEq which compares by final resolution. (I've been wondering if this can't just be the default but it's quite a large scale change as it affects a lot of lints and I haven't yet looked at all uses of it to see if there are lints that really do care about having exactly the same path segments).

Maybe an alternative would be to turn the hir types/consts into middle types/consts and compare them instead but I'm not sure there's really a good way to do that

changelog: none
2024-10-03 21:16:22 +00:00
y21
d6be597fbc Use SpanlessEq for in trait_bounds lints 2024-10-03 15:49:37 +00:00
Philipp Krones
d300cdfcda
Merge remote-tracking branch 'upstream/master' into rustup 2024-10-03 14:47:50 +02:00
bors
398be8c842 Auto merge of #13443 - SpriteOvO:simplify-option-neg-methods, r=xFrednet
Simplify negative `Option::{is_some_and,is_none_or}`

Closes #13436.

Improved based on the existing lint `nonminimal_bool`, since there is already handling of similar methods `Option::{is_some,is_none}` and `Result::{is_ok,is_err}`, and there is a lot of reusable code.

When `is_some_and` or `is_none_or` have a negation, we invert it into another method by removing the Not sign and inverting the expression in the closure.

For the case where the closure block has statements, currently no simplification is implemented. (Should we do it?)

```rust
// Currently will not simplify this
_ = !opt.is_some_and(|x| {
    let complex_block = 100;
    x == complex_block
});
```

changelog: [`nonminimal_bool`]: Simplify negative `Option::{is_some_and,is_none_or}`
2024-10-02 08:54:12 +00:00
Rob Shearman
c88cb08afc Fix mut_mutex_lock when reference not ultimately mutable
When there is are multiple references where one of the references
isn't mutable then this results in a false-positive for
`mut_mutex_lock` as it only checks the mutability of the first
reference level.

Fix this by using `peel_mid_ty_refs_is_mutable` which correctly
determines whether the reference is ultimately mutable and thus
whether `Mutex::get_lock()` can actually be used.

Fixes #9854
2024-10-01 22:19:42 +02:00
y21
f06a46ee4d consider wait() calls in nested bodies 2024-10-01 19:21:30 +02:00
bors
db1bda3df1 Auto merge of #13286 - smoelius:elidable-impl-lifetimes, r=Alexendoo
Extend `needless_lifetimes` to suggest eliding `impl` lifetimes

Example:
```
error: the following explicit lifetimes could be elided: 'a
  --> tests/ui/needless_lifetimes.rs:332:10
   |
LL |     impl<'a> Foo for Baz<'a> {}
   |          ^^              ^^
   |
help: elide the lifetimes
   |
LL -     impl<'a> Foo for Baz<'a> {}
LL +     impl Foo for Baz<'_> {}
```
The main change is in how `impl` lifetime uses are tracked. Previously, a hashmap was created, and lifetimes were removed from the hashmap as their uses were discovered. However, the uses are needed to generate elision suggestions. So, now, uses are added to the hashmap as they are discovered.

The PR is currently organized as six commits, which I think are self-explanatory:
- Extend `needless_lifetimes` to suggest eliding `impl` lifetimes
- Reorder functions _[not strictly necessary, but IMHO, the code is better structured as a result]_
- Fix lifetime tests
- Fix non-lifetime tests
- Fix `clippy_lints` and `clippy_utils`
- Fix typo in `needless_lifetimes` test

r? `@Alexendoo` (I think you are `needless_lifetimes`' primary author? Sorry if I have this wrong.)

---

changelog: Extend `needless_lifetimes` to suggest eliding `impl` lifetimes
2024-10-01 00:41:21 +00:00
Samuel Moelius
54e4761d05 Address review comment
https://github.com/rust-lang/rust-clippy/pull/13286#discussion_r1780038602
2024-09-29 18:21:47 -04:00
Asuna
762a91b40e Simplify negative Option::{is_some_and,is_none_or} 2024-09-30 03:36:41 +08:00
Yuri Astrakhan
f7d5d9d892 Convert &Option<T> to Option<&T> 2024-09-28 19:51:02 -04:00
Samuel Moelius
b3cf448761 Adjust comment 2024-09-28 16:30:31 -04:00
bors
7b566c214e Auto merge of #13336 - nyurik:ref-option-sig, r=llogiq
Suggest `Option<&T>` instead of `&Option<T>`

closes #13054

```rust
// bad code
fn foo(a: &Option<T>) {}
fn bar(&self) -> &Option<T> {}

// Use instead
fn foo(a: Option<&T>) {}
fn bar(&self) -> Option<&T> {}
```

Handles argument types and return types in functions, methods, and closures with explicit types.  Honors `avoid_breaking_exported_api` parameter.

See this great [YouTube video](https://www.youtube.com/watch?v=6c7pZYP_iIE) with the in-depth explanation.

### Open Questions
These are not blocking, and could be done in separate PRs if needed.
* [ ] Should `&Option<Box<T>>` be suggested as `Option<&T>` -- without the box?  Handled by [clippy::borrowed_box](https://rust-lang.github.io/rust-clippy/master/index.html#/borrowed_box)
* [ ] Should `&Option<String>` be suggested as `Option<&str>` -- using de-refed type?

### Possible Future Improvements
These cases might also be good to handle, probably in a separate PR.

```rust
fn lambdas() {
  let x = |a: &Option<String>| {};
  let x = |a: &Option<String>| -> &Option<String> { todo!() };
}

fn mut_ref_to_ref(a: &mut &Option<u8>) {}
```

changelog: [`ref_option`]: Suggest `Option<&T>` instead of `&Option<T>`
2024-09-28 20:16:10 +00:00
Yuri Astrakhan
10e02cf8e3 suggest &str 2024-09-28 13:17:29 -04:00
Yuri Astrakhan
38295a0d8a update docs 2024-09-28 13:01:51 -04:00
Yuri Astrakhan
73a16c10db Suggest Option<&T> instead of &Option<T> 2024-09-28 11:57:34 -04:00
y21
d24a63199a remove method call receiver special casing in unused_async lint 2024-09-27 21:44:19 +00:00
y21
c8725f70e7 add proc macro check to needless_return 2024-09-26 17:44:59 +02:00
Samuel Moelius
51b527266e Fix bug found during review
https://github.com/rust-lang/rust-clippy/pull/13286#issuecomment-2374245772
2024-09-25 17:56:51 -04:00
VictorHugoPilled
bc07027397 fix(clippy_lints/matches): wildcard_in_or_patterns will no longer be triggered for types annotated with #[nonexhaustive] 2024-09-25 13:12:19 -05:00
Samuel Moelius
66f1f544af Fix clippy_lints and clippy_utils 2024-09-25 13:52:12 -04:00
Samuel Moelius
0a11c5c49a Reorder functions 2024-09-25 13:48:15 -04:00
Samuel Moelius
55fcd46f88 Extend needless_lifetimes to suggest eliding impl lifetimes 2024-09-25 13:48:15 -04:00
bors
2a61f59628 Auto merge of #13393 - vHugoObject:master, r=Alexendoo
fix: Specifying reason in expect(clippy::needless_return) no longer triggers false positive

fixes #13366
changelog: none
2024-09-25 14:44:31 +00:00
bors
52b31180b2 Auto merge of #130778 - flip1995:clippy-subtree-update, r=Manishearth
Clippy subtree update

r? `@Manishearth`

Really delayed sync (2 1/2 weeks), because of a `debug_assertion` we hit, and I didn't have the time to investigate earlier.

It would be nice to merge this PR with some priority, as it includes a lot of formatting changes due to the rustfmt bump.

Include Cargo.lock update due to Clippy version bump and ui_test bump in Clippy.
2024-09-25 11:40:41 +00:00
bors
169adfc810 Auto merge of #13453 - samueltardieu:push-mtrklxqnmzzn, r=dswij
Use std_or_core to determine the correct prefix

This is a cleanup commit. It replaces hand-crafted tests by the a call to the `std_or_core()` utility function.

changelog: none
2024-09-25 07:57:41 +00:00
ljgermain
31bbe2cee3 Apply review suggestions 2024-09-24 15:17:52 -04:00
Samuel Tardieu
0791efaaac Use std_or_core to determine the correct prefix
Replace hand-crafted tests by the a call to the `std_or_core()` utility
function.
2024-09-24 19:09:40 +02:00