|
| 1 | +use std::borrow::Cow; |
| 2 | +use std::net::{Ipv4Addr, Ipv6Addr}; |
| 3 | + |
| 4 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 5 | +use clippy_utils::msrvs::{self, Msrv}; |
| 6 | +use clippy_utils::source::SpanRangeExt; |
| 7 | +use clippy_utils::sym; |
| 8 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 9 | +use rustc_ast::LitKind; |
| 10 | +use rustc_errors::Applicability; |
| 11 | +use rustc_hir::{Expr, ExprKind}; |
| 12 | +use rustc_lint::LateContext; |
| 13 | +use rustc_span::Symbol; |
| 14 | + |
| 15 | +use super::PARSED_STRING_LITERALS; |
| 16 | + |
| 17 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, msrv: Msrv) { |
| 18 | + if let ExprKind::Lit(lit) = recv.kind |
| 19 | + && let LitKind::Str(str, _) = lit.node |
| 20 | + { |
| 21 | + let consts_available = || msrv.meets(cx, msrvs::IPADDR_CONSTANTS); |
| 22 | + let ty = cx.typeck_results().expr_ty(expr); |
| 23 | + if is_type_diagnostic_item(cx, ty, sym::Ipv4Addr) |
| 24 | + && let Some(sugg) = ipv4_subst(str, consts_available()) |
| 25 | + { |
| 26 | + maybe_emit_lint(cx, expr, sugg.is_borrowed(), sugg); |
| 27 | + } else if is_type_diagnostic_item(cx, ty, sym::Ipv6Addr) |
| 28 | + && let Some(sugg) = ipv6_subst(str, consts_available()) |
| 29 | + { |
| 30 | + maybe_emit_lint(cx, expr, sugg.is_borrowed(), sugg); |
| 31 | + } else if is_type_diagnostic_item(cx, ty, sym::IpAddr) { |
| 32 | + let with_consts = consts_available(); |
| 33 | + if let Some(sugg) = ipv4_subst(str, with_consts) { |
| 34 | + maybe_emit_lint(cx, expr, sugg.is_borrowed(), format!("IpAddr::V4({sugg})").into()); |
| 35 | + } else if let Some(sugg) = ipv6_subst(str, with_consts) { |
| 36 | + maybe_emit_lint(cx, expr, sugg.is_borrowed(), format!("IpAddr::V6({sugg})").into()); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Suggests a replacement if `addr` is a correct IPv4 address |
| 43 | +fn ipv4_subst(addr: Symbol, with_consts: bool) -> Option<Cow<'static, str>> { |
| 44 | + addr.as_str().parse().ok().map(|ipv4: Ipv4Addr| { |
| 45 | + if with_consts && ipv4.as_octets() == &[127, 0, 0, 1] { |
| 46 | + "Ipv4Addr::LOCALHOST".into() |
| 47 | + } else if with_consts && ipv4.is_broadcast() { |
| 48 | + "Ipv4Addr::BROADCAST".into() |
| 49 | + } else if with_consts && ipv4.is_unspecified() { |
| 50 | + "Ipv4Addr::UNSPECIFIED".into() |
| 51 | + } else { |
| 52 | + let ipv4 = ipv4.as_octets(); |
| 53 | + format!("Ipv4Addr::new({}, {}, {}, {})", ipv4[0], ipv4[1], ipv4[2], ipv4[3]).into() |
| 54 | + } |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +/// Suggests a replacement if `addr` is a correct IPv6 address |
| 59 | +fn ipv6_subst(addr: Symbol, with_consts: bool) -> Option<Cow<'static, str>> { |
| 60 | + addr.as_str().parse().ok().map(|ipv6: Ipv6Addr| { |
| 61 | + if with_consts && ipv6.is_loopback() { |
| 62 | + "Ipv6Addr::LOCALHOST".into() |
| 63 | + } else if with_consts && ipv6.is_unspecified() { |
| 64 | + "Ipv6Addr::UNSPECIFIED".into() |
| 65 | + } else { |
| 66 | + format!( |
| 67 | + "Ipv6Addr::new([{}])", |
| 68 | + ipv6.segments() |
| 69 | + .map(|n| if n < 2 { n.to_string() } else { format!("{n:#x}") }) |
| 70 | + .join(", ") |
| 71 | + ) |
| 72 | + .into() |
| 73 | + } |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +/// Emit the lint if the length of `sugg` is no longer than the original `expr` span, or if `force` |
| 78 | +/// is set. |
| 79 | +fn maybe_emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, force: bool, sugg: Cow<'_, str>) { |
| 80 | + if force || expr.span.check_source_text(cx, |snip| snip.len() >= sugg.len()) { |
| 81 | + span_lint_and_sugg( |
| 82 | + cx, |
| 83 | + PARSED_STRING_LITERALS, |
| 84 | + expr.span, |
| 85 | + "unnecessary runtime parsing of IP address", |
| 86 | + "use", |
| 87 | + sugg.into(), |
| 88 | + Applicability::MaybeIncorrect, |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments