Skip to content

Add ability to use or-patterns within expressions in match arms #14516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Manishearth opened this issue May 29, 2014 · 2 comments
Closed

Add ability to use or-patterns within expressions in match arms #14516

Manishearth opened this issue May 29, 2014 · 2 comments

Comments

@Manishearth
Copy link
Member

We already can use patterns like Some(_) in a match arm. However, stuff like Some( 1 | 2 | 3) doesn't work.

Could support for nesting patterns be added?

@ben0x539
Copy link
Contributor

See also #9663—I guess this should go through the RFC process at this point

@alexcrichton
Copy link
Member

This is a dupe of #17180, and as @P1start mentioned over there the corresponding RFC is rust-lang/rfcs#99, so I'm going to close this in favor of the RFC.

bors added a commit to rust-lang-ci/rust that referenced this issue Jun 5, 2023
Restrict "sort items" assist for traits & impls

This restricts the "sort items alphabetically" assist when the selection is inside a `Impl` or `Trait` node & intersects with one of the associated items.

It re-orders the conditional checks of AST nodes in the `sort_items` function to check for more specific nodes first before checking `Trait` or `Impl` nodes. The `AssistContext` is passed into the `add_sort_methods_assist` function to check if the selection intersects with any inner items, e.g. associated const or type alias, function. In this case the assist does not apply.

Fixes: rust-lang#14516
flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 3, 2025
…items (rust-lang#14516)

Found this while reviewing PR
rust-lang#138384: See
rust-lang#138384 (comment) in
which I suggested a FIXME to be added that I'm now fixing in this PR.

---

Before this PR, `redundant_pub_crate` computed a broken primary Span
when flagging items (`hir::Item`s) that never bear a name (`ForeignMod`,
`GlobalAsm`, `Impl`, `Use(UseKind::Glob)`, `Use(UseKind::ListStem)`).
Namely, it created a span whose high byte index is `DUMMY_SP.hi()` which
is quite broken (`DUMMY_SP` is synonymous with `0..0` wrt. the entire
`SourceMap` meaning it points at the start of the very first source file
in the `SourceMap`).

Why did this happen? Before PR
rust-lang#138384, the offending line looked
like `let span = item.span.with_hi(item.ident.span.hi());`. For nameless
items, `item.ident` used to be set to `Ident(sym::empty, DUMMY_SP)`.
This is where the `DUMMY_SP` came from.

The code means to compute a "shorter item span" that doesn't include the
"body" of items, only the "head" (similar to `TyCtxt::def_span`).

<details><summary>Examples of Clippy's butchered output on
master</summary>

```rs
#![deny(clippy::redundant_pub_crate)]

mod m5 {
    pub mod m5_1 {}
    pub(crate) use m5_1::*;
}
```

```
error: pub(crate) import inside private module
 --> file.rs:1:1
  |
1 | / #![deny(clippy::redundant_pub_crate)]
2 | |
3 | | mod m5 {
4 | |     pub mod m5_1 {}
5 | |     pub(crate) use m5_1::*;
  | |    ^---------- help: consider using: `pub`
  | |____|
  |
```

Or if the `SourceMap` contains multiple files (notice how it leaks
`clippy.toml`!):

```
error: pub(crate) import inside private module
 --> /home/fmease/programming/rust/clippy/clippy.toml:1:1
  |
1 | / avoid-breaking-exported-api = false
2 | |
3 | | check-inconsistent-struct-field-initializers = true
... |
6 | |
  | |_^
  |
 ::: file.rs:6:5
  |
6 |       pub(crate) use m5_1::{*}; // Glob
  |       ---------- help: consider using: `pub`
  |
```

</details>

---

**Note**: Currently, the only nameless item kind that can also have a
visibility is `Use(UseKind::{Glob,ListStem})`. Thus I'm just falling
back to the entire item's Span which wouldn't be that verbose. However,
in the future Rust will feature impl restrictions (like `pub(crate) impl
Trait for Type {}`, see [RFC
3323](https://rust-lang.github.io/rfcs/3323-restrictions.html) and
rust-lang#106074). Using `item.span` for
these would be quite bad (it would include all associated items). Should
I add a FIXME?

---

changelog: [`redundant_pub_crate`]: Fix the code highlighting for
nameless items.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants