Skip to content
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: strlen_on_c_strings #7243

Merged
merged 1 commit into from
Jul 5, 2021

Conversation

mgacek8
Copy link
Contributor

@mgacek8 mgacek8 commented May 18, 2021

This is WIP, linting in case of CString has been added, but for CStr, 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 on libc::strlen(some_cstring.as_ptr())

@rust-highfive
Copy link

r? @giraffate

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 18, 2021
@mgacek8 mgacek8 marked this pull request as draft May 18, 2021 15:25
@mgacek8 mgacek8 force-pushed the issue7145_strlen_on_c_strings branch 2 times, most recently from 8f0a09a to c3de3a9 Compare May 18, 2021 18:54
}

if_chain! {
if let hir::ExprKind::Call(func, args) = expr.kind;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by nit

Suggested change
if let hir::ExprKind::Call(func, args) = expr.kind;
if let hir::ExprKind::Call(func, [recv]) = expr.kind;

@bors
Copy link
Collaborator

bors commented May 20, 2021

☔ The latest upstream changes (presumably #7253) made this pull request unmergeable. Please resolve the merge conflicts.

@giraffate giraffate 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 25, 2021
@giraffate
Copy link
Contributor

ping from triage @mgacek8. rust-lang/rust#85439 has been merged, so is this ready for review?

@mgacek8 mgacek8 force-pushed the issue7145_strlen_on_c_strings branch from c3de3a9 to 441420d Compare June 9, 2021 07:52
@mgacek8
Copy link
Contributor Author

mgacek8 commented Jun 9, 2021

ping from triage @mgacek8. rust-lang/rust#85439 has been merged, so is this ready for review?

Not yet, I resolved conflicts, but I need to investigate why is_type_diagnostic_item(cx, ty, sym::CStr) doesn't return true when type is CStr and fix it.

@bors
Copy link
Collaborator

bors commented Jun 10, 2021

☔ The latest upstream changes (presumably #7315) made this pull request unmergeable. Please resolve the merge conflicts.

@mgacek8 mgacek8 force-pushed the issue7145_strlen_on_c_strings branch 2 times, most recently from 3fb1c7d to 9feae0d Compare June 13, 2021 12:58
@mgacek8 mgacek8 marked this pull request as ready for review June 13, 2021 13:01
@mgacek8 mgacek8 force-pushed the issue7145_strlen_on_c_strings branch from 9feae0d to 724da32 Compare June 13, 2021 13:37
@mgacek8
Copy link
Contributor Author

mgacek8 commented Jun 24, 2021

It is ready for the review, please take a look. Can we change the label ?

Copy link
Contributor

@giraffate giraffate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, thanks! I made some comments.

clippy_lints/src/strlen_on_c_strings.rs Outdated Show resolved Hide resolved
clippy_lints/src/strlen_on_c_strings.rs Show resolved Hide resolved
clippy_lints/src/strlen_on_c_strings.rs Outdated Show resolved Hide resolved
clippy_lints/src/strlen_on_c_strings.rs Outdated Show resolved Hide resolved
@mgacek8 mgacek8 force-pushed the issue7145_strlen_on_c_strings branch from 724da32 to 9ef47c9 Compare June 30, 2021 07:20
@bors
Copy link
Collaborator

bors commented Jun 30, 2021

☔ The latest upstream changes (presumably #7400) made this pull request unmergeable. Please resolve the merge conflicts.

@mgacek8 mgacek8 force-pushed the issue7145_strlen_on_c_strings branch from 9ef47c9 to e02d468 Compare June 30, 2021 20:42
--> $DIR/strlen_on_c_strings.rs:11:24
|
LL | let len = unsafe { libc::strlen(cstring.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstring.as_bytes().len()`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created the issue for that: #7436

@mgacek8 mgacek8 force-pushed the issue7145_strlen_on_c_strings branch from e02d468 to 5cad4f5 Compare July 5, 2021 07:16
--> $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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this comment be fixed like this?

Suggested change
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()" ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it looks good!

@mgacek8 mgacek8 force-pushed the issue7145_strlen_on_c_strings branch from 5cad4f5 to 59a164e Compare July 5, 2021 09:10
@giraffate
Copy link
Contributor

@bors r+

Thanks!

@bors
Copy link
Collaborator

bors commented Jul 5, 2021

📌 Commit 59a164e has been approved by giraffate

@bors
Copy link
Collaborator

bors commented Jul 5, 2021

⌛ Testing commit 59a164e with merge 64d74df...

@bors
Copy link
Collaborator

bors commented Jul 5, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: giraffate
Pushing 64d74df to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lint on libc::strlen(some_cstr.as_ptr())
5 participants