RS.CLIPPY.IP_CONSTANT

Hardcoded localhost IP address

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: ip_constant. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for IP addresses that could be replaced with predefined constants such as Ipv4Addr::new(127, 0, 0, 1) instead of using the appropriate constants.

Why is this bad?

Using specific IP addresses like 127.0.0.1 or ::1 is less clear and less maintainable than using the predefined constants Ipv4Addr::LOCALHOST or Ipv6Addr::LOCALHOST. These constants improve code readability, make the intent explicit, and are less error-prone.

Example

use std::net::{Ipv4Addr, Ipv6Addr};

// IPv4 loopback
let addr_v4 = Ipv4Addr::new(127, 0, 0, 1);

// IPv6 loopback
let addr_v6 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);

Use instead:

use std::net::{Ipv4Addr, Ipv6Addr};

// IPv4 loopback
let addr_v4 = Ipv4Addr::LOCALHOST;

// IPv6 loopback
let addr_v6 = Ipv6Addr::LOCALHOST;