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

too-many-parameters

Level: warn · Article: Structs

The signal that you need a struct: you’re passing the same three parameters to five different functions. Those parameters are trying to tell you something.

What it checks

A function or method takes more than 7 parameters (self not counted). Methods inside impl Trait for T are skipped.

Don’t

#![allow(unused)]
fn main() {
impl Renderer {
    fn render(
        &self,
        title: &str,
        width: Width,
        height: Height,
        dpi: Dpi,
        margin: Margin,
        font: &Font,
        color: Color,
        background: Color,
    ) -> Image {
        self.blank(width, height, dpi)
            .fill(background)
            .text(title, font, color, margin)
    }
}
}

Do

#![allow(unused)]
fn main() {
struct Canvas {
    dpi: Dpi,
    height: Height,
    margin: Margin,
    width: Width,
}

struct Style {
    background: Color,
    color: Color,
    font: Font,
}

impl Renderer {
    fn render(&self, title: &str, canvas: &Canvas, style: &Style) -> Image {
        self.blank(canvas)
            .fill(style.background)
            .text(title, &style.font, style.color, canvas.margin)
    }
}
}

Parameters that travel together are a struct waiting to be named. Once named, they get a home for the logic that was scattered across every caller.

Options

[thresholds]
too-many-parameters = 7

Silence it

#![allow(unused)]
fn main() {
// rabot: allow(too-many-parameters) mirrors the C ABI of libfoo_render exactly
}