|
| 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::{std_or_core, 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, warn_about}; |
| 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(explicit_type) = explicit_type |
| 40 | + && let Some((sugg, typed_const, entity, app)) = ip_subst(cx, lit, explicit_type) |
| 41 | + { |
| 42 | + maybe_emit_lint(cx, expr, typed_const, entity, sugg, app); |
| 43 | + } else { |
| 44 | + warn_about( |
| 45 | + cx, |
| 46 | + expr, |
| 47 | + "an IP address", |
| 48 | + format!( |
| 49 | + "use a variant of the `{}::net::IpAddr` enum instead", |
| 50 | + std_or_core(cx).unwrap_or("std") |
| 51 | + ), |
| 52 | + ); |
| 53 | + } |
| 54 | + }, |
| 55 | + _ => unreachable!(), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Suggests a replacement if `addr` is a correct IPv4 address, with: |
| 60 | +/// - the replacement string |
| 61 | +/// - a boolean that indicates if a typed constant is used |
| 62 | +/// - the applicability |
| 63 | +/// |
| 64 | +/// The replacement will be `T::CONSTANT` if a constant is detected, |
| 65 | +/// where `T` is either `explicit_type` if provided, or `Ipv4Addr` |
| 66 | +/// otherwise. |
| 67 | +/// |
| 68 | +/// In other cases, when the type has been explicitly given as `T`, the |
| 69 | +/// `T::new()` constructor will be used. If no type has been explicitly |
| 70 | +/// given, then `[u8; 4].into()` will be used as the context should |
| 71 | +/// already provide the proper information. This allows us not to use |
| 72 | +/// a type name which might not be available in the current scope. |
| 73 | +fn ipv4_subst( |
| 74 | + cx: &LateContext<'_>, |
| 75 | + addr: Symbol, |
| 76 | + with_consts: bool, |
| 77 | + explicit_type: Option<hir::Ty<'_, AmbigArg>>, |
| 78 | +) -> Option<(String, bool, Applicability)> { |
| 79 | + addr.as_str().parse().ok().map(|ipv4: Ipv4Addr| { |
| 80 | + let bytes = ipv4.as_octets(); |
| 81 | + if let Some(ty) = explicit_type { |
| 82 | + let mut app = Applicability::MachineApplicable; |
| 83 | + let ty = snippet_with_applicability(cx, ty.span, "_", &mut app); |
| 84 | + if with_consts && bytes == &[127, 0, 0, 1] { |
| 85 | + (format!("{ty}::LOCALHOST"), true, app) |
| 86 | + } else if with_consts && ipv4.is_broadcast() { |
| 87 | + (format!("{ty}::BROADCAST"), true, app) |
| 88 | + } else if with_consts && ipv4.is_unspecified() { |
| 89 | + (format!("{ty}::UNSPECIFIED"), true, app) |
| 90 | + } else { |
| 91 | + ( |
| 92 | + format!("{ty}::new({}, {}, {}, {})", bytes[0], bytes[1], bytes[2], bytes[3]), |
| 93 | + false, |
| 94 | + app, |
| 95 | + ) |
| 96 | + } |
| 97 | + } else { |
| 98 | + ( |
| 99 | + format!("[{}, {}, {}, {}].into()", bytes[0], bytes[1], bytes[2], bytes[3]), |
| 100 | + false, |
| 101 | + Applicability::MachineApplicable, |
| 102 | + ) |
| 103 | + } |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +/// Suggests a replacement if `addr` is a correct IPv6 address, with: |
| 108 | +/// - the replacement string |
| 109 | +/// - a boolean that indicates if a typed constant is used |
| 110 | +/// - the applicability |
| 111 | +/// |
| 112 | +/// Replacement will either be: |
| 113 | +/// - `T::CONSTANT` |
| 114 | +/// - `Ipv6Addr::CONSTANT` if no `explicit_type` is defined |
| 115 | +/// - `T::new(…)` |
| 116 | +/// - `[u16; 8].into()` if no `explicit_type` is defined |
| 117 | +/// |
| 118 | +/// See [`ipv4_subst()`] for more details. |
| 119 | +fn ipv6_subst( |
| 120 | + cx: &LateContext<'_>, |
| 121 | + addr: Symbol, |
| 122 | + with_consts: bool, |
| 123 | + explicit_type: Option<hir::Ty<'_, AmbigArg>>, |
| 124 | +) -> Option<(String, bool, Applicability)> { |
| 125 | + addr.as_str().parse().ok().map(|ipv6: Ipv6Addr| { |
| 126 | + let addr = || { |
| 127 | + ipv6.segments() |
| 128 | + .map(|n| if n < 2 { n.to_string() } else { format!("{n:#x}") }) |
| 129 | + .join(", ") |
| 130 | + }; |
| 131 | + if let Some(ty) = explicit_type { |
| 132 | + let mut app = Applicability::MachineApplicable; |
| 133 | + let ty = snippet_with_applicability(cx, ty.span, "_", &mut app); |
| 134 | + if with_consts && ipv6.is_loopback() { |
| 135 | + (format!("{ty}::LOCALHOST"), true, app) |
| 136 | + } else if with_consts && explicit_type.is_some() && ipv6.is_unspecified() { |
| 137 | + (format!("{ty}::UNSPECIFIED"), true, app) |
| 138 | + } else { |
| 139 | + (format!("{ty}::new({})", addr()), false, app) |
| 140 | + } |
| 141 | + } else { |
| 142 | + (format!("[{}].into()", addr()), false, Applicability::MachineApplicable) |
| 143 | + } |
| 144 | + }) |
| 145 | +} |
| 146 | + |
| 147 | +/// Suggests a replacement if `addr` is a correct IPv6 address, with: |
| 148 | +/// - the replacement string |
| 149 | +/// - a boolean that indicates if a typed constant is used |
| 150 | +/// - the entity that was detected |
| 151 | +/// - the applicability |
| 152 | +/// |
| 153 | +/// `explicit_type` refers to `IpAddr`, and not to the content of one of the variants |
| 154 | +/// (`IpAddr::V4` or `IpAddr::V6`). The use of constants from `Ipv4Addr` or `Ipv6Addr` |
| 155 | +/// will not be proposed because we do not know if those types are imported in the scope. |
| 156 | +fn ip_subst( |
| 157 | + cx: &LateContext<'_>, |
| 158 | + addr: Symbol, |
| 159 | + explicit_type: hir::Ty<'_, AmbigArg>, |
| 160 | +) -> Option<(String, bool, &'static str, Applicability)> { |
| 161 | + let (sugg, typed_const, entity, variant) = if let Some((sugg, typed_const, _)) = ipv4_subst(cx, addr, false, None) { |
| 162 | + (sugg, typed_const, IPV4_ENTITY, "V4") |
| 163 | + } else if let Some((sugg, typed_const, _)) = ipv6_subst(cx, addr, false, None) { |
| 164 | + (sugg, typed_const, IPV6_ENTITY, "V6") |
| 165 | + } else { |
| 166 | + return None; |
| 167 | + }; |
| 168 | + // If a typed constant has been used, we cannot know for sure that the `Ipv4Addr`/`Ipv6Addr` is |
| 169 | + // present in the current scope because the explicit type if present would be `IpAddr`. |
| 170 | + let mut app = if typed_const { |
| 171 | + Applicability::MaybeIncorrect |
| 172 | + } else { |
| 173 | + Applicability::MachineApplicable |
| 174 | + }; |
| 175 | + Some(( |
| 176 | + format!( |
| 177 | + "{}::{variant}({sugg})", |
| 178 | + snippet_with_applicability(cx, explicit_type.span, "_", &mut app) |
| 179 | + ), |
| 180 | + typed_const, |
| 181 | + entity, |
| 182 | + app, |
| 183 | + )) |
| 184 | +} |
0 commit comments