Skip to content

parsed_string_literals: new lint #14794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6155,6 +6155,7 @@ Released 2018-09-13
[`panic_params`]: https://rust-lang.github.io/rust-clippy/master/index.html#panic_params
[`panicking_overflow_checks`]: https://rust-lang.github.io/rust-clippy/master/index.html#panicking_overflow_checks
[`panicking_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#panicking_unwrap
[`parsed_string_literals`]: https://rust-lang.github.io/rust-clippy/master/index.html#parsed_string_literals
[`partial_pub_fields`]: https://rust-lang.github.io/rust-clippy/master/index.html#partial_pub_fields
[`partialeq_ne_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_ne_impl
[`partialeq_to_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_to_none
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
crate::methods::OPTION_MAP_OR_NONE_INFO,
crate::methods::OR_FUN_CALL_INFO,
crate::methods::OR_THEN_UNWRAP_INFO,
crate::methods::PARSED_STRING_LITERALS_INFO,
crate::methods::PATH_BUF_PUSH_OVERWRITE_INFO,
crate::methods::PATH_ENDS_WITH_EXT_INFO,
crate::methods::RANGE_ZIP_WITH_LEN_INFO,
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![feature(array_windows)]
#![feature(box_patterns)]
#![feature(cow_is_borrowed)]
#![feature(macro_metavar_expr_concat)]
#![feature(f128)]
#![feature(f16)]
#![feature(if_let_guard)]
#![feature(ip_as_octets)]
#![feature(iter_intersperse)]
#![feature(iter_partition_in_place)]
#![feature(never_type)]
Expand Down
35 changes: 35 additions & 0 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ mod option_map_or_none;
mod option_map_unwrap_or;
mod or_fun_call;
mod or_then_unwrap;
mod parsed_string_literals;
mod path_buf_push_overwrite;
mod path_ends_with_ext;
mod range_zip_with_len;
Expand Down Expand Up @@ -4528,6 +4529,36 @@ declare_clippy_lint! {
"detect swap with a temporary value"
}

declare_clippy_lint! {
/// ### What it does
/// Checks for parsing string literals into types from the standard library
///
/// ### Why is this bad?
/// Parsing known values at runtime consumes resources and forces to
/// unwrap the `Ok()` variant returned by `parse()`.
///
/// ### Example
/// ```no_run
/// use std::net::Ipv4Addr;
///
/// let number = "123".parse::<u32>().unwrap();
/// let addr1: Ipv4Addr = "10.2.3.4".parse().unwrap();
/// let addr2: Ipv4Addr = "127.0.0.1".parse().unwrap();
/// ```
/// Use instead:
/// ```no_run
/// use std::net::Ipv4Addr;
///
/// let number = 123_u32;
/// let addr1: Ipv4Addr = Ipv4Addr::new(10, 2, 3, 4);
/// let addr2: Ipv4Addr = Ipv4Addr::LOCALHOST;
/// ```
#[clippy::version = "1.89.0"]
pub PARSED_STRING_LITERALS,
perf,
"known-correct literal IP address parsing"
}

#[expect(clippy::struct_excessive_bools)]
pub struct Methods {
avoid_breaking_exported_api: bool,
Expand Down Expand Up @@ -4706,6 +4737,7 @@ impl_lint_pass!(Methods => [
MANUAL_CONTAINS,
IO_OTHER_ERROR,
SWAP_WITH_TEMPORARY,
PARSED_STRING_LITERALS,
]);

/// Extracts a method call name, args, and `Span` of the method name.
Expand Down Expand Up @@ -5420,6 +5452,9 @@ impl Methods {
Some((sym::or, recv, [or_arg], or_span, _)) => {
or_then_unwrap::check(cx, expr, recv, or_arg, or_span);
},
Some((sym::parse, recv, [], _, _)) => {
parsed_string_literals::check(cx, expr, recv, self.msrv);
},
_ => {},
}
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
Expand Down
173 changes: 173 additions & 0 deletions clippy_lints/src/methods/parsed_string_literals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
use std::borrow::Cow;
use std::fmt::Display;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::str::FromStr;

use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::{SpanRangeExt, str_literal_to_char_literal};
use clippy_utils::sym;
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty};
use rustc_span::Symbol;

use super::PARSED_STRING_LITERALS;

pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, msrv: Msrv) {
if let ExprKind::Lit(lit) = recv.kind
&& let LitKind::Str(lit, _) = lit.node
{
let ty = cx.typeck_results().expr_ty(expr);
if !check_primitive(cx, expr, lit, ty, recv) {
check_ipaddr(cx, expr, lit, ty, msrv);
}
}
}

fn check_primitive(cx: &LateContext<'_>, expr: &Expr<'_>, lit: Symbol, ty: Ty<'_>, strlit: &Expr<'_>) -> bool {
macro_rules! number {
($kind:ident, $expr:expr, $msg:expr, [$($subkind:ident => $ty:ident),*$(,)?]$(,)?) => {{
match $expr {
$(ty::$kind::$subkind => (try_parse::<$ty>(lit, Some(stringify!($ty))), $msg),)*
#[allow(unreachable_patterns)]
_ => return false,
}
}};
}

let mut app = Applicability::MachineApplicable;
if let (Some(subst), entity) = match ty.kind() {
ty::Int(int_ty) => number!(IntTy, int_ty, "a signed integer",
[Isize => isize, I8 => i8, I16 => i16, I32 => i32, I64 => i64, I128 => i128]),
ty::Uint(uint_ty) => number!(UintTy, uint_ty, "an unsigned integer",
[Usize => usize, U8 => u8, U16 => u16, U32 => u32, U64 => u64, U128 => u128]),
// FIXME: ignore `f16` and `f128` for now as they cannot use the default formatter
ty::Float(float_ty) => number!(FloatTy, float_ty, "a real number",
[F32 => f32, F64 => f64]),
ty::Bool => (try_parse::<bool>(lit, None), "a boolean"),
ty::Char => (str_literal_to_char_literal(cx, strlit, &mut app, false), "a character"),
_ => return false,
} {
maybe_emit_lint(cx, expr, false, entity, subst.into(), app);
}
true
}

fn check_ipaddr(cx: &LateContext<'_>, expr: &Expr<'_>, lit: Symbol, ty: Ty<'_>, msrv: Msrv) {
static IPV4_ENTITY: &str = "an IPv4 address";
static IPV6_ENTITY: &str = "an IPv6 address";
let ipaddr_consts_available = || msrv.meets(cx, msrvs::IPADDR_CONSTANTS);
if is_type_diagnostic_item(cx, ty, sym::Ipv4Addr)
&& let Some(sugg) = ipv4_subst(lit, ipaddr_consts_available())
{
maybe_emit_lint(
cx,
expr,
sugg.is_borrowed(),
IPV4_ENTITY,
sugg,
Applicability::MaybeIncorrect,
);
} else if is_type_diagnostic_item(cx, ty, sym::Ipv6Addr)
&& let Some(sugg) = ipv6_subst(lit, ipaddr_consts_available())
{
maybe_emit_lint(
cx,
expr,
sugg.is_borrowed(),
IPV6_ENTITY,
sugg,
Applicability::MaybeIncorrect,
);
} else if is_type_diagnostic_item(cx, ty, sym::IpAddr) {
let with_consts = ipaddr_consts_available();
if let Some(sugg) = ipv4_subst(lit, with_consts) {
maybe_emit_lint(
cx,
expr,
sugg.is_borrowed(),
IPV4_ENTITY,
format!("IpAddr::V4({sugg})").into(),
Applicability::MaybeIncorrect,
);
} else if let Some(sugg) = ipv6_subst(lit, with_consts) {
maybe_emit_lint(
cx,
expr,
sugg.is_borrowed(),
IPV6_ENTITY,
format!("IpAddr::V6({sugg})").into(),
Applicability::MaybeIncorrect,
);
}
}
}

/// Suggests a replacement if `addr` is a correct IPv4 address
fn ipv4_subst(addr: Symbol, with_consts: bool) -> Option<Cow<'static, str>> {
addr.as_str().parse().ok().map(|ipv4: Ipv4Addr| {
if with_consts && ipv4.as_octets() == &[127, 0, 0, 1] {
"Ipv4Addr::LOCALHOST".into()
} else if with_consts && ipv4.is_broadcast() {
"Ipv4Addr::BROADCAST".into()
} else if with_consts && ipv4.is_unspecified() {
"Ipv4Addr::UNSPECIFIED".into()
} else {
let ipv4 = ipv4.as_octets();
format!("Ipv4Addr::new({}, {}, {}, {})", ipv4[0], ipv4[1], ipv4[2], ipv4[3]).into()
}
})
}

