Commit graph

9018 commits

Author SHA1 Message Date
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
Asuna
762a91b40e Simplify negative Option::{is_some_and,is_none_or} 2024-09-30 03:36:41 +08: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
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
5c2f6db289 Fix typo in needless_lifetimes test 2024-09-25 13:52:12 -04:00
Samuel Moelius
877585089e Fix non-lifetime tests 2024-09-25 13:52:12 -04:00
Samuel Moelius
f2495a1777 Fix lifetime tests 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
f01dfed99f Auto merge of #13442 - Alexendoo:no-rustfix-reasons, r=Jarcho
Add reasons for or remove some `//@no-rustfix` annotations

changelog: none
2024-09-25 02:00:26 +00:00
ljgermain
31bbe2cee3 Apply review suggestions 2024-09-24 15:17:52 -04:00
ljgermain
11363c7bf2 Fix failing UI test 2024-09-24 11:22:26 -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
b7ab258a0e Auto merge of #13439 - flip1995:empty-line-fix, r=Alexendoo
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.

r? `@Alexendoo`

This unblocks the sync

changelog: none
2024-09-23 19:17:44 +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
Alex Macleod
6e387c90f9 Add reasons for or remove some //@no-rustfix annotations 2024-09-23 15:39:02 +00:00
Samuel Tardieu
acff511871 Lint comparison to empty slice using PartialEq methods 2024-09-22 21:29:32 +02:00
Samuel Tardieu
cc2f447f07 Check that #[deny(allow_attributes)] do not issue spurious messages 2024-09-22 21:10:16 +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
0e1ded0f8d Auto merge of #13384 - Alexendoo:remove-old-collect-metadata, r=llogiq
Remove unused `collect_metadata` function

Leftover from #13221

changelog: none
2024-09-21 07:22:44 +00:00
Ruairidh Williamson
739ef7bf0d
Add unused_trait_names tests 2024-09-21 00:57:47 +01:00
bors
bb04c68b2e Auto merge of #124895 - obeis:static-mut-hidden-ref, r=compiler-errors
Disallow hidden references to mutable static

Closes #123060

Tracking:
- https://github.com/rust-lang/rust/issues/123758
2024-09-20 17:25:34 +00:00
Lukas Lueg
290a01e448 Initial impl of unnecessary_first_then_check
Fixes #11212
2024-09-19 21:27:39 +02:00
VictorHugoPilled
ae5f857096 fix: Specifying reason in expect(clippy::needless_return) no longer triggers false positive
chore: Moved new tests into needless_return.rs

chore: Ran cargo uibless

Initial commit
2024-09-18 06:10:51 +00:00
Lukas Lueg
c3d58cd5fa Fix if_then_some_else_none sugg missing closure intro
Fixes #13407
2024-09-17 23:19:29 +02:00
bors
903293b199 Auto merge of #13382 - c410-f3r:blah, r=y21
[`missing_panics_doc`] Fix #13381

Fix #13381

Makes `missing_panics_doc` act like other "panicking" lints (`unwrap_used`, `panic`, etc) in constant environments.

changelog: Ignore `missing_panics_doc` in constant environments
2024-09-17 12:34:00 +00:00
Matthias Krüger
e3bb779e1e Rollup merge of #130293 - gurry:130142-lint-level-issue, r=cjgillot
Fix lint levels not getting overridden by attrs on `Stmt` nodes

Fixes #130142. See comments on the issue for context.

r? `@cjgillot`
2024-09-15 16:01:37 +02:00
Ralf Jung
976131f896 stabilize const_mut_refs 2024-09-15 09:51:32 +02:00
Folkert de Vries
b5ea5c23b3 stabilize const_extern_fn 2024-09-14 18:07:06 +02:00
Gurinder Singh
9e697964dc Fix lint levels not getting overridden by attrs on Stmt nodes 2024-09-14 16:12:00 +05:30
y21
15495ebf42 Look at adjusted types instead of fn signature types in ptr_arg 2024-09-13 18:26:29 +02:00
bors
2b7d80b80a Auto merge of #13377 - vHugoObject:master, r=dswij
fix: Fixed incorrect comment form suggestion for too_long_first_doc_paragraph lint

