Commit Graph

9598 Commits

Author SHA1 Message Date
bors ffe95252bd Auto merge of #10954 - y21:issue10158, r=llogiq
[`derivable_impls`]: don't lint if `default()` call expr unsize-coerces to trait object

Fixes #10158.

This fixes a FP where the derive-generated Default impl would have different behavior because of unsize coercion from `Box<T>` to `Box<dyn Trait>`:
```rs
struct S {
  x: Box<dyn std::fmt::Debug>
}
impl Default for S {
  fn default() -> Self {
    Self {
      x: Box::<()>::default()
     // ^~ Box<()> coerces to Box<dyn Debug>
     // #[derive(Default)] would call Box::<dyn Debug>::default()
    }
  }
}
```
(this intentionally only looks for trait objects `dyn` specifically, and not any unsize coercion, e.g. `&[i32; 5]` to `&[i32]`, because that breaks existing tests and isn't actually problematic, as far as I can tell)

changelog: [`derivable_impls`]: don't lint if `default()` call expression unsize-coerces to trait object
2023-06-14 16:59:26 +00:00
y21 4795c91939 fix internal lints 2023-06-14 17:15:57 +02:00
y21 fcb9a382f7 dogfood 2023-06-14 17:10:26 +02:00
y21 5b6ba204a7 fmt 2023-06-14 17:01:57 +02:00
y21 2ba1926955 [derivable_impls]: don't lint if expr unsize-coerces 2023-06-14 16:52:02 +02:00
bors 1d0d686f10 Auto merge of #10950 - KisaragiEffective:ignore_main_in_notest_doc_block, r=xFrednet
[`needless_doctest_main`]: ignore `main()` in `no_test` code fences

close #10491

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

changelog: [`needless_doctest_main`]: ignore `main()` in `no_test` code fence
2023-06-14 10:09:57 +00:00
bors a59236ffb4 Auto merge of #10919 - y21:issue10579, r=blyxyas,xFrednet
[`map_unwrap_or`]: don't lint when referenced variable is moved

Fixes #10579.

The previous way of checking if changing `map(f).unwrap_or(a)` to `map_or(a, f)` is safe had a flaw when the argument to `unwrap_or` moves a binding and the `map` closure references that binding in some way.

It used to simply check if any of the identifiers in the `unwrap_or` argument are referenced in the `map` closure, but it didn't consider the case where the moved binding is referred to through references, for example:
```rs
let x = vec![1, 2];
let x_ref = &x;
Some(()).map(|_| x_ref.clone()).unwrap_or(x);
```
This compiles as is, but we cannot change it to `map_or`. This lint however did suggest changing it, because the simple way of checking if `x` is referenced anywhere in the `map` closure fails here. The safest thing to do here imo (what this PR does) is check if the moved value `x` is referenced *anywhere* in the body (before the `unwrap_or` call). One can always create a reference to the value and smuggle them into the closure, without actually referring to `x`. The original, linked issue shows another one such example:
```rs
    let x = vec![1,2,3,0];
    let y = x.strip_suffix(&[0]).map(|s| s.to_vec()).unwrap_or(x);
```
`x.strip_suffix(&[0])` creates a reference to `x` that is available through `s` inside of the `map` closure, so we can't change it to `map_or`.

changelog: [`map_unwrap_or`]: don't lint when referenced variable is moved
2023-06-14 09:48:22 +00:00
bors b9b453748d Auto merge of #10945 - Centri3:no_effect, r=llogiq
[`no_effect`]: Suggest adding `return` if applicable

Closes #10941

Unfortunately doesn't catch anything complex as `no_effect` already wouldn't, but I'm fine with that (it catches `ControlFlow` at least :D)

changelog: [`no_effect`]: Suggest adding `return` if statement has same type as function's return type and is the last statement in a block
2023-06-14 06:39:44 +00:00
Kisaragi Marine 062b209904 Ignore main() in no_test code fences 2023-06-14 12:31:49 +09:00
Centri3 74a0c9c62f [needless_lifetimes]' suggestion now points at the lifetimes 2023-06-13 14:05:22 -05:00
Centri3 d255e7a53f [no_effect]: suggest adding return if applicable
make cargo test pass

Revert "Make it `Unspecified`"

This reverts commit 774863041c1878ab7fb6e27c1c61c04de0e25bc8.

Make it `Unspecified`
2023-06-13 13:23:33 -05:00
bors 72332b2598 Auto merge of #10944 - GuillaumeGomez:cleanup-needless-pass-by-value, r=xFrednet
Remove dead code in `needless_pass_by_value`

The `spans_need_deref` is never used so I removed it alongside all linked code as well.

changelog: none
2023-06-13 15:58:10 +00:00
Guillaume Gomez bcaf655b70 Remove dead code in needless_pass_by_value 2023-06-13 17:08:51 +02:00
blyxyas d5b2f11340 Now cargo collect-metadata updates the CHANGELOG.md 2023-06-13 16:52:18 +02:00
bors 8a1f0cd765 Auto merge of #10935 - Alexendoo:needless-if-cases, r=Manishearth
Don't lint non-statement/faux empty `needless_if`s

Also has a basic fall-back for `if` statements that have attributes applied to them and incorporates https://github.com/rust-lang/rust-clippy/pull/10921#pullrequestreview-1474008780 while I was there

r? `@Manishearth`

changelog: none
2023-06-13 04:34:37 +00:00
bors 7c2bf28365 Auto merge of #10358 - pksunkara:unnecessary-unwrap, r=llogiq
Add `unnecessary_literal_unwrap` lint

Add lint for more unnecessary unwraps and suggest fixes for them.

Fixes #10352

- [x] Followed [lint naming conventions][lint_naming]
- [x] Added passing UI tests (including committed `.stderr` file)
- [x] `cargo test` passes locally
- [x] Executed `cargo dev update_lints`
- [x] Added lint documentation
- [x] Run `cargo dev fmt`

r? `@llogiq`

---

changelog: New lint [`unnecessary_literal_unwrap`]
[#10358](https://github.com/rust-lang/rust-clippy/pull/10358)
<!-- changelog_checked -->
2023-06-12 18:04:22 +00:00
bors da56c3502a Auto merge of #10933 - y21:issue2262-followup, r=Centri3
[`useless_vec`]: lint on `vec![_]` invocations that adjust to a slice

Fixes #2262 (well, actually my PR over at #10901 did do most of the stuff, but this PR implements the one last other case mentioned in the comments that my PR didn't fix)

Before this change, it would lint `(&vec![1]).iter().sum::<i32>()`, but not `vec![1].iter().sum::<i32>()`. This PR handles this case.
This also refactors a few things that I wanted to do in my other PR but forgot about.

changelog: [`useless_vec`]: lint on `vec![_]` invocations that adjust to a slice
2023-06-12 16:25:59 +00:00
Pavan Kumar Sunkara 6e4c5561be Preserve type annotations when present 2023-06-12 16:19:26 +01:00
Pavan Kumar Sunkara 6e4dc93e24 Support suggesting panics 2023-06-12 16:19:26 +01:00
Pavan Kumar Sunkara 69af0e13b2 Recognize Err 2023-06-12 16:19:26 +01:00
rsdy 6e0e09c8f7 Track init and unwrap of expr 2023-06-12 16:19:26 +01:00
Pavan Kumar Sunkara 7ed7283e0f Recognize unwrap_or_else method 2023-06-12 16:19:26 +01:00
Pavan Kumar Sunkara 8f83502989 Recognize unwrap_or methods 2023-06-12 16:19:26 +01:00
rsdy 1d159e7d11 Recognize Ok 2023-06-12 16:19:26 +01:00
Pavan Kumar Sunkara daf6197481 Implement the suggestion 2023-06-12 16:19:26 +01:00
Pavan Kumar Sunkara 21b88ce290 Implement the lint for expect 2023-06-12 16:19:26 +01:00
Pavan Kumar Sunkara 0b1bb5fbf3 Implement the lint 2023-06-12 16:19:26 +01:00
Pavan Kumar Sunkara 2902359b3c Add the lint to the lib 2023-06-12 16:19:26 +01:00
rsdy 5bb768177e Add hook 2023-06-12 16:19:26 +01:00
Pavan Kumar Sunkara e707447a86 Boilerplate for the new lint 2023-06-12 16:19:26 +01:00
Alex Macleod 5e20a572ee Don't lint non-statement/faux empty needless_ifs 2023-06-12 15:19:00 +00:00
y21 b309875fd8 [useless_vec]: detect unnecessary vec![_] 2023-06-12 15:48:04 +02:00
bors b095247ab6 Auto merge of #10911 - lochetti:fix_9657, r=Alexendoo
Don't linting `as_conversions` in proc macros

Don't linting `as_conversions` if code was generated by procedural macro.

This PR fixes https://github.com/rust-lang/rust-clippy/issues/9657

I implemented the fix changing the lint code to be a `LateLintPass` in order to be able to use the `is_from_proc_macro` out of the box. If the reviwer thinks that it would be better to do the other way (implementing `WithSearchPat`) just let me know. I might need some help in implementing it for the `ustc_ast::ast::Expr`

changelog: [`as_conversions`] avoiding warnings in macro-generated code
2023-06-12 12:28:36 +00:00
y21 de7d43bc61 make lint description easier to read, prevent ICE 2023-06-12 12:21:38 +02:00
y21 7aa4babb5c rename "drawbacks" to "known issues" 2023-06-12 12:21:38 +02:00
y21 6b232fdee9 use span_lint_and_note 2023-06-12 12:21:38 +02:00
y21 52c6ef77fd fmt 2023-06-12 12:21:38 +02:00
y21 760f91f895 cargo dev update_lints 2023-06-12 12:21:38 +02:00
y21 7312a93a06 new lint: large_stack_frames 2023-06-12 12:21:38 +02:00
Centri3 29c1c6e104 refactor and add link to issue 2023-06-12 03:22:01 -05:00
Centri3 95d1bff225 add to tests and configuration 2023-06-12 03:22:01 -05:00
Centri3 243943ff56 make it work for locals as well
oopos
2023-06-12 03:22:01 -05:00
Centri3 7cdd87ca4a ignore generics and allow arbitrary threshold 2023-06-12 03:22:01 -05:00
Centri3 03c8db048e make cargo test pass + example 2023-06-12 03:21:43 -05:00
Centri3 e2ecb132a5 rename the lint 2023-06-12 03:21:43 -05:00
Centri3 52cfc997af Add lint single_letter_idents 2023-06-12 03:21:43 -05:00
bors 903fe3b9f2 Auto merge of #10894 - Centri3:type_repetition_in_bounds, r=blyxyas,xFrednet
[`type_repetition_in_bounds`]: Don't lint on derived code

fixes #10504.

changelog: [`type_repetition_in_bounds`]: Don't lint on derived code
2023-06-12 07:18:39 +00:00
bors 841f2199e0 Auto merge of #10416 - Jarcho:explicit_iter_loop_ext, r=Manishearth
Extend `explicit_iter_loop` and `explicit_into_iter_loop`

fixes #1518

Some included cleanups
* Split `for_loop` test into different files for each lint (partially).
* Move handling of some `into_iter` cases from `explicit_into_iter`.

---

changelog: Enhancement: [`explicit_iter_loop`]: Now also handles types that implement `IntoIterator`.
[#10416](https://github.com/rust-lang/rust-clippy/pull/10416)

changelog: Sugg: [`explicit_into_iter_loop`]: The suggestion now works on mutable references.
[#10416](https://github.com/rust-lang/rust-clippy/pull/10416)
<!-- changelog_checked -->
2023-06-12 05:30:32 +00:00
bors 21e6235b4c Auto merge of #10921 - Centri3:needless_if, r=blyxyas,Manishearth
Add `needless_if` lint

first off: Sorry about the large diff. Seems a ton of tests do this (understandably so).

this is basically everything I wanted in #10868, while it doesn't lint *all* unnecessary empty blocks, it lints needless if statements; which are basically the crux of the issue (for me) anyway. I've committed code that includes this far too many times 😅 hopefully clippy can help me out soon

closes #10868

changelog: New lint [`needless_if`]
2023-06-12 04:18:50 +00:00
Centri3 108c04acf0 Stop visiting once it's found Let 2023-06-11 09:43:26 -05:00