/// Suggests a replacement if `addr` is a correct IPv6 address
fn ipv6_subst(addr: Symbol, with_consts: bool) -> Option<Cow<'static, str>> {
addr.as_str().parse().ok().map(|ipv6: Ipv6Addr| {
if with_consts && ipv6.is_loopback() {
"Ipv6Addr::LOCALHOST".into()
} else if with_consts && ipv6.is_unspecified() {
"Ipv6Addr::UNSPECIFIED".into()
} else {
format!(
"Ipv6Addr::new([{}])",
ipv6.segments()
.map(|n| if n < 2 { n.to_string() } else { format!("{n:#x}") })
.join(", ")
)
.into()
}
})
}

fn try_parse<T: FromStr + Display>(lit: Symbol, suffix: Option<&str>) -> Option<String> {
lit.as_str()
.parse::<T>()
.ok()
.map(|_| suffix.map_or_else(|| lit.to_string(), |suffix| format!("{lit}_{suffix}")))
}

/// Emit the lint if the length of `sugg` is no longer than the original `expr` span, or if `force`
/// is set.
fn maybe_emit_lint(
cx: &LateContext<'_>,
expr: &Expr<'_>,
force: bool,
entity: &str,
sugg: Cow<'_, str>,
applicability: Applicability,
) {
if force || expr.span.check_source_text(cx, |snip| snip.len() >= sugg.len()) {
span_lint_and_sugg(
cx,
PARSED_STRING_LITERALS,
expr.span,
format!("unnecessary runtime parsing of {entity}"),
"use",
sugg.into(),
applicability,
);
}
}
2 changes: 1 addition & 1 deletion clippy_utils/src/msrvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ msrv_aliases! {
1,33,0 { UNDERSCORE_IMPORTS }
1,32,0 { CONST_IS_POWER_OF_TWO }
1,31,0 { OPTION_REPLACE }
1,30,0 { ITERATOR_FIND_MAP, TOOL_ATTRIBUTES }
1,30,0 { ITERATOR_FIND_MAP, TOOL_ATTRIBUTES, IPADDR_CONSTANTS }
1,29,0 { ITER_FLATTEN }
1,28,0 { FROM_BOOL, REPEAT_WITH, SLICE_FROM_REF }
1,27,0 { ITERATOR_TRY_FOLD }
Expand Down
83 changes: 83 additions & 0 deletions tests/ui/parsed_string_literals.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#![warn(clippy::parsed_string_literals)]

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

fn main() {
_ = Ipv4Addr::new(137, 194, 161, 2);
//~^ parsed_string_literals

_ = Ipv4Addr::LOCALHOST;
//~^ parsed_string_literals

_ = Ipv4Addr::BROADCAST;
//~^ parsed_string_literals

_ = Ipv4Addr::UNSPECIFIED;
//~^ parsed_string_literals

// Wrong address family
_ = "::1".parse::<Ipv4Addr>().unwrap();
_ = "127.0.0.1".parse::<Ipv6Addr>().unwrap();

_ = Ipv6Addr::LOCALHOST;
//~^ parsed_string_literals

_ = Ipv6Addr::UNSPECIFIED;
//~^ parsed_string_literals

_ = IpAddr::V6(Ipv6Addr::LOCALHOST);
//~^ parsed_string_literals

_ = IpAddr::V6(Ipv6Addr::UNSPECIFIED);
//~^ parsed_string_literals

// The substition text would be larger than the original and wouldn't use constants
_ = "2a04:8ec0:0:47::131".parse::<Ipv6Addr>().unwrap();
_ = "2a04:8ec0:0:47::131".parse::<IpAddr>().unwrap();

_ = true;
//~^ parsed_string_literals
_ = false;
//~^ parsed_string_literals

let _: i64 = -17_i64;
//~^ parsed_string_literals
_ = 10_usize;
//~^ parsed_string_literals
_ = 1.23_f32;
//~^ parsed_string_literals
_ = 1.2300_f32;
//~^ parsed_string_literals
_ = 'c';
//~^ parsed_string_literals
_ = '"';
//~^ parsed_string_literals
_ = '\'';
//~^ parsed_string_literals

// Check that the original form is preserved ('🦀' == '\u{1f980}')
_ = '\u{1f980}';
//~^ parsed_string_literals
_ = '🦀';
//~^ parsed_string_literals

// Do not lint invalid values
_ = "-10".parse::<usize>().unwrap();

// Do not lint content or code coming from macros
macro_rules! mac {
(str) => {
"10"
};
(parse $l:literal) => {
$l.parse::<u32>().unwrap()
};
}
_ = mac!(str).parse::<u32>().unwrap();
_ = mac!(parse "10");
}

#[clippy::msrv = "1.29"]
fn msrv_under() {
_ = "::".parse::<IpAddr>().unwrap();
}
Loading