fixes #13309
changelog: none

Comment form is now a variable and a new test for too_long_first_doc_paragraph was added.
2024-09-13 11:55:46 +00:00
Obei Sideg
cd99729478 Update tests for hidden references to mutable static 2024-09-13 14:10:56 +03:00
VictorHugoPilled
d66e9addd6 fix: Fixed incorrect comment form suggestion
chore: Ran cargo dev fmt

chore: Fixed spacing

fix: Fixed spacing for comment suggestion

fix: Added new module level test to too_long_first_doc_paragraph

chore: Ran cargo uibless
2024-09-12 23:33:22 +00:00
Samuel Tardieu
6371b308d8 Make it clearer that the suggestion is an alternative one
`needless_pass_by_value` sometimes suggest marking the concerned type as
`Copy`. Adding a `or` before this suggestion makes it clearer that this
is not the second part of the original suggestion, but an alternative one.
2024-09-12 16:32:56 +02:00
bors
e039e72d85 Auto merge of #13386 - alex-semenyuk:no_lint_on_duplicate_reason, r=xFrednet
Not trigger `duplicated_attributes` on duplicate reasons

As at #13355 we shoudn't  trigger `duplicated_attributes` on duplicate reasons attr
changelog: [`duplicated_attributes`]: not trigger `duplicated_attributes` on duplicate reasons
2024-09-12 10:26:14 +00:00
Alexey Semenyuk
7097830a9b Not trigger duplicated_attributes on duplicate reasons 2024-09-12 11:49:16 +05:00
Caio
0905a7786e Fix #13381 2024-09-11 13:58:05 -03:00
bors
131681b4b9 Auto merge of #13369 - samueltardieu:issue-13361, r=y21
Special-case suggestions for null pointers constness cast

This implements the suggestions from #13361. It fits into the existing `ptr_cast_constness` lint, as this is a specialized version. However,

1. I have not modified the lint MSRV, so the documentation for this lint will still show that it applies only from Rust 1.72.0. This is true in the general case, but the lint for null pointers will trigger even before this version as `null()` and `null_mut()` were already present in Rust 1.0 and there is no reason not to apply this lint. I guess this is only a minor documentation issue that can be ignored.
2. I have not covered the `core::ptr::null::<T>().cast_mut()` (could be made into `core::ptr::null_mut::<T>()`) and `cotr::ptr::null_mut::<T>().cast_const()` (could be made into `core::ptr::null::<T>()`) cases. Should they be covered? If they should, here or in a separate PR?

changelog: [`ptr_cast_constness`]: special-case suggestions for null pointers constness cast

Fix #13361
2024-09-11 15:43:48 +00:00
Alex Macleod
2775dcdd24 Remove unused collect_metadata function 2024-09-11 13:49:04 +00:00
bors
a53614a910 Auto merge of #13368 - oli-obk:bump_ui_test, r=flip1995
Bump ui_test

This should give a bunch of ui improvements when lots of tests are running. Please lmk of any issues with it. Switching to `--quiet` should always avoid any issues, so there's a workaround if anything crops up. Also please check that this does not regress performance for you. It doesn't for me, but that doesn't mean much.

changelog: none
2024-09-10 11:42:02 +00:00
bors
938f8ba20a Auto merge of #13367 - y21:issue13364, r=Manishearth
Visit struct fields recursively in uninit fallback check

This makes the fallback a bit more consistent with the other checks and rustc.

Fixes #13364. When using a generic type as the `Vec` element type like the issue title says, rustc's uninit check fails and our fallback is used, which didn't look at struct fields when it could.

changelog: none
2024-09-09 23:25:46 +00:00