Skip to content

Conversation

samueltardieu
Copy link
Member

@samueltardieu samueltardieu commented May 13, 2025

This lint detects parsing of string literals into IP addresses when they are known correct.

changelog: [parsed_string_literals]: new lint

Inspired by this Zulip comment.

@rustbot
Copy link
Collaborator

rustbot commented May 13, 2025

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 13, 2025
@Jarcho
Copy link
Contributor

Jarcho commented May 14, 2025

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.

@samueltardieu
Copy link
Member Author

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

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 14, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 14, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

This comment has been minimized.

@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch 2 times, most recently from ceb8d7f to 938d056 Compare May 21, 2025 20:50
@samueltardieu
Copy link
Member Author

Rebased to take advantage of the new diagnostic items.

What about clippy::parsed_string_literals as a name?

@Jarcho
Copy link
Contributor

Jarcho commented May 21, 2025

That sounds like a good name.

@Jarcho
Copy link
Contributor

Jarcho commented May 21, 2025

@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch from 938d056 to ead8755 Compare May 21, 2025 22:07
@samueltardieu samueltardieu changed the title unnecessary_ip_addr_parse: new lint parsed_string_literals: new lint May 21, 2025
@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch from ead8755 to 67212c0 Compare May 24, 2025 09:15
@samueltardieu
Copy link
Member Author

Amended to avoid a useless string allocation during diagnostic production (and version number).

@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch 2 times, most recently from 77fc5d0 to a1dac58 Compare May 25, 2025 17:29
@samueltardieu
Copy link
Member Author

Added numerical/boolean types as well.

@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch 2 times, most recently from 6fb6b92 to 9c05182 Compare May 26, 2025 09:00
@samueltardieu samueltardieu requested a review from Jarcho May 28, 2025 13:59
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 28, 2025
@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch 2 times, most recently from b0fa40c to fc3dd92 Compare May 29, 2025 20:14
@samueltardieu
Copy link
Member Author

I've leveraged the existing clippy_utils::source::str_literal_to_char_literal to ensure that the original representation of a character literal is preserved when possible.

@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch from fc3dd92 to 42a298b Compare May 29, 2025 20:16
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Sep 30, 2025
@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch 2 times, most recently from 2c22b5f to 588007e Compare September 30, 2025 21:54
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Sep 30, 2025
try_parse::<$ty>(
lit,
Some(stringify!($ty)),
explicit_type.map(|ty| snippet_with_applicability(cx, ty.span, "_", &mut app))
Copy link
Contributor

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.

Copy link
Member Author

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.

@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch from 588007e to e8f3f64 Compare October 1, 2025 08:02
@samueltardieu samueltardieu requested a review from Jarcho October 1, 2025 08:03
@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch from e8f3f64 to 6257caf Compare October 6, 2025 22:31
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch from 6257caf to 6fccef3 Compare October 9, 2025 22:21
@rustbot

This comment has been minimized.

@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch from 6fccef3 to 463acf7 Compare October 11, 2025 07:37
@rustbot
Copy link
Collaborator

rustbot commented Oct 11, 2025

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.

@samueltardieu
Copy link
Member Author

Anything needs to be modified?

@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch from 463acf7 to aa94d54 Compare October 15, 2025 15:38
@samueltardieu samueltardieu requested a review from Jarcho October 15, 2025 15:39
@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch from aa94d54 to 16dd9db Compare October 15, 2025 15:43
@samueltardieu
Copy link
Member Author

(second push was to update a test comment now that we don't use type from let)

This lint detects parsing of string literals into primitive types or
IP addresses when they are known correct.
@samueltardieu samueltardieu force-pushed the unnecessary-ip-addr-parse branch from 16dd9db to f092032 Compare October 16, 2025 17:23
@samueltardieu
Copy link
Member Author

samueltardieu commented Oct 16, 2025

Changes in the recent push:

  • Special inf and nan (with any case, and with an optional leading + or -) are now properly handled through the type's associated constants, thanks to @y21 for noticing this.
  • Parsing f16 has been added as well – only f128 doesn't support scanning and formatting by default.
  • Category has been set to complexity as it looks this is the way the Zulip poll is trending.
  • Clippy version has been set to "1.93.0" as today was the last sync before the "1.92.0" Rust version is cut from the master branch on next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants