-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
base: master
Are you sure you want to change the base?
parsed_string_literals
: new lint
#14794
Conversation
Is there any reason to limit linting this to just ip addresses? Any call to parse on a string literal has the same issue regardless of the type. The suggestion would have to be specialised per type so it could only exist for a subset of cases. Possibly a config to ignore specific types, but I'm not sure if this would be needed. |
I guess this could be extended, but at this stage I'm not sure we should make a generic lint for this. Specialized lints can be collectively renamed into a more generic one if we don't see one of them being disabled in isolation. I'd be more comfortable with a separate lint for the time being. In any case, I'll suspend this PR until rust-lang/rust#140976 is merged and synced to Clippy (hopefully tomorrow). @rustbot author |
Reminder, once the PR becomes ready for a review, use |
This comment has been minimized.
This comment has been minimized.
ceb8d7f
to
938d056
Compare
Rebased to take advantage of the new diagnostic items. What about |
That sounds like a good name. |
938d056
to
ead8755
Compare
unnecessary_ip_addr_parse
: new lintparsed_string_literals
: new lint
ead8755
to
67212c0
Compare
Amended to avoid a useless string allocation during diagnostic production (and version number). |
77fc5d0
to
a1dac58
Compare
Added numerical/boolean types as well. |
6fb6b92
to
9c05182
Compare
b0fa40c
to
fc3dd92
Compare
I've leveraged the existing |
fc3dd92
to
42a298b
Compare
Ping @Jarcho |
This comment has been minimized.
This comment has been minimized.
56bae3b
to
cd7b821
Compare
This comment has been minimized.
This comment has been minimized.
cd7b821
to
dbff4dc
Compare
Rebased (first push), and updated the version to 1.92.0 (second push). |
match ty.kind() {
ty::UInt(_) => check_uint,
ty::Int(_) => check_int,
ty::Bool => check_bool,
ty::Float(_) => check_float,
ty::Adt(..) => check_ipaddr,
} For integers and floats |
dbff4dc
to
8cecdb0
Compare
This comment has been minimized.
This comment has been minimized.
I've reworked completely this lint, and reorganized it with submodules so that it is not only easier to read, but also it should be much easier to add new types beyond primitive types and ip addresses. |
8cecdb0
to
ae2950d
Compare
clippy_lints/src/methods/parsed_string_literals/primitive_types.rs
Outdated
Show resolved
Hide resolved
} else if let Node::LetStmt(let_stmt) = cx.tcx.parent_hir_node(expr.hir_id) | ||
&& let Some(ty) = let_stmt.ty | ||
&& let Some(ty) = ty.try_as_ambig_ty() | ||
&& matches!(ty.kind, hir::TyKind::Path(_)) | ||
{ | ||
Some(*ty) | ||
} else { | ||
None | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you looking for the parent statement here? If the type isn't on the parse
call itself then type inference will work just fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I hadn't noticed that IpAddr
could get direct conversion from [u8; 4]
(IPv4) or [u16; 8]
(IPv6), and thought I had to go through one intermediate value. I will fix this, this will be even better then.
2c22b5f
to
588007e
Compare
try_parse::<$ty>( | ||
lit, | ||
Some(stringify!($ty)), | ||
explicit_type.map(|ty| snippet_with_applicability(cx, ty.span, "_", &mut app)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A snippet isn't needed here and it's the slowest available option. The fastest thing to do is match the type down to a single segment path and compare the symbol.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, and .with_source_text()
is used whenever possible at lint emission type to limit the number of copies.
588007e
to
e8f3f64
Compare
This lint detects parsing of string literals into primitive types or IP addresses when they are known correct.
e8f3f64
to
6257caf
Compare
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This lint detects parsing of string literals into IP addresses when they are known correct.
changelog: [
parsed_string_literals
]: new lintInspired by this Zulip comment.