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