Repository navigation
lint: Avoid unused_parens warning for ref patterns binding an identifier - #165948
Conversation
|
r? @folkertdev rustbot has assigned @folkertdev. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| && let Ident(_, _, Some(ref inner_pat)) = pat.kind | ||
| && matches!( | ||
| inner_pat.kind, | ||
| Struct(..) | Tuple(..) | TupleStruct(..) | Slice(..) | ||
| ) |
There was a problem hiding this comment.
| && let Ident(_, _, Some(ref inner_pat)) = pat.kind | |
| && matches!( | |
| inner_pat.kind, | |
| Struct(..) | Tuple(..) | TupleStruct(..) | Slice(..) | |
| ) | |
| && let Ident(_, _, Some(_)) = pat.kind |
Should we perhaps suppress it for all ident @ … patterns?
There was a problem hiding this comment.
I suppose it could help. One could argue &v @ w is also a little confusing?
Yea can't think of a case where it might be unnecessary to have parenthesis. Thanks I'll update it then!
This comment has been minimized.
This comment has been minimized.
| // Avoid linting on patterns like `&(x @ _)`, as without parens `&x @ _` | ||
| // it creates confusion and seems like `x` is binding by reference, #165636. | ||
| if let Paren(ref pat) = p.kind | ||
| && *pinned == Pinnedness::Not | ||
| && let Ident(_, _, Some(..)) = pat.kind | ||
| { | ||
| return; | ||
| } |
There was a problem hiding this comment.
Can you add some test cases, we have a bunch at tests/ui/lint/unused_parens_*.
Also why the special logic for pinned?
There was a problem hiding this comment.
Can you add some test cases, we have a bunch at tests/ui/lint/unused_parens_*.
Yes of course!
Also why the special logic for pinned?
The existing pattern matching allows linting on pinned reference patterns and I think something like &pin const (mut _x @ _) doesn't really need parenthesis at all and is just as fine if written as &pin const mut _x @ _ ?
Also the existing tests for pinned reference patterns lints such patterns and warns against using parens. So removing the pinned check would also require modifying those tests. I guess its debatable whether or not parens are helpful for &pin() patterns, personally it feels fine but writing code with a bit of explicit semantic meaning never hurts.
|
Reminder, once the PR becomes ready for a review, use |
For reference patterns like `&(v @ S{a, b})` the compiler emits the
unused parens warning. Without the parenthesis the pattern becomes
`&v @ S{a, b}` which makes the expression look a little confusing and
it seems like `v` is being binded by reference.
Add an explicit pattern matcher to catch such a pattern that would
involve an identifier binding with a compound type and avoid emitting
the `unused_parens` diagnostics for this.
Signed-off-by: Hardik Kumar <[email protected]>
Signed-off-by: Hardik Kumar <[email protected]>
Signed-off-by: Hardik Kumar <[email protected]>
Signed-off-by: Hardik Kumar <[email protected]>
26b7334 to
4ec3020
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
…uwer Rollup of 11 pull requests Successful merges: - #165913 (MirPatch: reduce new statement insertion complexity) - #166081 (test `feature(asm_const_ptr)` with a vtable) - #166094 (Chain the spans of qualified paths instead of having a first-to-last scheme ) - #166113 (Fix dead-code marking dead enum variants as live) - #166118 (rustc_target: catch up with LLVM 24 movement on ppc64le bsd data layout) - #166155 (windows seek_write: document interaction with append mode) - #165935 (Duplicate attribute diagnostic cleanups) - #165948 (lint: Avoid unused_parens warning for ref patterns binding an identifier) - #166018 (`rustc_mir_build`: de-duplicate `local_temp_lifetime()`) - #166111 (Add `cd` alias to `std::env::set_current_dir`) - #166157 (Don't recover invalid literals in attribute parsing)
…uwer Rollup of 11 pull requests Successful merges: - #165913 (MirPatch: reduce new statement insertion complexity) - #166081 (test `feature(asm_const_ptr)` with a vtable) - #166094 (Chain the spans of qualified paths instead of having a first-to-last scheme ) - #166113 (Fix dead-code marking dead enum variants as live) - #166118 (rustc_target: catch up with LLVM 24 movement on ppc64le bsd data layout) - #166155 (windows seek_write: document interaction with append mode) - #165935 (Duplicate attribute diagnostic cleanups) - #165948 (lint: Avoid unused_parens warning for ref patterns binding an identifier) - #166018 (`rustc_mir_build`: de-duplicate `local_temp_lifetime()`) - #166111 (Add `cd` alias to `std::env::set_current_dir`) - #166157 (Don't recover invalid literals in attribute parsing)
Rollup merge of #165948 - hardikxk:lint/unused-parens, r=folkertdev lint: Avoid unused_parens warning for ref patterns binding an identifier For reference patterns like `&(v @ S{a, b})` the compiler emits the unused parens warning. Without the parenthesis the pattern becomes `&v @ S{a, b}` which makes the expression look a little confusing and it seems like `v` is being binded by reference. Add an explicit pattern matcher to catch all such patterns that would involve an identifier binding with any pattern and avoid emitting the `unused_parens` diagnostic for this. closes #165636
For reference patterns like
&(v @ S{a, b})the compiler emits the unused parens warning. Without the parenthesis the pattern becomes&v @ S{a, b}which makes the expression look a little confusing and it seems likevis being binded by reference.Add an explicit pattern matcher to catch all such patterns that would involve an identifier binding with any pattern and avoid emitting the
unused_parensdiagnostic for this.closes #165636