Commit graph

6656 commits

Author SHA1 Message Date
Guillaume Gomez
6410606815 Update map_clone lint ui test 2024-01-06 17:22:21 +01:00
Guillaume Gomez
7aa4624a8f Extend struct_field_names lint ui test 2024-01-05 16:44:41 +01:00
Guillaume Gomez
bf5c0d8334 Add tests for extension of unconditional_recursion lint on Default trait 2024-01-04 17:40:28 +01:00
bors
0153ca95ae Auto merge of #12062 - GuillaumeGomez:fix-false-positive-unconditional_recursion, r=xFrednet
Fix false positive `unconditional_recursion`

Fixes #12052.

Only checking if both variables are `local` was not enough, we also need to confirm they have the same type as `Self`.

changelog: Fix false positive for `unconditional_recursion` lint
2024-01-03 09:14:58 +00:00
bors
2eb13d3a7c Auto merge of #12056 - PartiallyTyped:12050, r=xFrednet
Fixes: #12050 - `identity_op` correctly suggests a deference for coerced references

When `identity_op` identifies a `no_op`, provides a suggestion, it also checks the type of the type of the variable. If the variable is a reference that's been coerced into a value, e.g.

```
let x = &0i32;
let _ = x + 0;
```

the suggestion will now use a derefence. This is done by identifying whether the variable is a reference to an integral value, and then whether it gets dereferenced.

changelog: false positive: [`identity_op`]: corrected suggestion for reference coerced to value.

fixes: #12050
2024-01-03 09:02:02 +00:00
Yuxiang Qiu
88541d6637
fix some typos 2024-01-02 19:21:51 -05:00
Quinn Sinclair
70024e16c0 New Lint: [thread_local_initializer_can_be_made_const]
Adds a new lint to suggest using `const` on `thread_local!`
initializers that can be evaluated at compile time.

Impl details:

The lint relies on the expansion of `thread_local!`. For non
const-labelled initializers, `thread_local!` produces a function
called `__init` that lazily initializes the value. We check the function
and decide whether the body can be const. The body of the function is
exactly the initializer. If so, we lint the body.

changelog: new lint [`thread_local_initializer_can_be_made_const`]
2024-01-01 18:06:10 +02:00
Florian Brucker
fe35e08e9f 8733: Suggest str.lines when splitting at hard-coded newlines
Adds a new `splitting_strings_at_newlines` lint that suggests to use
`str.lines` instead of splitting a trimmed string at hard-coded
newlines.
2023-12-31 13:30:36 +01:00
Guillaume Gomez
7107aa22b2 Add regression tests for unconditional_recursion 2023-12-31 11:20:49 +01:00
bors
44c99a8089 Auto merge of #11980 - GuillaumeGomez:UNCONDITIONAL_RECURSION-tostring, r=llogiq
Extend UNCONDITIONAL_RECURSION to check for ToString implementations

Follow-up of https://github.com/rust-lang/rust-clippy/pull/11938.

r? `@llogiq`

changelog: Extend `UNCONDITIONAL_RECURSION` to check for `ToString` implementations
2023-12-31 09:12:42 +00:00
bors
7f185bdef6 Auto merge of #12047 - ARandomDev99:12007-empty-enum-variants-with-brackets, r=Jarcho
New Lint: empty_enum_variants_with_brackets

This PR:
- adds a new early pass lint that checks for enum variants with no fields that were defined using brackets. **Category: Restriction**
- adds relevant UI tests for the new lint.

Closes #12007

```
changelog: New lint: [`empty_enum_variants_with_brackets`]
```
2023-12-30 19:01:53 +00:00
bors
0cc53f48f5 Auto merge of #11957 - J-ZhengLi:issue11535, r=Jarcho
don't lint [`default_numeric_fallback`] on return and local assigned macro calls with type stated

fixes: #11535

changelog: don't lint [`default_numeric_fallback`] on return and local assigned macro calls with type stated
2023-12-30 16:47:14 +00:00
bors
c6aeb28a7b Auto merge of #11865 - yuxqiu:map_unwrap_or_default, r=Jarcho
feat: add `manual_is_variant_and` lint

