Commit Graph

6253 Commits

Author SHA1 Message Date
bors
b97eaab558 Auto merge of #11387 - y21:issue11371, r=blyxyas
[`unnecessary_unwrap`]: lint on `.as_ref().unwrap()`

Closes #11371

This turned out to be a little more code than I originally thought, because the lint also makes sure to not lint if the user tries to mutate the option:
```rs
if option.is_some() {
  option = None;
  option.unwrap(); // don't lint here
}
```
... which means that even if we taught this lint to recognize `.as_mut()`, it would *still* not lint because that would count as a mutation. So we need to allow `.as_mut()` calls but reject other kinds of mutations.
Unfortunately it doesn't look like this is possible with `is_potentially_mutated` (seeing what kind of mutation happened).
This replaces it with a custom little visitor that does basically what it did before, but also allows `.as_mut()`.

changelog: [`unnecessary_unwrap`]: lint on `.as_ref().unwrap()`
2023-08-28 20:29:42 +00:00
bors
5cc5f27899 Auto merge of #11385 - markhuang1212:master, r=blyxyas
skip float_cmp check if lhs is a custom type

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

changelog: [`float_cmp`]: allow float eq comparison when lhs is a custom type that implements PartialEq<f32/f64>

If the lhs of a comparison is not float, it means there is a user implemented PartialEq, and the caller is invoking that custom version of `==`, instead of the default floating point equal comparison.

People may wrap f32 with a struct (say `MyF32`) and implement its PartialEq that will do the `is_close()` check, so that `MyF32` can be compared with either f32 or `MyF32`.
2023-08-28 18:27:53 +00:00
bors
4118738998 Auto merge of #11401 - y21:issue11394, r=xFrednet
[`if_then_some_else_none`]: look into local initializers for early returns

Fixes #11394

As the PR title says, problem was that it only looked for early returns in semi statements. Local variables don't count as such, so it didn't count `let _v = x?;` (or even just `let _ = return;`) as a possible early return and didn't realize that it can't lint then.

Imo the `stmts_contains_early_return` function that was used before is redundant. `contains_return` could already do that if we just made the parameter a bit more generic, just like `for_each_expr`, which can already accept `&[Stmt]`

changelog: [`if_then_some_else_none`]: look into local initializers for early returns
2023-08-28 08:48:35 +00:00
bors
74a46a74e4 Auto merge of #11391 - mojave2:fix-uitest-enum_clike_unportable_variant, r=Alexendoo
fix the uitest `enum_clike_unportable_variant`

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

changelog: none
2023-08-26 11:01:10 +00:00
bors
4736908ff7 Auto merge of #11404 - mojave2:issue-11368, r=matthiaskrgr
fix "derivable_impls: attributes are ignored"

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

changelog: [`derivable_impls`]: allow the lint when the trait-impl methods has any attribute.
2023-08-26 10:19:21 +00:00
mojave2
d2f6522d78
update uitest enum_clike_unportable_variant
update an uitest stderr

update uitest enum_clike_unportable_variant
2023-08-26 09:34:38 +08:00
y21
f80c55deb5 add a test for statics and doc comments 2023-08-26 01:10:32 +02:00
Meng Huang
e43c234168
allow float_cmp when lhs is a custom type 2023-08-25 19:48:25 +08:00
mojave2
90fcc67d32
fix "derivable_impls: attributes are ignored" 2023-08-25 19:15:44 +08:00
bors
706c48b62a Auto merge of #11395 - c410-f3r:let-chain, r=Manishearth
[`arithmetic_side_effects`] Fix #11393

Fix #11393

```
changelog: [`arithmetic_side_effects`]: Detect division by zero for `Wrapping` and `Saturating`
```
2023-08-25 01:05:38 +00:00
bors
19eaafb920 Auto merge of #11338 - y21:issue11337, r=Centri3
allow trait alias DefIds in `implements_trait_with_env_from_iter`

Fixes #11337

changelog: none
2023-08-24 22:28:29 +00:00
y21
dba7763128 [if_then_some_else_none]: look into local initializers 2023-08-24 23:44:17 +02:00
bors
d65c4595ee Auto merge of #11360 - lengyijun:any_all, r=blyxyas
[`iter_overeager_cloned`]: detect .cloned().all() and .cloned().any()

changelog: [`iter_overeager_cloned`]

