Commit Graph

17522 Commits

Author SHA1 Message Date
bors
0b63e95dce Auto merge of #10949 - y21:issue8010, r=Alexendoo
[`manual_filter_map`]: lint on `matches` and pattern matching

Fixes #8010

Previously this lint only worked specifically for a very limited set of methods on the filter call (`.filter(|opt| opt.is_some())` and `.filter(|res| res.is_ok())`). This PR extends it to also recognize `matches!` in the `filter` and pattern matching with `if let` or `match` in the `map`.

Example:
```rs
enum Enum {
  A(i32),
  B,
}

let _ = [Enum::A(123), Enum::B].into_iter()
  .filter(|x| matches!(x, Enum::A(_)))
  .map(|x| if let Enum::A(s) = x { s } else { unreachable!() });
```
Now suggests:
```diff
-  .filter(|x| matches!(x, Enum::A(_))).map(if let Enum::A(s) = x { s } else { unreachable!() })
+  .filter_map(|x| match x { Enum::A(s) => Some(s), _ => None })
```

Adding this required a somewhat large change in code because it originally seemed to be specifically written with only method calls in the filter in mind, and `matches!` has different behavior in the map, so this new setup should make it possible to support more "generic" cases that need different handling for the filter and map calls.

changelog: [`manual_filter_map`]: lint on `matches` and pattern matching (and some internal refactoring)
2023-07-19 12:59:51 +00:00
bors
7a34143fa3 Auto merge of #11135 - smoelius:unwrap_or_else_default-fp, r=Centri3
Fix `unwrap_or_else_default` false positive

This PR fixes a false positive in the handling of `unwrap_or_else` with a default value when the value is needed for type inference.

An easy example to exhibit the false positive is the following:
```rust
    let option = None;
    option.unwrap_or_else(Vec::new).push(1);
```
The following code would not compile, because the fact that the value is a `Vec` has been lost:
```rust
    let option = None;
    option.unwrap_or_default().push(1);
```
The fix is to:
- implement a heuristic to tell whether an expression's type can be determined purely from its subexpressions, and the arguments and locals they use;
- apply the heuristic to `unwrap_or_else`'s receiver.

The heuristic returns false when applied to `option` in the above example, but it returns true when applied to `option` in either of the following examples:
```rust
    let option: Option<Vec<u64>> = None;
    option.unwrap_or_else(Vec::new).push(1);
```
```rust
    let option = None::<Vec<u64>>;
    option.unwrap_or_else(Vec::new).push(1);
```