changelog: add a new lint [`manual_is_variant_and`].
- Replace `option.map(f).unwrap_or_default()` and `result.map(f).unwrap_or_default()` with `option.is_some_and(f)` and `result.is_ok_and(f)` where `f` is a function or closure that returns `bool`.
- MSRV is set to 1.70.0 for this lint; when `is_some_and` and `is_ok_and` was stabilised

---

For example, for the following code:

```rust
let opt = Some(0);
opt.map(|x| x > 1).unwrap_or_default();
```

It suggests to instead write:

```rust
let opt = Some(0);
opt.is_some_and(|x| x > 1)
```
2023-12-30 16:37:36 +00:00
bors
b19b5f293e Auto merge of #12008 - J-ZhengLi:issue9872, r=Jarcho
make [`mutex_atomic`] more type aware

fixes: #9872

---

changelog: [`mutex_atomic`] now suggests more specific atomic types and skips mutex i128 and u128
2023-12-30 16:29:13 +00:00
Guillaume Gomez
d161f3b559 Add ui test for UNCONDITIONAL_RECURSION lint on ToString impl 2023-12-30 17:11:56 +01:00
Quinn Sinclair
c2b3f5c767 identity_op correctly suggests a deference for coerced references
When `identity_op` identifies a `no_op`, provides a suggestion, it also
checks the type of the type of the variable. If the variable is
a reference that's been coerced into a value, e.g.

```
let x = &0i32;
let _ = x + 0;
```

the suggestion will now use a derefence. This is done by identifying
whether the variable is a reference to an integral value, and then
whether it gets dereferenced.

changelog: false positive: [`identity_op`]: corrected suggestion for
reference coerced to value.

fixes: #12050
2023-12-30 13:31:32 +02:00
y21
0848e120b2 add expansion checks to iter_without_into_iter and into_iter_without_iter 2023-12-30 04:56:46 +01:00
Aneesh Kadiyala
1ee9993a96 add new lint, empty_enum_variants_with_brackets
- Add a new early pass lint.
- Add relevant UI tests.
2023-12-29 19:23:31 +05:30
Philipp Krones
9ff84af787
Merge remote-tracking branch 'upstream/master' into rustup 2023-12-28 19:20:18 +01:00
Florian Brucker
ebc0588937 6459: Check for redundant matches! with Ready, Pending, V4, V6 2023-12-27 19:10:04 +01:00
bors
c689d32a90 Auto merge of #11981 - y21:eager_int_transmute, r=llogiq
new lint: `eager_transmute`

