Commit graph

11975 commits

Author SHA1 Message Date
GnomedDev
4b7c19018d
Fix large_stack_arrays triggering for static/constants 2024-10-04 19:06:39 +01: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
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
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
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
Liam Germain
f203e1ede5
Fix false positive invalid null usage warning for ptr::slice_from_raw_parts_* functions 2024-09-24 09:52:11 -04:00
bors
7901289135 Auto merge of #13432 - samueltardieu:issue-13401, r=Manishearth
Lint comparison to empty slice using `PartialEq` methods

changelog: [`comparison_to_empty`]: Also  detect comparaisons using `PartialEq` methods

Fix #13401
2024-09-23 23:08:04 +00:00
bors
8ab744e2d1 Auto merge of #13431 - GnomedDev:split-def_path_res, r=y21
Split def_path_res into two parts

`def_path_res` previously had two jobs:
1. looking up the crates to find the path in
2. looking up path in said crates

This splits that job up into two functions, keeping `def_path_res` as an adapter between the both, to avoid repeating the first step when repeatedly looking up items in the same crate.

changelog: none
2024-09-23 19:39:12 +00:00
Philipp Krones
7f30dafa9b
Use contiguous spans for empty_line_after_* suggestion
Replacing an empty span (which an empty line is) with an empty string triggers a
debug assertion in rustc. This fixes the debug assertion by using contiguous
spans, with the same resulting suggestion.
2024-09-23 17:56:32 +02:00
GnomedDev
6b34c8df2c
Avoid looking regex crate up multiple times 2024-09-23 09:05:33 +01:00
Samuel Tardieu
acff511871 Lint comparison to empty slice using PartialEq methods 2024-09-22 21:29:32 +02:00
Philipp Krones
3ab1da8bab
Formatting 2024-09-22 20:52:15 +02:00
Philipp Krones
d140e26cc0
Merge remote-tracking branch 'upstream/master' into rustup 2024-09-22 20:51:17 +02:00
bors
abdf173ef2 Auto merge of #13322 - RuairidhWilliamson:anon-trait-import, r=y21
Unused trait imports (formerly anonymous trait import)

For #11969

I'm looking for help and feedback on implementing a new lint for suggesting `use ... as _` for traits where possible.

I have had a go at implementing this but I don't know if this is the best way to do it as I am new to clippy.

There are some edge cases I can think of where this doesn't work but have aired on the side of false negatives instead of false positives.

An example of a false negative. I couldn't figure out the best way to resolve an import from within clippy. The sub module imports MyAny so that cannot be anonymized but `use std::any::Any` could be. In this case it is not caught because `Any` and `MyAny` have the same DefId.
```rust
mod nested_mod_used_bad1 {
    use std::any::Any;
    use std::any::Any as MyAny;
    mod foo {
        use crate::nested_mod_used_bad1::MyAny;
        fn foo() {
            println!("{:?}", MyAny::type_id("foo"));
        }
    }
}
```

Any feedback is much appreciated.

-------
changelog: new lint: `unused_trait_names`
2024-09-22 14:22:40 +00:00
bors
612ae353d7 Auto merge of #13409 - lukaslueg:issue13407, r=dswij
Fix `if_then_some_else_none` sugg missing closure intro

Fixes #13407

#13407 works in current stable. The suggestion-generating code got trampled over in 0532104247 :-)

changelog: [`if_then_some_else_none`]: Fix missing closure in suggestion
2024-09-22 11:59:12 +00:00
bors
2c5e600386 Auto merge of #13392 - alex-semenyuk:doc_type_complexity, r=dswij
Clarify example for `type_complexity`

As mentioned #13387 it's not clear how to fix issue with complexity so add example for this

changelog: none
2024-09-22 11:51:07 +00:00
Ruairidh Williamson
739ef7bf0d
Add unused_trait_names tests 2024-09-21 00:57:47 +01:00
Ruairidh Williamson
05ebce8e1a
Add lint unused_trait_names 2024-09-21 00:56:38 +01:00
GnomedDev
4f319002cd [Clippy] Remove final std paths for diagnostic item 2024-09-20 10:39:31 +01:00
Lukas Lueg
290a01e448 Initial impl of unnecessary_first_then_check
Fixes #11212
2024-09-19 21:27:39 +02:00
GnomedDev
c7453b4280 [Clippy] Swap open_options to use diagnostic items instead of paths 2024-09-19 13:13:43 +01:00
GnomedDev
917775fff1 [Clippy] Swap iter_over_hash_type to use diagnostic items instead of paths 2024-09-19 13:13:43 +01:00
GnomedDev
37e38320b4 [Clippy] Swap non_octal_unix_permissions to use diagnostic item instead of path 2024-09-19 13:13:43 +01:00
GnomedDev
f1fc9c07c4 [Clippy] Swap unnecessary_owned_empty_strings to use diagnostic item instead of path 2024-09-19 13:13:43 +01:00
GnomedDev
1b76ae683c [Clippy] Swap manual_strip to use diagnostic items instead of paths 2024-09-19 13:13:43 +01:00
GnomedDev
acb6748f80 [Clippy] Swap unnecessary_to_owned to use diagnostic item instead of path 2024-09-19 13:13:42 +01:00
GnomedDev
98dc68e85e [Clippy] Swap instant_subtraction to use diagnostic item instead of path 2024-09-19 13:13:42 +01:00
GnomedDev
d63e35ba22 [Clippy] Swap waker_clone_wake to use diagnostic item instead of path 2024-09-19 13:13:42 +01:00
GnomedDev
45c1700e13 [Clippy] Swap filter_map_bool_then to use diagnostic item instead of path 2024-09-19 13:13:42 +01:00
GnomedDev
b0152909d6 [Clippy] Swap manual_while_let_some to use diagnostic items instead of paths 2024-09-19 13:13:42 +01:00
GnomedDev
984bd6fed6 [Clippy] Swap repeat_vec_with_capacity to use diagnostic item instead of path 2024-09-19 13:13:42 +01:00
GnomedDev
545967955a [Clippy] Swap VecArgs::hir to use diagnostic items instead of paths 2024-09-19 13:13:40 +01:00
GnomedDev
f66915e8f8 [Clippy] Swap single_char_add_str/format_push_string to use diagnostic items instead of paths 2024-09-19 13:13:20 +01:00
GnomedDev
959f7a2bbb [Clippy] Swap manual_main_separator_str to use diagnostic item instead of path 2024-09-19 13:13:20 +01:00
GnomedDev
1922a99bc6 [Clippy] Swap redundant_clone to use diagnostic items instead of paths 2024-09-19 13:13:20 +01:00
GnomedDev
8fc9e67cf5 [Clippy] Swap float_equality_without_abs to use diagnostic items instead of paths 2024-09-19 13:13:20 +01:00