Add new lint: ip_consant

This commit is contained in:
relaxcn
2025-05-24 00:17:47 +08:00
committed by Boot0x7c00
parent 5dccb101ed
commit 8d8b3f16d7
13 changed files with 699 additions and 3 deletions
+1
View File
@@ -5888,6 +5888,7 @@ Released 2018-09-13
[`inverted_saturating_sub`]: https://rust-lang.github.io/rust-clippy/master/index.html#inverted_saturating_sub
[`invisible_characters`]: https://rust-lang.github.io/rust-clippy/master/index.html#invisible_characters
[`io_other_error`]: https://rust-lang.github.io/rust-clippy/master/index.html#io_other_error
[`ip_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#ip_constant
[`is_digit_ascii_radix`]: https://rust-lang.github.io/rust-clippy/master/index.html#is_digit_ascii_radix
[`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements
[`items_after_test_module`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_test_module
+1
View File
@@ -379,6 +379,7 @@
crate::methods::INSPECT_FOR_EACH_INFO,
crate::methods::INTO_ITER_ON_REF_INFO,
crate::methods::IO_OTHER_ERROR_INFO,
crate::methods::IP_CONSTANT_INFO,
crate::methods::IS_DIGIT_ASCII_RADIX_INFO,
crate::methods::ITERATOR_STEP_BY_ZERO_INFO,
crate::methods::ITER_CLONED_COLLECT_INFO,
+1
View File
@@ -55,6 +55,7 @@
extern crate rustc_span;
extern crate rustc_target;
extern crate rustc_trait_selection;
extern crate smallvec;
extern crate thin_vec;
#[macro_use]
+52
View File
@@ -0,0 +1,52 @@
use clippy_utils::consts::{ConstEvalCtxt, Constant};
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, QPath, Ty, TyKind};
use rustc_lint::LateContext;
use rustc_span::sym;
use smallvec::SmallVec;
use super::IP_CONSTANT;
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args: &[Expr<'_>]) {
if let ExprKind::Path(QPath::TypeRelative(
Ty {
kind: TyKind::Path(QPath::Resolved(_, func_path)),
..
},
p,
)) = func.kind
&& p.ident.name == sym::new
&& let Some(func_def_id) = func_path.res.opt_def_id()
&& matches!(
cx.tcx.get_diagnostic_name(func_def_id),
Some(sym::Ipv4Addr | sym::Ipv6Addr)
)
&& let Some(args) = args
.iter()
.map(|arg| {
if let Some(Constant::Int(constant @ (0 | 1 | 127 | 255))) = ConstEvalCtxt::new(cx).eval(arg) {
u8::try_from(constant).ok()
} else {
None
}
})
.collect::<Option<SmallVec<[u8; 8]>>>()
{
let constant_name = match args.as_slice() {
[0, 0, 0, 0] | [0, 0, 0, 0, 0, 0, 0, 0] => "UNSPECIFIED",
[127, 0, 0, 1] | [0, 0, 0, 0, 0, 0, 0, 1] => "LOCALHOST",
[255, 255, 255, 255] => "BROADCAST",
_ => return,
};
span_lint_and_then(cx, IP_CONSTANT, expr.span, "hand-coded well-known IP address", |diag| {
diag.span_suggestion_verbose(
expr.span.with_lo(p.ident.span.lo()),
"use",
constant_name,
Applicability::MachineApplicable,
);
});
}
}
+39
View File
@@ -37,6 +37,7 @@
mod inspect_for_each;
mod into_iter_on_ref;
mod io_other_error;
mod ip_constant;
mod is_digit_ascii_radix;
mod is_empty;
mod iter_cloned_collect;
@@ -4528,6 +4529,42 @@
"detect swap with a temporary value"
}
declare_clippy_lint! {
/// ### 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
/// ```no_run
/// 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:
/// ```no_run
/// use std::net::{Ipv4Addr, Ipv6Addr};
///
/// // IPv4 loopback
/// let addr_v4 = Ipv4Addr::LOCALHOST;
///
/// // IPv6 loopback
/// let addr_v6 = Ipv6Addr::LOCALHOST;
/// ```
#[clippy::version = "1.89.0"]
pub IP_CONSTANT,
pedantic,
"hardcoded localhost IP address"
}
#[expect(clippy::struct_excessive_bools)]
pub struct Methods {
avoid_breaking_exported_api: bool,
@@ -4706,6 +4743,7 @@ pub fn new(conf: &'static Conf, format_args: FormatArgsStorage) -> Self {
MANUAL_CONTAINS,
IO_OTHER_ERROR,
SWAP_WITH_TEMPORARY,
IP_CONSTANT,
]);
/// Extracts a method call name, args, and `Span` of the method name.
@@ -4738,6 +4776,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
useless_nonzero_new_unchecked::check(cx, expr, func, args, self.msrv);
io_other_error::check(cx, expr, func, args, self.msrv);
swap_with_temporary::check(cx, expr, func, args);
ip_constant::check(cx, expr, func, args);
},
ExprKind::MethodCall(method_call, receiver, args, _) => {
let method_span = method_call.ident.span;
@@ -1,4 +1,5 @@
#![warn(clippy::await_holding_invalid_type)]
#![allow(clippy::ip_constant)]
use std::net::Ipv4Addr;
async fn bad() -> u32 {
@@ -1,5 +1,5 @@
error: holding a disallowed type across an await point `std::string::String`
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:5:9
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:6:9
|
LL | let _x = String::from("hello");
| ^^
@@ -9,13 +9,13 @@ LL | let _x = String::from("hello");
= help: to override `-D warnings` add `#[allow(clippy::await_holding_invalid_type)]`
error: holding a disallowed type across an await point `std::net::Ipv4Addr`
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:11:9
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:12:9
|
LL | let x = Ipv4Addr::new(127, 0, 0, 1);
| ^
error: holding a disallowed type across an await point `std::string::String`
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:35:13
--> tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.rs:36:13
|
LL | let _x = String::from("hi!");
| ^^
+107
View File
@@ -0,0 +1,107 @@
#![warn(clippy::ip_constant)]
#![allow(dead_code)]
#![allow(clippy::identity_op)]
#![allow(clippy::eq_op)]
fn literal_test1() {
use std::net::Ipv4Addr;
let _ = Ipv4Addr::LOCALHOST;
//~^ ip_constant
let _ = Ipv4Addr::BROADCAST;
//~^ ip_constant
let _ = Ipv4Addr::UNSPECIFIED;
//~^ ip_constant
use std::net::Ipv6Addr;
let _ = Ipv6Addr::LOCALHOST;
//~^ ip_constant
let _ = Ipv6Addr::UNSPECIFIED;
//~^ ip_constant
}
fn literal_test2() {
use std::net;
let _ = net::Ipv4Addr::LOCALHOST;
//~^ ip_constant
let _ = net::Ipv4Addr::BROADCAST;
//~^ ip_constant
let _ = net::Ipv4Addr::UNSPECIFIED;
//~^ ip_constant
let _ = net::Ipv6Addr::LOCALHOST;
//~^ ip_constant
let _ = net::Ipv6Addr::UNSPECIFIED;
//~^ ip_constant
}
fn literal_test3() {
let _ = std::net::Ipv4Addr::LOCALHOST;
//~^ ip_constant
let _ = std::net::Ipv4Addr::BROADCAST;
//~^ ip_constant
let _ = std::net::Ipv4Addr::UNSPECIFIED;
//~^ ip_constant
let _ = std::net::Ipv6Addr::LOCALHOST;
//~^ ip_constant
let _ = std::net::Ipv6Addr::UNSPECIFIED;
//~^ ip_constant
}
const CONST_U8_0: u8 = 0;
const CONST_U8_1: u8 = 1;
const CONST_U8_127: u8 = 127;
const CONST_U8_255: u8 = 255;
const CONST_U16_0: u16 = 0;
const CONST_U16_1: u16 = 1;
fn const_test1() {
use std::net::Ipv4Addr;
let _ = Ipv4Addr::LOCALHOST;
//~^ ip_constant
let _ = Ipv4Addr::BROADCAST;
//~^ ip_constant
let _ = Ipv4Addr::UNSPECIFIED;
//~^ ip_constant
use std::net::Ipv6Addr;
let _ = Ipv6Addr::LOCALHOST;
let _ = Ipv6Addr::UNSPECIFIED;
}
fn const_test2() {
use std::net::Ipv4Addr;
let _ = Ipv4Addr::LOCALHOST;
//~^ ip_constant
let _ = Ipv4Addr::BROADCAST;
//~^ ip_constant
let _ = Ipv4Addr::UNSPECIFIED;
//~^ ip_constant
use std::net::Ipv6Addr;
let _ = Ipv6Addr::LOCALHOST;
//~^ ip_constant
let _ = Ipv6Addr::LOCALHOST;
//~^ ip_constant
}
macro_rules! ipv4_new {
($a:expr, $b:expr, $c:expr, $d:expr) => {
std::net::Ipv4Addr::new($a, $b, $c, $d)
};
}
fn macro_test() {
let _ = ipv4_new!(127, 0, 0, 1);
// no lint
let _ = ipv4_new!(255, 255, 255, 255);
// no lint
let _ = ipv4_new!(0, 0, 0, 0);
// no lint
}
fn main() {
// UI Test
}
+127
View File
@@ -0,0 +1,127 @@
#![warn(clippy::ip_constant)]
#![allow(dead_code)]
#![allow(clippy::identity_op)]
#![allow(clippy::eq_op)]
fn literal_test1() {
use std::net::Ipv4Addr;
let _ = Ipv4Addr::new(127, 0, 0, 1);
//~^ ip_constant
let _ = Ipv4Addr::new(255, 255, 255, 255);
//~^ ip_constant
let _ = Ipv4Addr::new(0, 0, 0, 0);
//~^ ip_constant
use std::net::Ipv6Addr;
let _ = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
//~^ ip_constant
let _ = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
//~^ ip_constant
}
fn literal_test2() {
use std::net;
let _ = net::Ipv4Addr::new(127, 0, 0, 1);
//~^ ip_constant
let _ = net::Ipv4Addr::new(255, 255, 255, 255);
//~^ ip_constant
let _ = net::Ipv4Addr::new(0, 0, 0, 0);
//~^ ip_constant
let _ = net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
//~^ ip_constant
let _ = net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
//~^ ip_constant
}
fn literal_test3() {
let _ = std::net::Ipv4Addr::new(127, 0, 0, 1);
//~^ ip_constant
let _ = std::net::Ipv4Addr::new(255, 255, 255, 255);
//~^ ip_constant
let _ = std::net::Ipv4Addr::new(0, 0, 0, 0);
//~^ ip_constant
let _ = std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
//~^ ip_constant
let _ = std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
//~^ ip_constant
}
const CONST_U8_0: u8 = 0;
const CONST_U8_1: u8 = 1;
const CONST_U8_127: u8 = 127;
const CONST_U8_255: u8 = 255;
const CONST_U16_0: u16 = 0;
const CONST_U16_1: u16 = 1;
fn const_test1() {
use std::net::Ipv4Addr;
let _ = Ipv4Addr::new(CONST_U8_127, CONST_U8_0, CONST_U8_0, CONST_U8_1);
//~^ ip_constant
let _ = Ipv4Addr::new(CONST_U8_255, CONST_U8_255, CONST_U8_255, CONST_U8_255);
//~^ ip_constant
let _ = Ipv4Addr::new(CONST_U8_0, CONST_U8_0, CONST_U8_0, CONST_U8_0);
//~^ ip_constant
use std::net::Ipv6Addr;
let _ = Ipv6Addr::new(
//~^ ip_constant
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
CONST_U16_1,
);
let _ = Ipv6Addr::new(
//~^ ip_constant
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
CONST_U16_0,
);
}
fn const_test2() {
use std::net::Ipv4Addr;
let _ = Ipv4Addr::new(126 + 1, 0, 0, 1);
//~^ ip_constant
let _ = Ipv4Addr::new(254 + CONST_U8_1, 255, { 255 - CONST_U8_0 }, CONST_U8_255);
//~^ ip_constant
let _ = Ipv4Addr::new(0, CONST_U8_255 - 255, 0, { 1 + 0 - 1 });
//~^ ip_constant
use std::net::Ipv6Addr;
let _ = Ipv6Addr::new(0 + CONST_U16_0, 0, 0, 0, 0, 0, 0, 1);
//~^ ip_constant
let _ = Ipv6Addr::new(0 + 0, 0, 0, 0, 0, { 2 - 1 - CONST_U16_1 }, 0, 1);
//~^ ip_constant
}
macro_rules! ipv4_new {
($a:expr, $b:expr, $c:expr, $d:expr) => {
std::net::Ipv4Addr::new($a, $b, $c, $d)
};
}
fn macro_test() {
let _ = ipv4_new!(127, 0, 0, 1);
// no lint
let _ = ipv4_new!(255, 255, 255, 255);
// no lint
let _ = ipv4_new!(0, 0, 0, 0);
// no lint
}
fn main() {
// UI Test
}
+338
View File
@@ -0,0 +1,338 @@
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:8:13
|
LL | let _ = Ipv4Addr::new(127, 0, 0, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::ip-constant` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::ip_constant)]`
help: use
|
LL - let _ = Ipv4Addr::new(127, 0, 0, 1);
LL + let _ = Ipv4Addr::LOCALHOST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:10:13
|
LL | let _ = Ipv4Addr::new(255, 255, 255, 255);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv4Addr::new(255, 255, 255, 255);
LL + let _ = Ipv4Addr::BROADCAST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:12:13
|
LL | let _ = Ipv4Addr::new(0, 0, 0, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv4Addr::new(0, 0, 0, 0);
LL + let _ = Ipv4Addr::UNSPECIFIED;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:16:13
|
LL | let _ = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
LL + let _ = Ipv6Addr::LOCALHOST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:18:13
|
LL | let _ = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
LL + let _ = Ipv6Addr::UNSPECIFIED;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:24:13
|
LL | let _ = net::Ipv4Addr::new(127, 0, 0, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = net::Ipv4Addr::new(127, 0, 0, 1);
LL + let _ = net::Ipv4Addr::LOCALHOST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:26:13
|
LL | let _ = net::Ipv4Addr::new(255, 255, 255, 255);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = net::Ipv4Addr::new(255, 255, 255, 255);
LL + let _ = net::Ipv4Addr::BROADCAST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:28:13
|
LL | let _ = net::Ipv4Addr::new(0, 0, 0, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = net::Ipv4Addr::new(0, 0, 0, 0);
LL + let _ = net::Ipv4Addr::UNSPECIFIED;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:31:13
|
LL | let _ = net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
LL + let _ = net::Ipv6Addr::LOCALHOST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:33:13
|
LL | let _ = net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
LL + let _ = net::Ipv6Addr::UNSPECIFIED;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:38:13
|
LL | let _ = std::net::Ipv4Addr::new(127, 0, 0, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = std::net::Ipv4Addr::new(127, 0, 0, 1);
LL + let _ = std::net::Ipv4Addr::LOCALHOST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:40:13
|
LL | let _ = std::net::Ipv4Addr::new(255, 255, 255, 255);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = std::net::Ipv4Addr::new(255, 255, 255, 255);
LL + let _ = std::net::Ipv4Addr::BROADCAST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:42:13
|
LL | let _ = std::net::Ipv4Addr::new(0, 0, 0, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = std::net::Ipv4Addr::new(0, 0, 0, 0);
LL + let _ = std::net::Ipv4Addr::UNSPECIFIED;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:45:13
|
LL | let _ = std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
LL + let _ = std::net::Ipv6Addr::LOCALHOST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:47:13
|
LL | let _ = std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
LL + let _ = std::net::Ipv6Addr::UNSPECIFIED;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:61:13
|
LL | let _ = Ipv4Addr::new(CONST_U8_127, CONST_U8_0, CONST_U8_0, CONST_U8_1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv4Addr::new(CONST_U8_127, CONST_U8_0, CONST_U8_0, CONST_U8_1);
LL + let _ = Ipv4Addr::LOCALHOST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:63:13
|
LL | let _ = Ipv4Addr::new(CONST_U8_255, CONST_U8_255, CONST_U8_255, CONST_U8_255);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv4Addr::new(CONST_U8_255, CONST_U8_255, CONST_U8_255, CONST_U8_255);
LL + let _ = Ipv4Addr::BROADCAST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:65:13
|
LL | let _ = Ipv4Addr::new(CONST_U8_0, CONST_U8_0, CONST_U8_0, CONST_U8_0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv4Addr::new(CONST_U8_0, CONST_U8_0, CONST_U8_0, CONST_U8_0);
LL + let _ = Ipv4Addr::UNSPECIFIED;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:69:13
|
LL | let _ = Ipv6Addr::new(
| _____________^
LL | |
LL | | CONST_U16_0,
LL | | CONST_U16_0,
... |
LL | | CONST_U16_1,
LL | | );
| |_____^
|
help: use
|
LL - let _ = Ipv6Addr::new(
LL -
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_1,
LL - );
LL + let _ = Ipv6Addr::LOCALHOST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:81:13
|
LL | let _ = Ipv6Addr::new(
| _____________^
LL | |
LL | | CONST_U16_0,
LL | | CONST_U16_0,
... |
LL | | CONST_U16_0,
LL | | );
| |_____^
|
help: use
|
LL - let _ = Ipv6Addr::new(
LL -
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - CONST_U16_0,
LL - );
LL + let _ = Ipv6Addr::UNSPECIFIED;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:96:13
|
LL | let _ = Ipv4Addr::new(126 + 1, 0, 0, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv4Addr::new(126 + 1, 0, 0, 1);
LL + let _ = Ipv4Addr::LOCALHOST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:98:13
|
LL | let _ = Ipv4Addr::new(254 + CONST_U8_1, 255, { 255 - CONST_U8_0 }, CONST_U8_255);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv4Addr::new(254 + CONST_U8_1, 255, { 255 - CONST_U8_0 }, CONST_U8_255);
LL + let _ = Ipv4Addr::BROADCAST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:100:13
|
LL | let _ = Ipv4Addr::new(0, CONST_U8_255 - 255, 0, { 1 + 0 - 1 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv4Addr::new(0, CONST_U8_255 - 255, 0, { 1 + 0 - 1 });
LL + let _ = Ipv4Addr::UNSPECIFIED;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:104:13
|
LL | let _ = Ipv6Addr::new(0 + CONST_U16_0, 0, 0, 0, 0, 0, 0, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv6Addr::new(0 + CONST_U16_0, 0, 0, 0, 0, 0, 0, 1);
LL + let _ = Ipv6Addr::LOCALHOST;
|
error: hand-coded well-known IP address
--> tests/ui/ip_constant.rs:106:13
|
LL | let _ = Ipv6Addr::new(0 + 0, 0, 0, 0, 0, { 2 - 1 - CONST_U16_1 }, 0, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use
|
LL - let _ = Ipv6Addr::new(0 + 0, 0, 0, 0, 0, { 2 - 1 - CONST_U16_1 }, 0, 1);
LL + let _ = Ipv6Addr::LOCALHOST;
|
error: aborting due to 25 previous errors
+12
View File
@@ -0,0 +1,12 @@
//@error-in-other-file: hand-coded well-known IP address
//@no-rustfix
#![warn(clippy::ip_constant)]
fn external_constant_test() {
let _ = include!("localhost.txt");
// lint in external file `localhost.txt`
}
fn main() {
external_constant_test();
}
+16
View File
@@ -0,0 +1,16 @@
error: hand-coded well-known IP address
--> tests/ui/localhost.txt:1:1
|
LL | std::net::Ipv4Addr::new(127, 0, 0, 1)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::ip-constant` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::ip_constant)]`
help: use
|
LL - std::net::Ipv4Addr::new(127, 0, 0, 1)
LL + std::net::Ipv4Addr::LOCALHOST
|
error: aborting due to 1 previous error
+1
View File
@@ -0,0 +1 @@
std::net::Ipv4Addr::new(127, 0, 0, 1)