A small but still hopefully useful lint that looks for patterns such as `(x < 5).then_some(transmute(x))`.
This is almost certainly wrong because it evaluates the transmute eagerly and can lead to surprises such as the check being completely removed and always evaluating to `Some` no matter what `x` is (it is UB after all when the integer is not a valid bitpattern for the transmuted-to type). [Example](https://godbolt.org/z/xoY34fPzh).
The user most likely meant to use `then` instead.

I can't remember where I saw this but this is inspired by a real bug that happened in practice.

This could probably be a correctness lint?

changelog: new lint: [`eager_int_transmute`]
2023-12-27 15:16:46 +00:00
y21
08d8ca9edd new lint: eager_int_transmute 2023-12-27 14:16:35 +01:00
Yuxiang Qiu
c4a80f2e3e
feat: add manual_is_variant_and lint 2023-12-26 17:49:51 -07:00
Bruce Mitchener
f48b850c65 [doc_markdown]: Add "WebGL2", "WebGPU" to default doc_valid_idents 2023-12-26 16:58:43 -05:00
Michael Goulet
2230add36e Rollup merge of #119240 - compiler-errors:lang-item-more, r=petrochenkov
Make some non-diagnostic-affecting `QPath::LangItem` into regular `QPath`s

The rest of 'em affect diagnostics, so leave them alone... for now.

cc #115178
2023-12-26 13:29:13 -05:00
bors
9dd2252b2c Auto merge of #12004 - PartiallyTyped:11843, r=xFrednet
New lints `iter_filter_is_some` and `iter_filter_is_ok`

Adds a pair of lints that check for cases of an iterator over `Result` and `Option` followed by `filter` without being followed by `map` as that is covered already by a different, specialized lint.

Fixes #11843

PS, I also made some minor documentations fixes in a case where a double tick (`) was included.

---

changelog: New Lint: [`iter_filter_is_some`]
[#12004](https://github.com/rust-lang/rust/pull/12004)
changelog: New Lint: [`iter_filter_is_ok`]
[#12004](https://github.com/rust-lang/rust/pull/12004)
2023-12-26 15:41:40 +00:00
Michael Goulet
90a59d4990 Make some non-diagnostic-affecting QPath::LangItem into regular qpaths 2023-12-26 04:07:38 +00:00
Michael Goulet
e0097f5323 Fix clippy's usage of Body's coroutine_kind
Also fixes a bug where we weren't peeling blocks from async bodies
2023-12-25 21:13:41 +00:00
bors
99c783843d Auto merge of #11967 - samueltardieu:issue-11959, r=llogiq
Do not consider `async { (impl IntoFuture).await }` as redundant

changelog: [`redundant_async_block`]: do not trigger on `IntoFuture` instances

Fix #11959
2023-12-25 12:42:26 +00:00
J-ZhengLi
a578b9ba0b make [mutex_atomic] more type aware 2023-12-25 14:28:01 +08:00
J-ZhengLi
a9baf344b8 remove obsolete test comments 2023-12-25 09:39:32 +08:00
Quinn Sinclair
85e1646afc more tests 2023-12-24 17:32:00 +02:00
Quinn Sinclair
4b1ac31c18 Added MSRV and more tests, improved wording 2023-12-24 14:40:14 +02:00
bors
830f1c58f4 Auto merge of #11994 - y21:issue11993-fn, r=xFrednet
[`question_mark`]: also trigger on `return` statements

This fixes the false negative mentioned in #11993: the lint only used to check for `return` expressions, and not a statement containing a `return` expression (doesn't close the issue tho since there's still a useful suggestion that we could make, which is to suggest `.ok_or()?`/`.ok_or_else()?` for `else { return Err(..) }`)

changelog: [`question_mark`]: also trigger on `return` statements
2023-12-23 16:17:35 +00:00
y21
e44caea259 respect comments in question_mark 2023-12-23 16:22:12 +01:00
y21
dfb4ff8bbf [question_mark]: also trigger on return statements 2023-12-23 16:22:08 +01:00
bors
618fd4b494 Auto merge of #11999 - Takashiidobe:fix-typo-in-infinite-loop-lint, r=xFrednet
fix typo in infinite loop lint

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: This fixes a small typo introduced in https://github.com/rust-lang/rust-clippy/pull/11829
2023-12-23 14:46:52 +00:00
bors
858d96d63a Auto merge of #11871 - GuillaumeGomez:UNNECESSARY_TO_OWNED-split, r=llogiq
Extend `UNNECESSARY_TO_OWNED` to handle `split`

Fixes https://github.com/rust-lang/rust-clippy/issues/9965.

When you have `to_string().split('a')` or equivalent, it'll suggest to remove the `to_owned`/`to_string` part.

r? `@flip1995`

changelog: Extend `UNNECESSARY_TO_OWNED` to handle `split`
2023-12-23 11:36:53 +00:00
Takashi Idobe
3a4d99ec42 run cargo uibless 2023-12-22 19:02:22 -05:00
Takashi Idobe
67a33e8cc8 fix typo in infinite loop lint 2023-12-22 18:44:20 -05:00
bors
e0b25c5a64 Auto merge of #11998 - cocodery:fix/issue11762, r=llogiq
Check whether out of bound when access a known length array with a constant index

fixes [Issue#11762](https://github.com/rust-lang/rust-clippy/issues/11762)

Issue#11762 points that `Array references with known length are not flagged when indexed out of bounds`.

To fix this problem, it is needed to add check for `Expr::Index`. We expand this issue include reference and direct accessing a array.

When we access a array with a constant index `off`, and already know the length `size`, if `off >= size`, these code will throw an error, instead rustc's lint checking them or runtime panic happening.

changelog: [`out_of_bound_indexing`]: Add check for illegal accessing known length array with a constant index
2023-12-22 17:01:17 +00:00
cocodery
18eb406776 Add test for indexing_slicing_index and modify related test 2023-12-22 20:31:48 +08:00
J-ZhengLi
a556d00c0a stop [bool_comparison]'s suggestion from consuming parentheses 2023-12-21 18:20:25 +08:00
Quinn Sinclair
25b9ca3f64 New lints iter_filter_is_some and iter_filter_is_ok
Adds a pair of lints that check for cases of an iterator over `Result`
and `Option` followed by `filter` without being followed by `map` as
that is covered already by a different, specialized lint.

changelog: New Lint: [`iter_filter_is_some`]
changelog: New Lint: [`iter_filter_is_ok`]
2023-12-21 00:16:47 +02:00
Guillaume Gomez
238c5f9f27 Add ui tests for UNNECESSARY_TO_OWNED on split 2023-12-18 16:55:42 +01:00
bors
dd857f8207 Auto merge of #11966 - StackOverflowExcept1on:issue-8159, r=Jarcho
Do not lint `assertions_on_constants` for `const _: () = assert!(expr)`

Fixes #8159

```rust
pub fn f() {
    // warning
    assert!(true);
    assert!(usize::BITS >= 32);

    // ok
    const _: () = assert!(usize::BITS >= 32);
}
```

changelog: Fix `const _: () = assert!(expr)` false positive on `assertions_on_constants` lint
2023-12-17 14:03:13 +00:00
bors
f9b5def2ae Auto merge of #11869 - PartiallyTyped:result-filter-map, r=Alexendoo
New Lint: `result_filter_map` / Mirror of `option_filter_map`

Added the `Result` mirror of `option_filter_map`.

changelog: New Lint: [`result_filter_map`]

I had to move around some code because the function def was too long 🙃.

I have also added some pattern checks on `option_filter_map`
2023-12-16 23:29:07 +00:00
bors
fff484d18e Auto merge of #11977 - y21:is_const_evaluatable_ice, r=Manishearth
don't visit nested bodies in `is_const_evaluatable`

Fixes #11939

This ICE happened in `if_let_some_else_none`, but the root problem is in one of the utils that it uses.
It is (was) possible for `is_const_evalutable` to visit nested bodies which would lead to it trying to get the type of one of the expressions with the wrong typeck table, which won't have the type stored.

Notably, for the expression `Bytes::from_static(&[0; 256 * 1024]);` in the linked issue, the array length is an anonymous const in which type checking happens on its own, so we can't use the typeck table of the enclosing function in there.

Visiting nested bodies is also not needed for checking whether an expression can be const, so I think it's safe to ignore just ignore them altogether.

changelog: Fix ICE when checking for constness in nested bodies
2023-12-16 22:54:50 +00:00
y21
b5169aea52 don't visit any nested bodies in is_const_evaluatable 2023-12-16 22:13:54 +01:00
bors
9907b90b1e Auto merge of #11938 - GuillaumeGomez:unconditional_recursion, r=llogiq
Add new `unconditional_recursion` lint

Currently, rustc `unconditional_recursion` doesn't detect cases like:

```rust
enum Foo {
    A,
    B,
}

impl PartialEq for Foo {
    fn eq(&self, other: &Self) -> bool {
        self == other
    }
}
```

This is because the lint is currently implemented only for one level, and in the above code, `self == other` will then call `impl PartialEq for &T`, escaping from the detection. The fix for it seems to be a bit tricky (I started investigating potential solution to add one extra level of recursion [here](https://github.com/rust-lang/rust/compare/master...GuillaumeGomez:rust:trait-impl-recursion?expand=1) but completely broken at the moment).

I expect that this situation will remain for a while. In the meantime, I think it's acceptable to check it directly into clippy for the time being as a lot of easy cases like this one can be easily checked (next I plan to extend it to cover other traits like `ToString`).

changelog: Add new `unconditional_recursion` lint
2023-12-16 18:21:01 +00:00