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

orphan-module

Level: warn · Article: Structs

Every function in your utils file is a method on a type that doesn’t exist yet. The type is there. You just haven’t named it.

What it checks

A module named utils, util, helpers, helper, common or misc, whether declared inline or as mod utils;.

Don’t

#![allow(unused)]
fn main() {
mod utils {
    pub fn distance_between(from: &GpsCoordinates, to: &GpsCoordinates) -> Distance {
        Distance::haversine(from.latitude, from.longitude, to.latitude, to.longitude)
    }

    pub fn format_coordinates(coordinates: &GpsCoordinates) -> String {
        format!("{}, {}", coordinates.latitude, coordinates.longitude)
    }

    pub fn parse_coordinates(input: &str) -> Result<GpsCoordinates, InvalidCoordinates> {
        let (latitude, longitude) = input.split_once(',').ok_or(InvalidCoordinates::MissingComma)?;
        Ok(GpsCoordinates {
            latitude: latitude.parse()?,
            longitude: longitude.parse()?,
        })
    }
}
}

It starts with one function that has no obvious home. Then fifteen. Then it is 800 lines and nobody can say what it is about, because it is not about anything. It is a drawer.

Do

#![allow(unused)]
fn main() {
struct GpsCoordinates {
    latitude: Latitude,
    longitude: Longitude,
}

impl GpsCoordinates {
    fn parse(input: &str) -> Result<Self, InvalidCoordinates> {
        let (latitude, longitude) = input.split_once(',').ok_or(InvalidCoordinates::MissingComma)?;
        Ok(Self {
            latitude: latitude.parse()?,
            longitude: longitude.parse()?,
        })
    }

    fn display(&self) -> String {
        format!("{}, {}", self.latitude, self.longitude)
    }

    fn distance_to(&self, other: &Self) -> Distance {
        Distance::haversine(self, other)
    }
}
}

Five functions that share three parameters are a struct. Name it and the orphans find their home.

Options

[naming]
orphan-modules = ["common", "helper", "helpers", "misc", "util", "utils"]

Silence it

#![allow(unused)]
fn main() {
// rabot: allow(orphan-module) test support only: builders and fixtures for the integration suite
mod helpers;
}