*[View all
comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust-clippy/pull/16600)*
changelog: [`manual_option_zip`]: new lint that detects `a.and_then(|a|
b.map(|b| (a, b)))` and suggests using `a.zip(b)` instead
This PR introduces a new lint `manual_option_zip` that identifies cases
where `Option::and_then` is followed by `Option::map` in the provided
closure to construct a tuple that could be more concisely shaped using
`Option::zip`.
The lint detects the pattern:
```rust
a.and_then(|x| b.map(|y| (x, y)))
```
And suggests replacing it with:
```rust
a.zip(b)
```
Closesrust-lang/rust-clippy#16599
*[View all
comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust-clippy/pull/16687)*
Fixesrust-lang/rust-clippy#16639
### Description
This PR introduces a new lint manual\_noop\_waker that detects manual,
empty implementations of the std::task::Wake trait. These can be
replaced with std::task::Waker::noop(), which is more performant (avoids
Arc allocation) and cleaner.
### Example
```rust
struct MyWaker;
impl Wake for MyWaker {
fn wake(self: Arc<Self>) {}
fn wake_by_ref(self: &Arc<Self>) {}
}
````
Can be replaced with:
```rust
let waker = Waker::noop();
````
* \[x\] I've followed the [contribution
guidelines](https://github.com/rust-lang/rust-clippy/blob/master/CONTRIBUTING.md)
* \[x\] I've added UI tests for the new lint
* \[x\] I've run `cargo dev update_lints`
changelog: `[manual_noop_waker]`: Added a lint to detect manual no-op
Wake implementations.
*Please write a short comment explaining your change (or "none" for
internal only changes)*
Fixesrust-lang/rust-clippy#16110
changelog: [`similar_names`] : Changed the lint docs to reflect its
actual behavior
When a loop variable is unused (`for _ in 0..n`) and the counter is
being
incremented, Clippy previously suggested the generic zip-based rewrite.
This PR adds a more idiomatic `.take(n)` suggestion for the common case
where the range is half-open and starts at zero:
```rust
// before
for _ in 0..MAX {
base += 1;
}
// suggested
for base in (100..).take(MAX)
```
Non-zero starts (`5..MAX`) and inclusive ranges (`0..=MAX`) are
explicitly excluded and fall through to the existing zip suggestion.
Both cases are covered by new UI tests.
changelog: [`explicit_counter_loop`]: suggest `(init..).take(n)` when
loop variable is unused and range is `0..n`
fixesrust-lang/rust-clippy#16642
Add macro matcher for `guard` fragment specifier
Tracking issue #153104
This PR implements a new `guard` macro matcher to match `if-let` guards (specifically [`MatchArmGuard`](https://github.com/rust-lang/reference/blob/50a1075e879be75aeec436252c84eef0fad489f4/src/expressions/match-expr.md#match-guards)). In the upcoming PR, we can use this new matcher in the `matches!` and `assert_matches!` macros to support their use with `if-let` guards. (see #152313)
The original `Expr` used to represent a guard has been wrapped in a new `Guard` type, allowing us to carry the span information of the leading `if` keyword. However, it might be even better to include the `if` keyword in the `Guard` type as well? I've left a FIXME comment in the code.
fixes: rust-lang/rust-clippy#16742
properly select post operation for identity map according to map method
kind
changelog: [`iter_kv_map`]: handle identity map for `map` and `flat_map`
*[View all
comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust-clippy/pull/16683)*
Extend the lint to detect the case where the popped value is used, but
in such cases just emit the lint with no suggestion.
changelog: [`manual_pop_if`]: in case the popped value is used, just
emit the lint with no suggestion.
changelog: [`manual_pop_if`]: also cover `.pop().unwrap_unchecked()`
`matching_root_macro_call` was taking up lots of instructions. This PR
reduces its usage and thus, the number of instructions.
Profiled in `wasmi`. 23,010,846,019 icount -> 22,245,910,522 icount
**-3.324%** performance improvement!
3 Lints were affected:
- `repeat_vec_with_capacity`: 783,874,512 -> 24,488,225
- `string_patterns`: Not measured in benchmark.
- `filter_map`: Weirdly went from 22,000,078 to 22,017,509. But this
doesn't make sense so it's completely anecdotal. (In no world would
changing a function call with a boolean check make *anything* more
expensive.
changelog:[`repeat_vec_with_capacity`]: Optimize by 96.876%
`matching_root_macro_call` was taking up lots of instructions.
This PR reduces its usage and thus, the number of instructions.
Profiled in `wasmi`. 23,010,846,019 icount -> 22,245,910,522 icount
**-3.324%** performance improvement!
3 Lints were affected:
- `repeat_vec_with_capacity`: 783,874,512 -> 24,488,225
- `string_patterns`: Not measured in benchmark.
- `filter_map`: Weirdly went from 22,000,078 to 22,017,509. But this
doesn't make sense so it's completely anecdotal. (In no world would changing
a function call with a boolean check make *anything* more expensive.
changelog:[`repeat_vec_with_capacity`]: Optimize by 96.876%
Turns out that `manual_is_ascii_check` was taking up 820 million
instructions of `wasmi`'s 23 billion. Now... it doesn't. It now takes 23
million, still too much, but not as noticeable.
Profiled in `wasmi`. 23,821,891,527 icount -> 23,010,885,185 icount,
3.3% performance improvement.
changelog:[`manual_is_ascii_check`]: Optimize by 97.125%
Turns out that `manual_is_ascii_check` was taking up 820 million instructions
of `wasmi`'s 23 billion. Now... it doesn't. It now takes 23 million, still too much, but not
as noticeable.
Profiled in `wasmi`. 23,821,891,527 icount -> 23,035,885,185 icount
changelog:[`manual_is_ascii_check`]: Optimize by 97.125%
Extend the lint to detect the case where the popped value is used, but
in such cases just emit the lint with no suggestion.
Also detect the pop().unwrap_unchecked() case.
changelog: none
Simplify find_attr! for HirId usage
Add a `HasAttrs<'tcx, Tcx>` trait to `rustc_hir` that allows `find_attr!` to accept `DefId`, `LocalDefId`, `OwnerId`, and `HirId` directly, instead of requiring callers to manually fetch the attribute slice first.
Before:
`find_attr!(tcx.hir_attrs(hir_id), SomeAttr)`
After:
`find_attr!(tcx, hir_id, SomeAttr)`
The trait is defined in `rustc_hir` with a generic `Tcx` parameter to avoid a dependency cycle (`rustc_hir` cannot depend on `rustc_middle`). The four concrete impls for `TyCtxt` are in `rustc_middle`.
Fixes https://github.com/rust-lang/rust/issues/153103
Detects manual implementations of the newly implemented
[`BinaryHeap::pop_if()`](https://github.com/rust-lang/rust/issues/151828)
in `manual_pop_if`.
I wasn't sure about how best to handle checking for the nightly feature.
Could we let people compiling with nightly know that the feature is
available even if they don't have it enabled?
changelog: [`manual_pop_if`]: detect manual implementations of
`BinaryHeap::pop_if()`
properly select post operation for identity map according to map method kind
changelog: [`iter_kv_map`]: handle identity map for `map` and `flat_map`
Signed-off-by: Zihan <zihanli0822@gmail.com>
The pretty printer now correctly parenthesizes or-patterns inside
`box` patterns, which changes the Clippy `unnested_or_patterns` lint
suggestion from `box box (0 | 2 | 4)` to `box (box (0 | 2 | 4))`.
The new output is more correct — the old suggestion was itself missing
parens and would have been parsed as `box (box 0) | 2 | 4`.
Signed-off-by: Andrew V. Teylu <andrew.teylu@vector.com>
Couple of driver interface improvements
* Pass Session to `make_codegen_backend` callback. This simplifies some code in miri.
* Move env/file_depinfo from ParseSess to Session. There is no reason it has to be in ParseSess rather than Session.
* Rename hash_untracked_state to track_state to indicate that it isn't just used for hashing state, but also for adding env vars and files to be tracked through the dep info file.