|
| 1 | +use std::net::{Ipv4Addr, Ipv6Addr}; |
| 2 | + |
| 3 | +use clippy_utils::msrvs::{self, Msrv}; |
| 4 | +use clippy_utils::source::SpanRangeExt as _; |
| 5 | +use clippy_utils::sym; |
| 6 | +use rustc_hir::{Expr, QPath}; |
| 7 | +use rustc_lint::LateContext; |
| 8 | +use rustc_span::Symbol; |
| 9 | + |
| 10 | +use super::maybe_emit_lint; |
| 11 | + |
| 12 | +static IPV4_ENTITY: &str = "an IPv4 address"; |
| 13 | +static IPV6_ENTITY: &str = "an IPv6 address"; |
| 14 | + |
| 15 | +pub(super) fn check( |
| 16 | + cx: &LateContext<'_>, |
| 17 | + expr: &Expr<'_>, |
| 18 | + lit: Symbol, |
| 19 | + method: Symbol, |
| 20 | + explicit_type: Option<QPath<'_>>, |
| 21 | + msrv: Msrv, |
| 22 | +) { |
| 23 | + let ipaddr_consts_available = msrv.meets(cx, msrvs::IPADDR_CONSTANTS); |
| 24 | + match method { |
| 25 | + sym::Ipv4Addr => { |
| 26 | + // Only use constants such as `Ipv4Addr::LOCALHOST` when the type has been explicitly given |
| 27 | + if let Some((sugg, typed_const)) = ipv4_subst(cx, lit, ipaddr_consts_available, explicit_type) { |
| 28 | + maybe_emit_lint(cx, expr, typed_const, IPV4_ENTITY, sugg); |
| 29 | + } |
| 30 | + }, |
| 31 | + sym::Ipv6Addr => { |
| 32 | + // Only use constants such as `Ipv4Addr::LOCALHOST` when the type has been explicitly given |
| 33 | + if let Some((sugg, typed_const)) = ipv6_subst(cx, lit, ipaddr_consts_available, explicit_type) { |
| 34 | + maybe_emit_lint(cx, expr, typed_const, IPV6_ENTITY, sugg); |
| 35 | + } |
| 36 | + }, |
| 37 | + sym::IpAddr => { |
| 38 | + if let Some((sugg, entity)) = ip_subst(cx, lit, explicit_type) { |
| 39 | + maybe_emit_lint(cx, expr, false, entity, sugg); |
| 40 | + } |
| 41 | + }, |
| 42 | + _ => unreachable!(), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// Suggests a replacement if `addr` is a correct IPv4 address, with: |
| 47 | +/// - the replacement string |
| 48 | +/// - a boolean that indicates if a typed constant is used |
| 49 | +/// |
| 50 | +/// The replacement will be `T::CONSTANT` if a constant is detected, |
| 51 | +/// where `T` is either `explicit_type` if provided, or `Ipv4Addr` |
| 52 | +/// otherwise. |
| 53 | +/// |
| 54 | +/// In other cases, when the type has been explicitly given as `T`, the |
| 55 | +/// `T::new()` constructor will be used. If no type has been explicitly |
| 56 | +/// given, then `[u8; 4].into()` will be used as the context should |
| 57 | +/// already provide the proper information. This allows us not to use |
| 58 | +/// a type name which might not be available in the current scope. |
| 59 | +fn ipv4_subst( |
| 60 | + cx: &LateContext<'_>, |
| 61 | + addr: Symbol, |
| 62 | + with_consts: bool, |
| 63 | + explicit_type: Option<QPath<'_>>, |
| 64 | +) -> Option<(String, bool)> { |
| 65 | + as_ipv4_octets(addr).and_then(|bytes| { |
| 66 | + if let Some(qpath) = explicit_type { |
| 67 | + qpath.span().with_source_text(cx, |ty| { |
| 68 | + if with_consts && &bytes == Ipv4Addr::LOCALHOST.as_octets() { |
| 69 | + (format!("{ty}::LOCALHOST"), true) |
| 70 | + } else if with_consts && &bytes == Ipv4Addr::BROADCAST.as_octets() { |
| 71 | + (format!("{ty}::BROADCAST"), true) |
| 72 | + } else if with_consts && &bytes == Ipv4Addr::UNSPECIFIED.as_octets() { |
| 73 | + (format!("{ty}::UNSPECIFIED"), true) |
| 74 | + } else { |
| 75 | + ( |
| 76 | + format!("{ty}::new({}, {}, {}, {})", bytes[0], bytes[1], bytes[2], bytes[3]), |
| 77 | + false, |
| 78 | + ) |
| 79 | + } |
| 80 | + }) |
| 81 | + } else { |
| 82 | + Some(( |
| 83 | + format!("[{}, {}, {}, {}].into()", bytes[0], bytes[1], bytes[2], bytes[3]), |
| 84 | + false, |
| 85 | + )) |
| 86 | + } |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +/// Try parsing `addr` as an IPv4 address and return its octets |
| 91 | +fn as_ipv4_octets(addr: Symbol) -> Option<[u8; 4]> { |
| 92 | + addr.as_str().parse::<Ipv4Addr>().ok().map(|addr| *addr.as_octets()) |
| 93 | +} |
| 94 | + |
| 95 | +/// Suggests a replacement if `addr` is a correct IPv6 address, with: |
| 96 | +/// - the replacement string |
| 97 | +/// - a boolean that indicates if a typed constant is used |
| 98 | +/// |
| 99 | +/// Replacement will either be: |
| 100 | +/// - `T::CONSTANT` |
| 101 | +/// - `Ipv6Addr::CONSTANT` if no `explicit_type` is defined |
| 102 | +/// - `T::new(…)` |
| 103 | +/// - `[u16; 8].into()` if no `explicit_type` is defined |
| 104 | +/// |
| 105 | +/// See [`ipv4_subst()`] for more details. |
| 106 | +fn ipv6_subst( |
| 107 | + cx: &LateContext<'_>, |
| 108 | + addr: Symbol, |
| 109 | + with_consts: bool, |
| 110 | + explicit_type: Option<QPath<'_>>, |
| 111 | +) -> Option<(String, bool)> { |
| 112 | + as_ipv6_segments(addr).and_then(|segments| { |
| 113 | + if let Some(qpath) = explicit_type { |
| 114 | + qpath.span().with_source_text(cx, |ty| { |
| 115 | + if with_consts && segments == Ipv6Addr::LOCALHOST.segments() { |
| 116 | + (format!("{ty}::LOCALHOST"), true) |
| 117 | + } else if with_consts && explicit_type.is_some() && segments == Ipv6Addr::UNSPECIFIED.segments() { |
| 118 | + (format!("{ty}::UNSPECIFIED"), true) |
| 119 | + } else { |
| 120 | + (format!("{ty}::new({})", segments_to_string(&segments)), false) |
| 121 | + } |
| 122 | + }) |
| 123 | + } else { |
| 124 | + Some((format!("[{}].into()", segments_to_string(&segments)), false)) |
| 125 | + } |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +/// Try parsing `addr` as an IPv6 address and return its 16-bit segments |
| 130 | +fn as_ipv6_segments(addr: Symbol) -> Option<[u16; 8]> { |
| 131 | + addr.as_str().parse().ok().as_ref().map(Ipv6Addr::segments) |
| 132 | +} |
| 133 | + |
| 134 | +/// Return the `segments` separated by commas, in a common format for IPv6 addresses |
| 135 | +fn segments_to_string(segments: &[u16; 8]) -> String { |
| 136 | + segments |
| 137 | + .map(|n| if n < 2 { n.to_string() } else { format!("{n:#x}") }) |
| 138 | + .join(", ") |
| 139 | +} |
| 140 | + |
| 141 | +/// Suggests a replacement if `addr` is a correct IPv6 address, with: |
| 142 | +/// - the replacement string |
| 143 | +/// - the entity that was detected |
| 144 | +/// |
| 145 | +/// `explicit_type` refers to `IpAddr`, and not to the content of one of the variants |
| 146 | +/// (`IpAddr::V4` or `IpAddr::V6`). The use of constants from `Ipv4Addr` or `Ipv6Addr` |
| 147 | +/// will not be proposed because we do not know if those types are imported in the scope. |
| 148 | +fn ip_subst(cx: &LateContext<'_>, addr: Symbol, explicit_type: Option<QPath<'_>>) -> Option<(String, &'static str)> { |
| 149 | + if let Some([a0, a1, a2, a3]) = as_ipv4_octets(addr) { |
| 150 | + Some(( |
| 151 | + if let Some(qpath) = explicit_type { |
| 152 | + qpath |
| 153 | + .span() |
| 154 | + .with_source_text(cx, |ty| format!("{ty}::V4([{a0}, {a1}, {a2}, {a3}].into())"))? |
| 155 | + } else { |
| 156 | + format!("[{a0}, {a1}, {a2}, {a3}].into()") |
| 157 | + }, |
| 158 | + IPV4_ENTITY, |
| 159 | + )) |
| 160 | + } else if let Some(segments) = as_ipv6_segments(addr) { |
| 161 | + Some(( |
| 162 | + if let Some(qpath) = explicit_type { |
| 163 | + qpath |
| 164 | + .span() |
| 165 | + .with_source_text(cx, |ty| format!("{ty}::V6([{}].into())", segments_to_string(&segments)))? |
| 166 | + } else { |
| 167 | + format!("[{}].into()", segments_to_string(&segments)) |
| 168 | + }, |
| 169 | + IPV6_ENTITY, |
| 170 | + )) |
| 171 | + } else { |
| 172 | + None |
| 173 | + } |
| 174 | +} |
0 commit comments