-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 string_as_str
#9624
Conversation
r? @giraffate (rust-highfive has picked a reviewer for you, use r? to override) |
Thanks to @flip1995 for some hints :) |
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.
Some suggestions on how to clean up the lint code.
tests/ui/string_as_str.stderr
Outdated
LL | let a = s.as_str().chars().map(|c| c.is_digit(2)).collect::<Vec<_>>(); | ||
| ^^^^^^^^^^ help: replace it with: `&s` |
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.
This suggestion won't work. It should suggest (&s)
. You can use clippy_utils::Sugg
with maybe_paren
to automatically produce the parens.
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.
I don't think we should lint where parenthesis are needed. This feels like the canonical use of as_str
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.
Avoid to lint this
) | ||
} | ||
|
||
fn is_within_borrowed_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
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.
I'm not sure what this function is supposed to do. It looks like it checks for really specific things in the tests you've written.
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.
Don't know how to not lint here
None => Some(snip.as_str()), |
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 can't this be linted?
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 using &snip
there are different type on those two branch, one is &str and other &String, don't know if we can change the code in other way
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.
I'm not sure if this is a blocking issue since it's not a machine applicable lint.
This seems like a wrong inference from rustc
because explicit typing fixes the issue
let a: Option<&str> = match snip.split_once(" as ") {
None => Some(&snip),
Some((import, rename)) => {
if rename.trim() == name {
None
} else {
Some(import.trim())
}
},
}
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.
Exactly... Don't sure next step to complete this lint :( 🤔
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.
Took a closer look at this lint. The suggestion of changing this to &s
only applies when the String
can be coerced to str
. This is the case in calls where the argument is of type &str
. In every other place the suggestion has to be &*
I can see two paths forward here:
- Check if the expression parent is some kind of call and the parameter of that function/method/closure has type
&str
- Just lint everything and suggest
&*
. But in this case, this lint should berestriction
IMO, because I don't think that&*s
is better thans.as_str()
. Even for apedantic
lint, I think this is a questionable suggestion.
☔ The latest upstream changes (presumably #9566) made this pull request unmergeable. Please resolve the merge conflicts. |
@flip1995 Do you have any suggestion for checking parameter of |
I think we have a |
☔ The latest upstream changes (presumably #9667) made this pull request unmergeable. Please resolve the merge conflicts. |
Thank you for making Clippy better!
We're collecting our changelog from pull request descriptions.
If your PR only includes internal changes, you can just write
changelog: none
. Otherwise, please write a short commentexplaining your change.
It's also helpful for us that the lint name is put within backticks (
` `
),and then encapsulated by square brackets (
[]
), for example:If your PR fixes an issue, you can add
fixes #issue_number
into thisPR description. This way the issue will be automatically closed when
your PR is merged.
If you added a new lint, here's a checklist for things that will be
checked during review or continuous integration.
.stderr
file)cargo test
passes locallycargo dev update_lints
cargo dev fmt
Note that you can skip the above if you are just opening a WIP PR in
order to get feedback.
Delete this line and everything above before opening your PR.
Please write a short comment explaining your change (or "none" for internal only changes)
changelog: Add new lint [
string_as_str
]Closes #874