(Aside: https://github.com/rust-lang/rust-clippy/pull/10120 unfairly contained multiple changes in one PR. I am trying to break that PR up into smaller pieces.)

---

changelog: FP: [`unwrap_or_else_default`]: No longer lints if the default value is needed for type inference
2023-07-19 11:37:30 +00:00
bors
0fa1fd396e Auto merge of #11183 - Alexendoo:new-lint-template, r=xFrednet
Remove `#![allow(unused)]` and `--crate-name` from `cargo dev new_lint` generated tests

changelog: none

Also removes some unused flags from `ui-cargo` tests because the entrypoint is now the `Cargo.toml`, not the `.rs` files
2023-07-19 10:51:11 +00:00
Samuel Moelius
f583fd18e4 Fix unwrap_or_else_default false positive 2023-07-19 06:45:33 -04:00
bors
3dd6af957c Auto merge of #11046 - Centri3:iter_skip_zero, r=blyxyas
New lint [`iter_skip_zero`]

Idea came from my contribution to `unnecessary_cast` recently. Sorry about that 😅

Could be an issue if somebody manually implements `skip`, but I don't think anybody will (and this would be an issue with other lints too, afaik)

changelog: New lint [`iter_skip_zero`]
2023-07-19 09:03:40 +00:00
Catherine
4c79d8ace5 new lint iter_skip_zero 2023-07-19 03:26:15 -05:00
bors
ec571555a8 Auto merge of #11052 - Centri3:string_lit_chars_any, r=Jarcho
New lint [`string_lit_chars_any`]

Closes #10389

This lint can probably be deprecated if/when rustc optimizes `.chars().any(...)`.

changelog: New lint [`string_lit_chars_any`]
2023-07-19 06:58:03 +00:00
Catherine
9d08502496 New lint [string_lit_chars_any] 2023-07-19 00:38:22 -05:00
bors
d4a6634d37 Auto merge of #11175 - y21:issue11174, r=Manishearth
[`redundant_pattern_matching`]: include guard in suggestion

Fixes #11174

changelog: [`redundant_pattern_matching`]: include guard in suggestion
2023-07-18 20:37:30 +00:00
Alex Macleod
f539e1a1db Remove unused flags from ui-cargo tests 2023-07-18 18:46:09 +00:00
Alex Macleod
9c6b17e24d Remove #![allow(unused)] and --crate-name from new_lint tests 2023-07-18 18:45:47 +00:00
bors
9f0cbfd7df Auto merge of #11140 - Centri3:four_forward_slashes, r=blyxyas
New lint [`four_forward_slashes`]

Closes #9212

changelog: New lint [`four_forward_slashes`]
2023-07-18 12:56:18 +00:00
bors
747df85f95 Auto merge of #11171 - Centri3:tuple_array_conversions, r=llogiq
Rewrite [`tuple_array_conversions`]

Fixes #11100
Fixes #11144
Fixes #11124

#11082 still needs discussion and #11085 likely can't be fixed.

changelog: [`tuple_array_conversions`]: Move to `pedantic`
changelog: [`tuple_array_conversions`]: Don't lint if mutability of references changes
changelog: [`tuple_array_conversions`]: Don't lint if bindings don't come from the exact same pattern
changelog: [`tuple_array_conversions`]: Don't lint if bindings are used for more than just the conversion
2023-07-18 05:15:50 +00:00
y21
e7fd44f213 add guard to suggestion instead of not linting 2023-07-17 21:18:11 +02:00
y21
c26801ee92 [redundant_pattern_matching]: don't lint if if guards are present 2023-07-17 19:25:28 +02:00
bors
3e0170bff4 Auto merge of #11173 - Alexendoo:needless-return-fn-macro, r=Manishearth
Don't lint `needless_return` in fns across a macro boundary

Fixes #11167

changelog: none
2023-07-17 15:53:21 +00:00
bors
410456da51 Auto merge of #11116 - y21:format_collect, r=Manishearth
new lint: `format_collect`

A perf lint that looks for `format!`ing inside of `map`, then collecting it into a `String`. Did a quick benchmark locally and it's a bit more than 2x faster with fold.
`write!` is still not optimal (presumably because the fmt stuff goes through dynamic dispatch), but it's still a lot better than creating a new string on every element.
I thought about making a machine applicable suggestion, but there's a lot of suggestions that need to be made here, so I decided to just add help messages.

changelog: new lint: `format_collect`
2023-07-17 15:38:33 +00:00
Alex Macleod
d24f0d056f Don't lint needless_return in fns across a macro boundary 2023-07-17 14:03:00 +00:00
bors
54fa92287b Auto merge of #11172 - Alexendoo:tuple-array-conversions-nursery, r=xFrednet
Move tuple_array_conversions to nursery

changelog: Move [`tuple_array_conversions`] to `nursery` (Now allow-by-default)
<!-- FIY: Ignore this change, if the commit gets backported and also https://github.com/rust-lang/rust-clippy/pull/11146 -->
[#11172](https://github.com/rust-lang/rust-clippy/pull/11172)

The nursery change got lost in #11146 and it ended up in pedantic, this puts it in nursery and gives something to backport

r? `@xFrednet`
2023-07-17 12:18:27 +00:00
Alex Macleod
36f84a6176 Move tuple_array_conversions to nursery 2023-07-17 12:09:20 +00:00
Catherine
74a77047da Rewrite [tuple_array_conversions] 2023-07-17 05:36:50 -05:00
y21
c83d58f507 document that write!ing into a string never fails 2023-07-17 12:23:18 +02:00
y21
c3881569af new lint: format_collect 2023-07-17 12:13:34 +02:00
bors
568ccf3fc8 Auto merge of #11083 - sylvestre:autofix, r=dswij
[`semicolon_if_nothing_returned`]: add an autofix

changelog: [`semicolon_if_nothing_returned`]: add an autofix
2023-07-17 08:05:11 +00:00
bors
91095334bf Auto merge of #11168 - xFrednet:00000-free-ice-cream, r=flip1995
Update bug URL to use the ice template

The previous URL linked to the blank new issue from without any template. This will now link to the ICE template :)

* Before: https://github.com/rust-lang/rust-clippy/issues/new
* After: https://github.com/rust-lang/rust-clippy/issues/new?template=ice.yml

That's it, nothing too interesting besides that. For everyone reading this: here, have some free cream 🍨 🍦 and have a beautiful day. 🙃

changelog: none
2023-07-17 07:47:23 +00:00
Sylvestre Ledru
8ebdd62620 [semicolon_if_nothing_returned]: Update the tests with the autofix 2023-07-16 22:19:25 +02:00
bors
d9c24d1b1e Auto merge of #11169 - y21:new_lint_unelide_lifetimes, r=xFrednet
don't hide lifetimes for `LateContext`

Running `cargo dev new_lint --type methods` creates the lint file with hidden lifetimes for the `LateContext` parameter (i.e. `&LateContext`, when it should be `&LateContext<'_>`). This is already warned on with `#![warn(rust_2018_idioms)]`, so clippy should not use hidden lifetimes

changelog: none
2023-07-16 19:56:51 +00:00
y21
498db80d5c don't hide lifetimes for LateContext 2023-07-16 21:37:53 +02:00
Sylvestre Ledru
eaccc6d38f [semicolon_if_nothing_returned]: enable the autofix 2023-07-16 19:37:42 +02:00
xFrednet
f928d78211
clippy-driver: Update bug URL to use the ice template 2023-07-16 11:51:01 +02:00
bors
1d33469658 Auto merge of #11157 - flip1995:rustup, r=flip1995
Rustup

r? `@ghost`

changelog: none
2023-07-14 11:37:23 +00:00
Philipp Krones
753c30f347
Bump nightly version -> 2023-07-14 2023-07-14 13:36:32 +02:00
Philipp Krones
faa07d334a
Bump Clippy version -> 0.1.73 2023-07-14 13:36:23 +02:00
Philipp Krones
415fdb2d1a
Merge remote-tracking branch 'upstream/master' into rustup 2023-07-14 13:36:16 +02:00
bors
bafde54367 Auto merge of #11152 - Alexendoo:unnecessary-cast-applicability, r=Manishearth
Set `unnecessary_cast` suggestion to `MaybeIncorrect` for pointer casts

Closes #11113

changelog: none
2023-07-14 07:39:49 +00:00
bors
4b355d8f31 Auto merge of #11154 - Alexendoo:track-clippy-conf-dir, r=flip1995
Trigger a rebuild when `CLIPPY_CONF_DIR` changes

changelog: none

This is a fix for https://github.com/rust-lang/rust-clippy/issues/9928#issuecomment-1634698183, any time `CLIPPY_CONF_DIR` changes cargo will now rebuild the crate clippy is being run on, giving it a chance to lint with the (potentially) different config file

r? `@flip1995`
2023-07-13 21:31:11 +00:00
Alex Macleod
36279f1b63 Trigger a rebuild when CLIPPY_CONF_DIR changes 2023-07-13 20:06:12 +00:00
Alex Macleod
ea36a9df75 Set unnecessary_cast suggestion to MaybeIncorrect for pointer casts
Removing casts may cause type inference to stop working which requires
manual intervention
2023-07-13 13:29:41 +00:00
bors
7ccf5d404b Auto merge of #11095 - Alexendoo:rustfmt-imports, r=Manishearth
Add `imports_granularity = "Module"` to rustfmt.toml

This lets rustfmt split/merge imports, `Module` seems to be the most common style in clippy

https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#imports_granularity

changelog: none

Almost all the updates other than the config file change are from `cargo dev fmt` or blessed tests, the exceptions being

- `tests/ui/single_component_path_imports.rs`
- `tests/ui/single_component_path_imports_nested_first.rs`
- `tests/ui/single_component_path_imports_self_after.rs`
- `tests/ui/single_component_path_imports_self_before.rs`
- `tests/ui/unsafe_removed_from_name.rs` (added a test with merged imports as a drive by)
- `tests/ui/wildcard_imports.rs`
- `tests/ui/wildcard_imports_2021.rs`
2023-07-13 12:54:52 +00:00
Alex Macleod
2811effe34 Add imports_granularity = "Module" to rustfmt.toml 2023-07-13 12:44:57 +00:00
bors
a0e825786b Auto merge of #11147 - y21:issue11145, r=Alexendoo
[`arithmetic_side_effect`]: allow different types on the right hand side for `Wrapping<T>`

Fixes #11145

This lint has a list of allowed types, one of which is `Wrapping<T>`, but it was only actually allowed if the type on the right hand side was also `Wrapping<T>`, which meant that, for example, `Wrapping<u32> += u32` would still lint. It now allows binary ops involving `Wrapping<T>` regardless of the type on the rhs.
These impls have only existed since Rust 1.60.0, so that is probably why the lint was previously not handling this correctly

changelog: [`arithmetic_side_effect`]: allow different types on the right hand side for `Wrapping<T>` (e.g. `Wrapping<T> += T`)
2023-07-13 12:28:52 +00:00
bors
631faa1bc7 Auto merge of #11146 - hydro-project:tuple-array-conversions, r=Centri3,xFrednet
[`tuple_array_conversions`]: move from `complexity` to `nursery`

The lint suggestion is arguably often less readable and more complex than the original code.

For example, which of the following is the most readable:
```rust
let _vertices = edges.flat_map(|(src, dst)| [src, dst]);
let _vertices = edges.flat_map(<_ as Into<[i32; 2]>>::into);
let _vertices = edges.flat_map(<[i32; 2]>::from);
```

The lint can be useful, but really only applies if the tuple is either long enough that naming the fields is silly (maybe at least 4 entries long), or if the author intends the fields to be homogenous, which is author intent and can't be determined by the lint. Therefore I think the lint should be marked as pedantic.

Currently, there are also a lot of false positives with the lint:
* https://github.com/rust-lang/rust-clippy/issues/11082
* https://github.com/rust-lang/rust-clippy/issues/11085
* https://github.com/rust-lang/rust-clippy/issues/11100 (https://github.com/rust-lang/rust-clippy/pull/11105)
* https://github.com/rust-lang/rust-clippy/issues/11124
* https://github.com/rust-lang/rust-clippy/issues/11144

Should fix those issues before enabling it for everyone.

---

changelog: Move [`tuple_array_conversions`] to `nursery` (Now allow-by-default)
<!-- FIY: Ignore this change, if the commit gets backported -->
[#11146](https://github.com/rust-lang/rust-clippy/pull/11146)
2023-07-13 08:39:33 +00:00
bors
b10a0aa057 Auto merge of #11148 - smoelius:nb-known-problems, r=Jarcho
Add "Known problems" section to `needless_borrow` documentation

While this PR doesn't fix the issue in #11142, it at least communicates that this is a known limitation.

r? `@Jarcho`

changelog: Add "Known problems" section to `needless_borrow` documentation
2023-07-13 01:22:10 +00:00
Catherine
2e43d0c917 Improve suggestion and add more tests 2023-07-12 19:54:55 -05:00
Catherine
885ce610fe New lint [four_forward_slashes]
Make it trim the contents
2023-07-12 19:39:24 -05:00
Samuel Moelius
050b714c9d Add "Known problems" section to needless_borrow documentation 2023-07-12 20:15:54 -04:00
bors
df92b5284e Auto merge of #11123 - panosfol:master, r=giraffate
[`panic_in_result_fn`] remove `todo!`, `unimplemented!`, `unreachable!`

This commit fixes #11025 by removing checks for `todo!`, `unimplemented!` and `unreachable!`.

changelog: [`panic_in_result_fn`] remove `todo!`, `unimplemented!`, `unreachable!`
2023-07-12 23:57:29 +00:00
bors
d398e59163 Auto merge of #11138 - xFrednet:changelog-1-71, r=flip1995
Changelog for Rust 1.71 👑

Roses are red,
violets are blue,
new format is tried,
it's way less of a fight

---

Hey `@rust-lang/clippy,` `@blyxyas,` and `@Centri3,` I've tried the "new"/minimal changelog format we discussed a few meetings ago. I like it, and the writing process was also way quicker.

[🖼️ Rendered 🖼️](https://github.com/xFrednet/rust-clippy/blob/changelog-1-71/CHANGELOG.md#rust-171)

Furthermore, a big thank you to `@blyxyas` and `@Alexendoo` for updating the script that fetches the PR commits and adding links to the config values to the changelog. ❤️

---

changelog: none
2023-07-12 22:35:22 +00:00
Mingwei Samuel
4102a309d7 [tuple_array_conversions]: move from complexity to nursery
Due to outstanding issues:
* https://github.com/rust-lang/rust-clippy/issues/11082
* https://github.com/rust-lang/rust-clippy/issues/11085
* https://github.com/rust-lang/rust-clippy/issues/11100 (https://github.com/rust-lang/rust-clippy/pull/11105)
* https://github.com/rust-lang/rust-clippy/issues/11124
* https://github.com/rust-lang/rust-clippy/issues/11144
2023-07-12 15:28:46 -07:00
y21
c5fc61ca94 [arithmetic_side_effect]: allow different rhs type 2023-07-13 00:24:10 +02:00