Add new lint: strlen_on_c_strings#7243
Conversation
|
r? @giraffate (rust-highfive has picked a reviewer for you, use r? to override) |
8f0a09a to
c3de3a9
Compare
| } | ||
|
|
||
| if_chain! { | ||
| if let hir::ExprKind::Call(func, args) = expr.kind; |
There was a problem hiding this comment.
drive-by nit
| if let hir::ExprKind::Call(func, args) = expr.kind; | |
| if let hir::ExprKind::Call(func, [recv]) = expr.kind; |
|
☔ The latest upstream changes (presumably #7253) made this pull request unmergeable. Please resolve the merge conflicts. |
|
ping from triage @mgacek8. rust-lang/rust#85439 has been merged, so is this ready for review? |
c3de3a9 to
441420d
Compare
Not yet, I resolved conflicts, but I need to investigate why |
|
☔ The latest upstream changes (presumably #7315) made this pull request unmergeable. Please resolve the merge conflicts. |
3fb1c7d to
9feae0d
Compare
9feae0d to
724da32
Compare
|
It is ready for the review, please take a look. Can we change the label ? |
giraffate
left a comment
There was a problem hiding this comment.
Overall looks good, thanks! I made some comments.
724da32 to
9ef47c9
Compare
|
☔ The latest upstream changes (presumably #7400) made this pull request unmergeable. Please resolve the merge conflicts. |
9ef47c9 to
e02d468
Compare
tests/ui/strlen_on_c_strings.stderr
Outdated
| --> $DIR/strlen_on_c_strings.rs:11:24 | ||
| | | ||
| LL | let len = unsafe { libc::strlen(cstring.as_ptr()) }; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstring.as_bytes().len()` |
There was a problem hiding this comment.
The suggested code don't need unsafe block. Can unsafe block be checked? Or this comments needs to be improved to include removing unsafe block.
There was a problem hiding this comment.
Good catch, yes, unsafe block is redundant here. It can be removed when it's the only one unsafe operation inside of it.
What about for now to lint like this: "help: try this: cstring.as_bytes().len(), you might also need to get rid of unsafe block in some cases"?
There was a problem hiding this comment.
Yes, it looks good as a first step! That unsafe block might be removed is one of purposes of this lint.
If unsafe block has only libc::strlen(cstr.as_ptr()), it would be better to remove unsafe block in suggestion. But I think it can be fixed with another follow-up PR.
e02d468 to
5cad4f5
Compare
tests/ui/strlen_on_c_strings.stderr
Outdated
| --> $DIR/strlen_on_c_strings.rs:11:24 | ||
| | | ||
| LL | let len = unsafe { libc::strlen(cstring.as_ptr()) }; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstring.as_bytes().len(), you might also need to get rid of `unsafe` block in some cases` |
There was a problem hiding this comment.
Can this comment be fixed like this?
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstring.as_bytes().len(), you might also need to get rid of `unsafe` block in some cases` | |
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstring.as_bytes().len()`, you might also need to get rid of `unsafe` block in some cases |
There was a problem hiding this comment.
Seems like span_lint_and_sugg doesn't allow that.
What about: "help: try this (you might also need to get rid of unsafe block in some cases): cstring.as_bytes().len()" ?
5cad4f5 to
59a164e
Compare
|
@bors r+ Thanks! |
|
📌 Commit 59a164e has been approved by |
|
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
This is WIP, linting in case ofCStringhas been added, but forCStr, its diagnostic item needs to be available for clippy.PR that adds diagnostic item for CStr on rust repo.
Ready for the review. Please take a look.
fixes #7145
changelog: Add new lint:
strlen_on_c_strings, that lints onlibc::strlen(some_cstring.as_ptr())