question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non exhaustive reachable patterns lint

See original GitHub issue

Proposal

Add a new lint that checks for missing patterns in matches and destructuring assignment of non_exhaustive enums and structs. This was suggested in the original non_exhaustive PR https://github.com/rust-lang/rust/pull/45394 and there is an open issue https://github.com/rust-lang/rust/issues/84332 for this lint as well as a very early PR https://github.com/rust-lang/rust/pull/86809.

If the attribute is put on the wildcard pattern there is this issue with .. https://github.com/rust-lang/rust/issues/81282.

Example

// crate x
#[non_exhaustive]
pub struct Foo {
    a: u8,
    b: usize,
    c: String,
}

#[non_exhaustive]
pub enum Bar {
    A,
    B,
    C,
}

// crate y
match Bar::A {
    Bar::A => {},
    Bar::B => {},
    #[deny(reachable)] // attribute goes here or on the expresion
    _ => {} // triggers lint "missing Bar::C..."
}

let Foo { 
    a
    b,
    #[warn(reachable)]
    .. // triggers lint "missing field `c`..."
} = structure;

Summary and problem statement

Since the non_exhaustive attribute was introduced it allows crate authors to make normally breaking API changes in a non-breaking way. This lint will warn users of non_exhaustive types when the type changes and they are no longer handling all cases, using the _/.. for more than the NonExhaustive variant/field.

Motivation, use-cases, and solution sketches

Implement this as a lint inside of rustc_lint or implement the lint as part of the usefulness checker here

Links and related work

WIP PR: https://github.com/rust-lang/rust/pull/86809 lint in usefulness checker commit: https://github.com/DevinR528/rust/commit/df377ca309fd5acf1a3dcdcb90ecdfef9578eafb compiler-team MCP: https://github.com/rust-lang/compiler-team/issues/445 zulip compiler/major changes: https://rust-lang.zulipchat.com/#narrow/stream/233931-t-compiler.2Fmajor-changes/topic/Non.20exhaustive.20reachable.20patterns.20lint.20compiler-team.23445

Initial people involved

I have started a WIP PR and plan to finish this as part of my GSoC project.

  • Owner, if known: ?
  • Liaison

What happens now?

This issue is part of the lang-team initiative process. Once this issue is filed, a Zulip topic will be opened for discussion, and the lang-team will review open proposals in its weekly triage meetings. You should receive feedback within a week or two.

This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jplattecommented, Sep 28, 2021

The PR has been open for quite a while and was merged a week ago: rust-lang/rust#86809

2reactions
scottmcmcommented, Jul 21, 2021

👍 to an opt-in lint for this.

@rustbot second

Read more comments on GitHub >

github_iconTop Results From Across the Web

NON_EXHAUSTIVE_OMITTED_PATTERNS in rustc_lint::builtin - Rust
The `non_exhaustive_omitted_patterns` lint detects when a wildcard (`_` or `. ... warning: reachable patterns not covered of non exhaustive enum ...
Read more >
Never patterns, exhaustive matching, and uninhabited types
The idea: ! patterns. Traditionally, when one has an uninhabited type, one “matches against it” by not writing any patterns at all. So,...
Read more >
Linters | golangci-lint
Name Description Presets Since asasalint ⚙️ check for pass any as any in variadic func(...any) bugs 1.47.0 bidichk ⚙️ Checks for dangerous unicode character sequences...
Read more >
Non-exhaustive patterns - Rust match expressions
The _ will match any other value not previously mentioned. The unreachable!() tells the compiler "this code will never execute", ...
Read more >
src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns ...
error: unreachable pattern ... note: the lint level is defined here ... error[E0004]: non-exhaustive patterns: type `u8` is non-empty.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found