-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add new lint, checking for .to_string().parse().unwrap()
#14214
base: master
Are you sure you want to change the base?
Add new lint, checking for .to_string().parse().unwrap()
#14214
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
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.
The commit message mentions .unwrap()
, but the presence of unwrap()
is never checked.
I am a bit torn about this lint, as many uses found on GitHub look legitimate.
use rustc_lint::LateContext; | ||
use rustc_span::Span; | ||
|
||
pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, span: Span) { |
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.
You should at least check that the intermediate type is String
.
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.
Wouldn't it make more sense to check that
- the receiver of
to_string
isn't&str
, sinceimpl ToString for &str
+str::parse
is not actually suspicious - the receiver of
parse
is&str
(delegating toFromStr
)
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.
On second thought, impl ToString for &str
+ str::parse
is indeed suspicious, as the allocation as owned string is unnecessary when parsing only requires a string slice.
Am I missing something in the search result I had deemed not suspicious?
It should be using <Captures as Index>::index
, which yields a string slice already, making the to_string
call suspicious.
cx, | ||
super::PARSE_TO_STRING, | ||
expr.span.with_lo(span.lo()), | ||
"parsing a the output of `.to_string()`", |
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.
"parsing a the output of `.to_string()`", | |
"parsing the output of `.to_string()`", |
Thanks for your review! The I agree about some of the search results being non-suspicious: E.g. https://github.com/clearloop/leetcode-cli/blob/04a611b7996d8a6d8b622feecb844f248c465bd1/src/helper.rs#L147 uses I do not think that the output type of the matched However, this lint should indeed skip I'm new to clippy development and it might take a while for me to make time to learn the API well enough to make all required changes. However, I am nevertheless interested in massaging this PR to completion (or rejection). Edit: reviewing a few more search results, it's tricky. Some seem indeed suspicious, others are a legitimate, e.g. formatting a UUID as string, then parsing it as HTTP header value. One way I see to make this not have far too many false positives is to check if a fallible or infallible conversion between the input and output types exists - which still doesn't take lossy conversions into account, so it fails at achieving the lint's original goal. Is this only going to work by explicitly checking for input and output type pairs which are known to convert lossily via string? |
So you would avoid linting cases where the input type implements neither |
Inspired by rust-lang/rust#120048 (comment)
.stderr
file)cargo test
passes locallycargo dev update_lints
cargo dev fmt
r? @llogiq
changelog: add [
parse_to_string
] lint