r? `@blyxyas`
2023-08-24 19:54:05 +00:00
Caio
2faa43c8b4 [arithmetic_side_effects] Fix #11393 2023-08-24 13:22:27 -03:00
lengyijun
fb6fad20c8 [iter_overeager_cloned]: detect .cloned().all() and .cloned().any() 2023-08-24 08:44:25 +08:00
y21
42c6492ebc [unnecessary_unwrap]: lint on .as_ref().unwrap() 2023-08-23 21:02:01 +02:00
y21
12275713d5 support inherent impls and trait impls 2023-08-23 17:06:55 +02:00
y21
09506f49c1 rename lint, docs, improve diagnostics 2023-08-23 17:06:55 +02:00
y21
2ebff58969 make generics work
fix compile error in doc example
2023-08-23 17:05:55 +02:00
y21
42bd6d7af3 new lint: implied_bounds_in_impl 2023-08-23 17:05:55 +02:00
bors
4932d05733 Auto merge of #11373 - Red-Rapious:master, r=blyxyas,y21
Added new lint: `reserve_after_initialization`

Closes https://github.com/rust-lang/rust-clippy/issues/11330.

A new lint that informs the user about a more concise way to create a vector with a known capacity.
Example:
```rust
let mut v: Vec<usize> = vec![];
v.reserve(10);
```

Produces the following help:
```rust
  |
2 | /     let mut v: Vec<usize> = vec![];
3 | |     v.reserve(10);
  | |__________________^ help: consider using `Vec::with_capacity(space_hint)`: `let v: Vec<usize> = Vec::with_capacity(10);`
  |
```

And can be rewritten as:
```rust
let v: Vec<usize> = Vec::with_capacity(10);
```

changelog: new lint [`reserve_after_initialization`]
2023-08-23 12:06:41 +00:00
bors
edfee16ade Auto merge of #11379 - popzxc:fix-tuple-array-conversions, r=xFrednet
Fix tuple_array_conversions lint on nightly

```
changelog: ICE: [`tuple_array_conversions`]: Don't expect array length to always be usize
```

