Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sorted-struct-pattern

Level: warn · Fixed by rabot fmt · Article: Sorting

What it checks

Fields in a struct pattern, in let or in a match arm, are alphabetical. Patterns have no evaluation order, so this is always safe to rewrite. .. stays last.

Don’t

#![allow(unused)]
fn main() {
impl User {
    fn greeting(&self) -> String {
        let User { name, email, .. } = self;
        format!("{name} <{email}>")
    }
}

impl Event {
    fn describe(&self) -> String {
        match self {
            Event::Moved { to, from, at } => format!("{from} -> {to} at {at}"),
        }
    }
}
}

Do

#![allow(unused)]
fn main() {
impl User {
    fn greeting(&self) -> String {
        let User { email, name, .. } = self;
        format!("{name} <{email}>")
    }
}

impl Event {
    fn describe(&self) -> String {
        match self {
            Event::Moved { at, from, to } => format!("{from} -> {to} at {at}"),
        }
    }
}
}

Silence it

#![allow(unused)]
fn main() {
// rabot: allow(sorted-struct-pattern) mirrors the wire order documented in RFC-12
}