tl;dr: changed [`Const::eval_target_usize`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_middle/src/ty/consts.rs#L359) to [`Consts::try_eval_target_usize`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_middle/src/ty/consts.rs#L327) to get rid of ICE.

I have encountered a problem with clippy: it caught ICE when working with a codebase that uses a lot of nightly features.
Here's a (stripped) ICE info:

```
error: internal compiler error: /rustc/5c6a7e71cd66705c31c9af94077901a220f0870c/compiler/rustc_middle/src/ty/consts.rs:361:32: expected usize, got Const { ty: usize, kind: N/#1 }

thread 'rustc' panicked at /rustc/5c6a7e71cd66705c31c9af94077901a220f0870c/compiler/rustc_errors/src/lib.rs:1635:9:
Box<dyn Any>
stack backtrace:
...
  16:        0x110b9c590 - rustc_middle[449edf845976488d]::util:🐛:bug_fmt
  17:        0x102f76ae0 - clippy_lints[71754038dd04c2d2]::tuple_array_conversions::all_bindings_are_for_conv
...
```

I don't really know what's going on low-level-wise, but seems like this lin assumed that the length of the array can always be treated as `usize`, and *I assume* this doesn't play well with `feat(generic_const_exprs)`.

I wasn't able to build a minimal reproducible example, but locally this fix does resolve the issue.
2023-08-23 07:20:41 +00:00
Igor Aleksanov
f0eaa66263 Add a test for tuple_array_conversion 2023-08-23 09:54:50 +04:00
Red Rapious
7977d209b2 Do not lint inside macros 2023-08-22 19:36:58 +02:00
Red Rapious
df8bb47f17 Improved snippets and added tests 2023-08-22 18:46:16 +02:00
Guillaume Gomez
f4670121d5 Move code comments to prevent having weird clippy fmt issues 2023-08-22 17:18:12 +02:00
Guillaume Gomez
a05d3a4137 Automatic generation of error annotations for ui tests 2023-08-22 17:18:11 +02:00
Guillaume Gomez
3a31c05578 Remove/move comments to prevent weird rustfmt wrapping 2023-08-22 17:17:48 +02:00
Guillaume Gomez
9c96605b20 Manually add annotations for ui tests 2023-08-22 17:14:08 +02:00
Red Rapious
b0bd6219c8 Simplified code and added tests 2023-08-21 23:36:15 +02:00
Red Rapious
7fbf808a50 Added new lint: reserve_after_initialization 2023-08-21 18:50:53 +02:00
bors
fc1152abf6 Auto merge of #11359 - Alexendoo:unwrap-or-default-check-suggestion, r=dswij
Check that the suggested method exists in unwrap_or_default

Fixes #11355

changelog: none
2023-08-20 15:26:33 +00:00
Alex Macleod
8f2d47ea72 Check that the suggested method exists in unwrap_or_default 2023-08-19 20:22:45 +00:00
lengyijun
e440065a0f [iter_overeager_cloned]: detect .cloned().map() and .cloned().for_each()
key idea:
for `f` in `.map(f)` and `.for_each(f)`:
1. `f` must be a closure with one parameter
2. don't lint if mutable paramter in clsure `f`: `|mut x| ...`
3. don't lint if parameter is moved
2023-08-19 21:18:14 +08:00
y21
e52bd6f850 new lint: should_panic_without_expect 2023-08-18 18:57:14 +02:00
bors
1698ce0ba1 Auto merge of #11280 - samueltardieu:issue-11267, r=Centri3
[new_without_default]: include `where` clause in suggestions, make applicable

changelog: [`new_without_default`]: include `where` clause in suggestions
2023-08-18 15:55:40 +00:00
bors
d5298bea7f Auto merge of #11314 - GuillaumeGomez:needless_ref_mut_async_block, r=Centri3
Correctly handle async blocks for NEEDLESS_PASS_BY_REF_MUT

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

The problem was that the `async block`s are popping a closure which we didn't go into, making it miss the mutable access to the variables.

cc `@Centri3`

changelog: none
2023-08-17 15:55:23 +00:00
bors
701e77c87f Auto merge of #11070 - y21:issue11065, r=flip1995
[`useless_conversion`]: only lint on paths to fn items and fix FP in macro

Fixes #11065 (which is actually two issues: an ICE and a false positive)

It now makes sure that the function call path points to a function-like item (and not e.g. a `const` like in the linked issue), so that calling `TyCtxt::fn_sig` later in the lint does not ICE (fixes https://github.com/rust-lang/rust-clippy/issues/11065#issuecomment-1616836099).
It *also* makes sure that the expression is not part of a macro call (fixes https://github.com/rust-lang/rust-clippy/issues/11065#issuecomment-1616919639). ~~I'm not sure if there's a better way to check this other than to walk the parent expr chain and see if any of them are expansions.~~ (edit: it doesn't do this anymore)

changelog: [`useless_conversion`]: fix ICE when call receiver is a non-fn item
changelog: [`useless_conversion`]: don't lint if argument is a macro argument (fixes a FP)

r? `@llogiq` (reviewed #10814, which introduced these issues)
2023-08-17 14:41:46 +00:00
Guillaume Gomez
7e46217362 Add new regression test for needless_pass_by_ref_mut 2023-08-17 14:24:04 +02:00
bors
710db18ccd Auto merge of #11336 - Alexendoo:uitest-backslash, r=flip1995
Use ui_test's Windows path backslash heuristic

changelog: none

Instead of unconditionally replacing `\` with `/` we now use [`Match::PathBackslash`](https://docs.rs/ui_test/latest/ui_test/enum.Match.html#variant.PathBackslash) to only replace backslashes in paths that look like windows paths

`ui-toml` and `ui-cargo` tests still use the old way because they produce verbatim paths on windows in some tests (`\\?\C:\foo\...`) which was finnicky to get the replacement order correct with

Also removes the `ui_test` -> `compiletest` alias and `VarGuard`
2023-08-15 15:22:29 +00:00
J-ZhengLi
aa8995e589 allow calling to_owned with borrowed value for [implicit_clone] 2023-08-15 09:41:15 +08:00
y21
f746e19169 allow trait alias DefIds in implements_trait_with_env_from_iter 2023-08-14 19:10:33 +02:00
Alex Macleod
77d10ac63d Use ui_test's Windows path backslash heuristic 2023-08-14 15:59:00 +00:00
y21
f47165c703 find expansions more efficiently 2023-08-14 16:50:31 +02:00
y21
2820d980cb [useless_conversion]: fix FP in macro and add test 2023-08-14 16:28:04 +02:00
lengyijun
fc061890d6 [iter_overeager_cloned]: detect .cloned().filter() and .cloned().find()
Key idea:
```
// before
iter.cloned().filter(|x| unimplemented!() )
// after
iter.filter(|&x| unimplemented!() ).cloned()

// before
iter.cloned().filter( foo )
// after
iter.filter(|&x| foo(x) ).cloned()
```
2023-08-14 09:13:01 +08:00
unvalley
1eff39ddb6 fix: change msrv to 1.69 for binary heap 2023-08-13 22:52:06 +09:00
unvalley
d5dbee4aa0 feat: update manual_retain to lint binary_heap_retain
refactor: rename variable

chore: reorder

test: update naming for msrv
2023-08-13 17:09:39 +09:00
Samuel Tardieu
f9b22e7b84 [new_without_default]: make the suggestion machine-applicable
Now that generics and lifetimes are output as expected, the lint
should be applicable.
2023-08-11 21:16:56 +02:00
Samuel Tardieu
621e76d252 [new_without_default]: include where clauses in suggestion
Fix #11267
2023-08-11 21:10:18 +02:00